The "/clr" flag (C++ compiler) corrupt my structures !!

G

Guest

I m trying to create a C++ managed class to use it in C#. I m new to MC++ and
interoperability and I try to find a bug since a long time without succes,
maybe you can help me ?

(I have a sample project if you want to debug it, you can contact me at
(e-mail address removed) too)

I describe the problem:

I have 2 MS VC2003 projects... they contains the same set of classes. I have
difference between both projetc, the first one is a MC++ project I have
create (C++ project that use the /clr flag to have a Managed DLL for .NET),
the second is the original C++ library (that works fine!)

1 - Here is my managed class :
----------------------------------------------
public __gc class UFOzSocket
{
private:
addrinfo _hints;
public:
UFOzSocket(AddressFamily addressFamily, SocketType socketType);
~UFOzSocket();
void Connect(EndPoint * remoteEP);
protected:
UFOzSocket();
private:
UDTSOCKET _internalSocket;
};

During debugging, when I see this structure, it is like this :

Structure in the first application, see by the debugger (BAD fields order):
-----------------------------------------------------------------------------
- _hints : addrinfo
+ ai_addr : 0x00000000 sockaddr*
+ ai_addrlen : 0 unsigned __int32
+ ai_canonname : 0x00000000 char*
+ ai_family :2 __int32
+ ai_flags :1 __int32
+ ai_next : 0x00000000 addrinfo*
+ ai_protocol : 0 __int32
+ ai_socktype : 1 __int32

Structure in the second application, see by the debugger (GOOD fields order):
----------------------------------------------------------------------------------
- hints {ai_flags=1 ai_family=2 ai_socktype=1 ...} addrinfo
+ ai_flags : 1 int
+ ai_family : 2 int
+ ai_socktype : 1 int
+ ai_protocol : 0 int
+ ai_addrlen : 0 unsigned int
+ ai_canonname : 0x00000000 <Bad Ptr> char *
+ ai_addr : 0x00000000 {sa_family=??? sa_data=0x00000002 <Bad
Ptr> } sockaddr *
+ ai_next : 0x00000000 {ai_flags=??? ai_family=???
ai_socktype=??? ...} addrinfo *

So, how can I change this to force the first application to have the same
behavior ?

When I debug the second program (original one) all is fine. My solution
crash due to the fact that the structure is not good !

Thanks for your help

Chris
 
T

Tanja Krammer

Try to make your structure explicitely unmanaged with
#pragma unmanaged
struct UDTSOCKET {
}
#pragma managed
public __gc class UFOzSocket{
....
}
 
M

Marcus Heege

Hi Chris,

Chris said:
I m trying to create a C++ managed class to use it in C#. I m new to MC++
and
interoperability and I try to find a bug since a long time without succes,
maybe you can help me ?

(I have a sample project if you want to debug it, you can contact me at
(e-mail address removed) too)

I describe the problem:

I have 2 MS VC2003 projects... they contains the same set of classes. I
have
difference between both projetc, the first one is a MC++ project I have
create (C++ project that use the /clr flag to have a Managed DLL for
.NET),
the second is the original C++ library (that works fine!)

1 - Here is my managed class :
----------------------------------------------
public __gc class UFOzSocket
{
private:
addrinfo _hints;
public:
UFOzSocket(AddressFamily addressFamily, SocketType socketType);
~UFOzSocket();
void Connect(EndPoint * remoteEP);
protected:
UFOzSocket();
private:
UDTSOCKET _internalSocket;
};

During debugging, when I see this structure, it is like this :

Structure in the first application, see by the debugger (BAD fields
order):
-----------------------------------------------------------------------------
- _hints : addrinfo
+ ai_addr : 0x00000000 sockaddr*
+ ai_addrlen : 0 unsigned __int32
+ ai_canonname : 0x00000000 char*
+ ai_family :2 __int32
+ ai_flags :1 __int32
+ ai_next : 0x00000000 addrinfo*
+ ai_protocol : 0 __int32
+ ai_socktype : 1 __int32

Structure in the second application, see by the debugger (GOOD fields
order):
----------------------------------------------------------------------------------
- hints {ai_flags=1 ai_family=2 ai_socktype=1 ...} addrinfo
+ ai_flags : 1 int
+ ai_family : 2 int
+ ai_socktype : 1 int
+ ai_protocol : 0 int
+ ai_addrlen : 0 unsigned int
+ ai_canonname : 0x00000000 <Bad Ptr> char *
+ ai_addr : 0x00000000 {sa_family=??? sa_data=0x00000002 <Bad
Ptr> } sockaddr *
+ ai_next : 0x00000000 {ai_flags=??? ai_family=???
ai_socktype=??? ...} addrinfo *

So, how can I change this to force the first application to have the same
behavior ?

When I debug the second program (original one) all is fine. My solution
crash due to the fact that the structure is not good !

Thanks for your help

Chris

I can't tell you the precise source of your problem, but I am confident that
it is not related to C++/CLI interop. I assume you include different headers
in the different projects. Using #pragma unmanaged will not be useful here.

Marcus Heege
 
G

Guest

Yes,.... #pragma unmanaged do not help me !

The header that I include are :

#include <winsock2.h>
#include <Ws2tcpip.h>

Theses are standard headers file !! How can I include differents files ?!?!
I do not know how it is possible !

Have you another idea ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top