ANM

Hi,

I need to pass the address of an unsigned long variable from C# to a C++ library. The API in C++ expects a DWORD* as parameter and passes a value back to the caller. In C#, I declared an IntPtr variable, allocated memory uisng AllocHGlobal call to that and passed it by typecasting to ulong. On the C# side, it contains a valid value but on the C++ the function in C++ gets a null value.

Here is the code snippet.

The API in the unmanaged code is like this...

int UnmanagedCppCall(

H__HANDLE handle,

void *bufferPtr,

DWORD bufferSize,

DWORD *receivedBytesPtr)

The code in C# is like this...

byte[] traceByteArray;

ulong traceDataSize = 0;

System.IntPtr dataSizePtr;

traceBuf = Marshal.AllocHGlobal((Int32)MAX_BUFFER_LENGTH);

dataSizePtr = Marshal.AllocHGlobal((Int32)sizeof(ulong));

result = UnmanagedCppCall(handle, traceBuf, MAX_BUFFER_LENGTH, dataSizePtr);

Marshal.Copy(traceBuf, traceDetail, 0, 4);

traceByteArray = new byte[traceDataSize];

Marshal.Copy(traceBuf, traceByteArray, 0,(int)traceDataSize);

traceDataSize = (ulong)Marshal.ReadInt64(dataSizePtr);

Marshal.FreeHGlobal(traceBuf);

Marshal.FreeHGlobal(dataSizePtr);

the 'dataSizePtr' contains a valid value, but when I step inside the call 'UnmanagedCppCall', the 4th parameter 'receivedBytesPtr is just null.

Incidentally the traceBuf contains a valid address in C# as well as C++ code and it passes back correct data to the caller.

Can anybody help me out please...

ANM



Re: Visual C# General Passing address of unsigned long variable from managed code to unmanaged code

Peter Ritchie

DWORD is 32-bits. To call a C++ function with a DWORD pointer parameter, declare your method as follows:

Code Snippet

[DllImport("cpplib.dll", ExactSpelling = true)]

private static extern void GiveMeLong(ref Int32 longNumber);

And call it as follows:

Code Snippet

Int32 longNumber = 1;

GiveMeLong(ref longNumber);






Re: Visual C# General Passing address of unsigned long variable from managed code to unmanaged code

ANM

I did that. But still in C++ code, the value becomes zero. The passed value in managed code is '0x00000001' and in the called function, it becomes '0x00000000'. What could be the reason





Re: Visual C# General Passing address of unsigned long variable from managed code to unmanaged code

Peter Ritchie

Could be many things, can you post the C# declaration of the UnmanagedCppCall method (i.e. the DllImport-ed method). Could be that's not declared compatible with the sizes of arguments on the C++ side.




Re: Visual C# General Passing address of unsigned long variable from managed code to unmanaged code

ANM

Here it is...

This is the function declared in the unmanaged code (cpp)

int UnmanagedCppCall(

H__HANDLE handle,

void *bufferPtr,

DWORD bufferSize,

DWORD *receivedBytesPtr)

This is the code snippet of the caller in C#

[DllImport("CPPlib.dll", ExactSpelling=true)]

public static extern int UnmanagedCppCall(System.IntPtr handle, System.IntPtr bufferPtr, ulong size, ref Int32 bytereceived);

byte[] traceByteArray;

ulong traceDataSize = 0;

Int32 dataSizePtr = 1;

traceBuf = Marshal.AllocHGlobal((Int32)MAX_BUFFER_LENGTH);

result = UnmanagedCppCall(handle, traceBuf, MAX_BUFFER_LENGTH, ref dataSizePtr);





Re: Visual C# General Passing address of unsigned long variable from managed code to unmanaged code

Peter Ritchie

Ahh, that'll do it. "long" in C# is not the same as "long" in C++. "long" in C# is 64-bits, in Visual C++ it's 32. Change your "ulong" to "UInt32"




Re: Visual C# General Passing address of unsigned long variable from managed code to unmanaged code

ANM

My point of concern is the last parameter and it is already Int32





Re: Visual C# General Passing address of unsigned long variable from managed code to unmanaged code

ANM

the fourth parameter in the call is 'Int32 dataSizePtr' and it becomes 0 in the unmanaged code. Inside the unmanaged code, they check the value of 'receivedBytesPtr' and if it null, they return without assigning any value. I assigned value 1 to dataSizePtr, but when I step through the unmanaged code, the receivedBytesPtr becomes 0.





Re: Visual C# General Passing address of unsigned long variable from managed code to unmanaged code

JohnWein

You're not listening. You pass 64 bits to a function that expects 2 X 32 bits.



Re: Visual C# General Passing address of unsigned long variable from managed code to unmanaged code

ANM

Oh. I am sorry, I got it and it worked. thanks a lot...