pInvoke - arguments marshaling

A

Ashutosh

pInvoke - arguments marshaling
Hi,
I am using pInvoke and the member function has signature like this in C++

int F1(ULONG handle, LPCTSTR s1, LPCTSTR s2, LPCTSTR s3, LPCTSTR s4)

so, I am using this signature in C#

public static extern int F1([MarshalAs(UnmanagedType.U8)] UInt64 handle,
[MarshalAs(UnmanagedType.LPTStr)]string s1,
[MarshalAs(UnmanagedType.LPTStr)]string s2,
[MarshalAs(UnmanagedType.LPTStr)]string s3,
[MarshalAs(UnmanagedType.LPTStr)]string s4);

So, if I call it like this

F1(1234,"Earth","Moon","Sun","");

then I am receiving these values in C++ dll
handle= 1234
s1= null
s2= Earth
s3= Moon
s4= Sun

The arguments seems to be shifted.

If I remove the ULONG from the declaration at both place, everything
works fine.


ULONG is 64 bit unsigned int. So, it should be passed as U8!! So, whats
wrong here???

Please advice!!

Ashutosh
 
S

Stephen Martin

ULONG is a 32bit unsigned integer. long in C++ and long in C# are not the
same thing.
 
A

Ashutosh Bhawasinka

Thanks! I got confused with Visual Basic ULong!!! All this time I was
seeing VB's document in MSDN :)

Stephen said:
ULONG is a 32bit unsigned integer. long in C++ and long in C# are not the
same thing.

Ashutosh said:
pInvoke - arguments marshaling
Hi,
I am using pInvoke and the member function has signature like this in C++

int F1(ULONG handle, LPCTSTR s1, LPCTSTR s2, LPCTSTR s3, LPCTSTR s4)

so, I am using this signature in C#

public static extern int F1([MarshalAs(UnmanagedType.U8)] UInt64 handle,
[MarshalAs(UnmanagedType.LPTStr)]string s1,
[MarshalAs(UnmanagedType.LPTStr)]string s2,
[MarshalAs(UnmanagedType.LPTStr)]string s3,
[MarshalAs(UnmanagedType.LPTStr)]string s4);

So, if I call it like this

F1(1234,"Earth","Moon","Sun","");

then I am receiving these values in C++ dll
handle= 1234
s1= null
s2= Earth
s3= Moon
s4= Sun

The arguments seems to be shifted.

If I remove the ULONG from the declaration at both place, everything works
fine.


ULONG is 64 bit unsigned int. So, it should be passed as U8!! So, whats
wrong here???

Please advice!!

Ashutosh
 

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