471,350 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Question on Marshal of a unsigned short


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.

Nov 26 '05 #1
2 7924

"scottt" <sc****@discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...

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.
Nov 26 '05 #2
Thank you. so simple I over looked it.
"Willy Denoyette [MVP]" wrote:

"scottt" <sc****@discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...

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.

Nov 28 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Paul Edwards | last post: by
8 posts views Thread by avsrk | last post: by
4 posts views Thread by slougheed | last post: by
5 posts views Thread by wolverine | last post: by
1 post views Thread by wolverine | last post: by
10 posts views Thread by Jim Langston | last post: by
2 posts views Thread by CptDondo | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.