Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Assertion error when call Socket.Create() in Managed VC++ library.

5 views
Skip to first unread message

Jianxin

unread,
Nov 13, 2002, 1:42:29 PM11/13/02
to
I have been wrapping my MFC CSocket derived classes in
managed VC++ extesion (managed VC++ library). When I call
the CSocket.Create() function from an external C# program
(debug version), I always get a assertion error. If I
ignore the error, all the socket communication is OK. In
the release version there is no assertion error,
everything seems ok.

Also I found that if I do not call AfxSocketInit(), there
will be no this assertion error. Of course the socket
communication cannot go through either.

Any idea about the problem.

Thanks

Jianxin

Bobby Mattappally [MS]

unread,
Nov 13, 2002, 6:22:36 PM11/13/02
to
Jianxin,
What is the exact error and does it point to a line of code?

Hope this helps.

Thank you,
Bobby Mattappally
Microsoft VC++/C# Team

This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
>Content-Class: urn:content-classes:message
>From: "Jianxin" <jh...@smartstation.com>
>Sender: "Jianxin" <jh...@smartstation.com>
>Subject: Assertion error when call Socket.Create() in Managed VC++ library.
>>X-Tomcat-NG: microsoft.public.dotnet.languages.vc

Jianxin

unread,
Nov 14, 2002, 9:48:25 AM11/14/02
to
Thanks for your quick response.


I have tried to duplicate the problem in a more generic
way. Follows are what I did, and the error message:


I created a VC++ managed class libray from VC studio, and
created a managed class with public function to call
CSocket's Create() function. Here is the code:

#include "stdafx.h"
#include <afxsock.h> // MFC socket extensions
#include "Test.h"
namespace Test
{
void Class1::TestSocket()
{
AfxSocketInit();
CSocket sk;
sk.Create();
}
}

Then in a C# project, I call the TestSocket() function.
An assertion error will occur, when call sk.Create();


The error messages read:


Debug Assertion Failed:

Progrem XXX
File f:\vs70builds\9466
\vc\MFCATL\ship\atlmfc\include\afxwin1.inl
line: 23


Jianxin

>.
>

Bobby Mattappally [MS]

unread,
Nov 14, 2002, 8:30:22 PM11/14/02
to
Here is the callstack when the assert happens.

consoleCPP.exe!AfxGetInstanceHandle() Line 23 + 0x1e C++
consoleCPP.exe!AfxRegisterWndClass(unsigned int nClassStyle=0, HICON__ *
hCursor=0x00000000, HBRUSH__ * hbrBackground=0x00000000, HICON__ *
hIcon=0x00000000) Line 1404 + 0x5 C++
consoleCPP.exe!CAsyncSocket::AttachHandle(unsigned int hSocket=3780,
CAsyncSocket * pSocket=0x0012fdd8, int bDead=0) Line 419 + 0x22 C++
consoleCPP.exe!CAsyncSocket::Socket(int nSocketType=1, long lEvent=63,
int nProtocolType=0, int nAddressFormat=2) Line 574 C++
consoleCPP.exe!CAsyncSocket::Create(unsigned int nSocketPort=0, int
nSocketType=1, long lEvent=63, const char * lpszSocketAddress=0x00000000)
Line 107 + 0x14 C++
consoleCPP.exe!CSocket::Create(unsigned int nSocketPort=0, int
nSocketType=1, const char * lpszSocketAddress=0x00000000) Line 49 + 0x1d
C++

Basically what is happening sk.Create() calls AfxGetInstanceHandle()
during the Registration of Wndclass.
Since your code doesn't have any MFC initialization happening the
AfxGetInstanceHandle is returning NULL, resulting in the assert.

So you cannot use CSocket class just like you use CString class.

Is there is reson why you are not using .Net Socket class instead of the
MFC class?


Hope this helps.

Thank you,
Bobby Mattappally
Microsoft VC++/C# Team

This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
>Content-Class: urn:content-classes:message
>From: "Jianxin" <jh...@smartstation.com>
>Sender: "Jianxin" <jh...@smartstation.com>

>References: <1004401c28b44$6b9f8a90$2ae2...@phx.gbl>
<NvMDOt2iCHA.440@cpmsftngxa08>
>Subject: RE: Assertion error when call Socket.Create() in Managed VC++
library.
>Date: Thu, 14 Nov 2002 06:48:25 -0800

