| Content-Class: urn:content-classes:message
| Subject: CSocket creation failure
| Date: Tue, 4 Mar 2003 06:15:14 -0800
|
| I have a managed C++ project, the main project file is
| very simple. I tried to create a socket object then
| delete it twice. But the second time creation always
| thrown an exception as:
Hi,
I was able to take the sample code, build it with minimal modification
(declaring and initializing variables 'ipAddress' and 'port) and reproduce
the behavior you describe.
I believe this is occuring because your console app lacks a call to
AfxWinInit. For a console application, which does not use the MFC-supplied
WinMain function, you must call AfxWinInit directly to initialize MFC.
For more information, see :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcli...
_mfc_afxwininit.asp
BTW -- a debug version of the original sample code (without AfxWinInit)
will assert from AfxGetInstanceHandle. These asserts can be extremely
helpful in determining that there is a problem in an application.
--
Andrew Brown, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights.
--------------------
|
| An unhandled exception of
| type 'System.Runtime.InteropServices.SEHException'
| occurred in SocketTest.exe
| Additional information: External component has thrown an
| exception.
|
| And the break point is in the MFC file "wingdi.cpp",
| function:
|
| // resource failure
| void AFXAPI AfxThrowResourceException()
| {
| THROW((CResourceException*)
| &_simpleResourceException);
| }
|
|
| My main function is as simple as:
|
| // This is the entry point for this application
| int _tmain(void)
| {
| // TODO: Please replace the sample code below with your
| own.
| Console::WriteLine(S"Hello World");
|
| // TEST SOCKET
| if(!AfxSocketInit())
| AfxMessageBox("Failed to Initialize Sockets",MB_OK|
| MB_ICONSTOP);
|
| CSocket* socketObj = new CSocket();
| if (!socketObj->Create())
| {
| MessageBox(NULL, "Create failed.........", NULL,
| MB_OK);
| }
| if (!socketObj->Connect(ipAddress, port))
| {
| MessageBox(NULL, "Connection failed.........",
| NULL, MB_OK);
| }
| delete socketObj;
| socketObj = NULL;
|
| CSocket* socketObj1 = new CSocket();
| if (!socketObj1->Create()) // THIS ONE THROWN AN
| EXCEPTION
| {
| MessageBox(NULL, "Create failed.........", NULL,
| MB_OK);
| }
| if (!socketObj1->Connect(ipAddress, port))
| {
| MessageBox(NULL, "Connection failed.........",
| NULL, MB_OK);
| }
| delete socketObj1;
|
| return 0;
| }
|
|