Question on Marshal of a unsigned short

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to call into a C++ DLL from my C# code.
The function is expecting a void pointer to an unsigned short.
Which would be more correct?

UInt16 wRegData;


IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(wRegData));
Marshal.StructureToPtr(wRegData, p, false);

or

IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(wRegData));
Marshal.WriteInt16(p, (short) wRegData);


Thank you.
 
scottt said:
I need to call into a C++ DLL from my C# code.
The function is expecting a void pointer to an unsigned short.
Which would be more correct?

UInt16 wRegData;


IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(wRegData));
Marshal.StructureToPtr(wRegData, p, false);

or

IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(wRegData));
Marshal.WriteInt16(p, (short) wRegData);


Thank you.

ushort wRegData= 123;

fncExpectingPointerToUInt(ref wRegData);

Willy.
 

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

Back
Top