Jianxin

unread,
Nov 15, 2002, 9:26:22 AM11/15/02
to
Hi,

Thanks for the inside about the error I obverved.

The reason why I did not use the .NET socket is that I
just want to resue the old MFC code, and prefer not to
touch it if it works. This CSocket derived class was used
in a MFC window program. I just pull it out from the old
project and try to wrap it in a VC++ managed class library.

Is there any way for me to do a MFC intialization in VC++
managed libray in order to mute the assertion error?

It seems that the socket communication is still ok, even
after the assertion error in debug version. And in release
version, everything seems ok. Can I safely ignore the MFC
initialization problem, if I cannot do MFC initialization
in VC++ managed libray?

Thanks a lot for your help.

Jianxin

>.
>

Bobby Mattappally [MS]

unread,
Nov 18, 2002, 12:17:00 PM11/18/02
to
Jianxin,
My post last friday doesn't show up in google search.
Let me resend it.

Thanks.

Hope this helps.

Thank you,
Bobby Mattappally
Microsoft VC++/C# Team

This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------



>From: bob...@online.microsoft.com (Bobby Mattappally [MS])
>Organization: Microsoft
>Date: Fri, 15 Nov 2002 19:45:19 GMT
>Subject: RE: Assertion error when call Socket.Create() in Managed VC++
library.
>

>>Is there any way for me to do a MFC intialization in VC++
>>managed libray in order to mute the assertion error?
>

>Yes you can.
>You need to derive a class from CWinApp , initialize the socket in the
>InitInstance and then create a global instance of CMyWinApp.
>
>
>BOOL CMyWinApp::InitInstance(){
> CWinApp::InitInstance();
> if (!AfxSocketInit())
> {
> AfxMessageBox("Socket Initialization Failed");
> return FALSE;
> }
> // Just to make sure InitInstance gets called
> //You can comment this out later.
> AfxMessageBox("HEllo from InitInstance");
> return TRUE;
>}
>CMyWinApp theApp;
>
>Also define _USRDLL , preprocessor definition and make sure you link MFC
>libs ahead of the CRT libs.
>This can be done by making sure afxsock.h is the first header file
included
>in all the cpp files.
>
>I have attached a sample project that demonstrates this.
>Please let me know whether it works for you.
>
>Alternately, you could create an MFC user dll project with winsock support
>and add the /clr switch to it and remove the compiler options that are
>incompatible with the /clr switch.


>
>Hope this helps.
>
>Thank you,
>Bobby Mattappally
>Microsoft VC++/C# Team
>
>This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
>--------------------
>>Content-Class: urn:content-classes:message
>>From: "Jianxin" <jh...@smartstation.com>
>>Sender: "Jianxin" <jh...@smartstation.com>
>
>>

>>Hi,
>>
>>Thanks for the inside about the error I obverved.
>>
>>The reason why I did not use the .NET socket is that I
>>just want to resue the old MFC code, and prefer not to
>>touch it if it works. This CSocket derived class was used
>>in a MFC window program. I just pull it out from the old
>>project and try to wrap it in a VC++ managed class library.
>>

Jianxin

unread,
Nov 18, 2002, 12:56:06 PM11/18/02
to

Thanks you very much, Bobby. It worked for me.

I am impressed by the the exellent service provided by
this newsgroup.

Jianxin


>-----Original Message-----
>>Is there any way for me to do a MFC intialization in
VC++
>>managed libray in order to mute the assertion error?
>

>Hope this helps.
>
>Thank you,
>Bobby Mattappally
>Microsoft VC++/C# Team
>
>This posting is provided "AS IS" with no warranties, and
confers no rights.
>
>
>--------------------
>>Content-Class: urn:content-classes:message
>>From: "Jianxin" <jh...@smartstation.com>
>>Sender: "Jianxin" <jh...@smartstation.com>
>
>>

>>Hi,
>>
>>Thanks for the inside about the error I obverved.
>>
>>The reason why I did not use the .NET socket is that I
>>just want to resue the old MFC code, and prefer not to
>>touch it if it works. This CSocket derived class was
used
>>in a MFC window program. I just pull it out from the old
>>project and try to wrap it in a VC++ managed class
library.
>>

0 new messages