C++ to C# with unsigned long *

T

tutush

Hi there,
I have problem with importing my C++ code to C#.

The c++ looks like these

extern _declspec(dllexport) const char*
SendAndReceiveBufferedWrp(__int64 hConnection, const char*
pchSendBuffer, int iCmdDataMode,unsigned long bufferLength, unsigned
long * pulNumBytesRcved) {
}

And the C#
[DllImport("\\dll\\Test.dll", EntryPoint =
"SendAndReceiveBufferedWrp")]
unsafe private static extern string
SendAndReceiveBufferedDll(Int64 connection, string sendBuffer,int
cmdDataMode,ulong bufferLength,ref ulong numbytes);
unsafe public string SendAndReceiveBuffered(Int64 connection,
string sendBuffer,int cmdDataMode,ulong bufferLength,out ulong
numbytes)
{
return SendAndReceiveBufferedDll(connection, sendBuffer,
cmdDataMode, bufferLength,ref numbytes);
}


in the c++ the pulNumBytesRcved pointer is allways NULL there is no
address passed.
Could somebody help me with thise ?

Thanks in advance.
 
C

Carl Daniel [VC++ MVP]

Hi there,
I have problem with importing my C++ code to C#.

The C++ type unsigned long is 32 bits - equivalent to System.UInt32.

The C# type ulong is 64 bits, equivalent to unsigned __int64.

Fix the declarations in the C# code and you should be okay.

-cd
 
T

tutush

Thanks I have got it mean while. But now I got a new problem I have
structure that is

typedef struct _tagEventstruct
{
unsigned long ulVersion;
__int64 hConnection;
unsigned long ulListener;
unsigned long ulType;
unsigned long ulId;
unsigned long ulSubClass;
unsigned long ulTimeStampSec;
unsigned long ulTimeStampMicroSec;
unsigned char* puchData;
unsigned long ulDataLength;
unsigned short* pwszDesc;
unsigned short wszModuleName[128];
unsigned long ulModuleLineNumber;
#ifndef SWIG
void** ppData;
#else
unsigned long ppData;
#endif
}XdsEvent;

and in C# it is like


[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
public struct XdsEvent
{
public uint ulVersion;
public Int64 hConnection;
public uint ulListener;
public uint ulType;
public uint ulId;
public uint ulSubClass;
public uint ulTimeStampSec;
public uint ulTimeStampMicroSec;
public IntPtr puchData;
public uint ulDataLength;
public IntPtr pwszDesc;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=128)]public
string wszModuleName;
public uint ulModuleLineNumber;
public IntPtr ppData;
}

It works fine for some data i.e. wszModuleName but some data is allways
null.

I am getting the pwszDesc string value by
string data = Marshal.PtrToStringUni(eventdata.pwszDesc);
and
string data = Marshal.PtrToStringAnsi(eventdata.puchData);

but they allways return null also some othere data like ulDataLength
return bad numbers.


At the moment I am also stuck with a function
in C++

xds_bool BroadcastMessageReceived( __int64 hConnection,
const
unsigned char* pData,
unsigned
long ulDataLength,
unsigned
long ulStatusCode,
const
unsigned short * wszDesc,
const
unsigned short * wszModuleName,
unsigned
long ulLineNumber )

where wszDesc and wszModuleName are null terminate Unicode strings

and in C#
[DllImport("\\XdsIII.dll", EntryPoint =
"BroadcastMessageReceived")]
private static extern bool BroadcastMessageReceivedDll(Int64
connection, IntPtr data, uint dataLength, uint statusCode, IntPtr desc,
IntPtr moduleName, uint lineNumber);
public bool BroadcastMessageReceived(Int64 connection, string
data, uint dataLength, uint statusCode, string description, string
moduleName, uint lineNumber)
{

return
BroadcastMessageReceivedDll(connection,Marshal.StringToHGlobalAnsi(data),
dataLength, statusCode, Marshal.StringToHGlobalUni(description),
Marshal.StringToHGlobalUni(moduleName), lineNumber);
}

but it keeps faling with AccessViolationException

Could you help me with these?
 
T

tutush

I solved it. The problem was that the structure defined in c++ was in
wrong order and C# did not import it correctly. Although what I dont
know is that is this a standard behaviour that some data is swithced
due to some BigEndien stuff ?!

Finally finished.
 

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