32 vs 64 bit

  • Thread starter Thread starter Mark Chambers
  • Start date Start date
M

Mark Chambers

Hi there,

Given the following in Win32

[DllImport("User32.dll")]
internal static extern Int32 SendMessage(IntPtr hWnd, UInt32 Msg, UInt32
wParam, Int32 lParam);

the return value and last two parameters then become the following in Win64:

[DllImport("User32.dll")]
internal static extern Int64 SendMessage(IntPtr hWnd, UInt32 Msg, UInt64
wParam, Int64 lParam);

How would you then declare this in code so that it works in both
environments. Do I have to #define my own WIN32 constant for instance and do
a conditional compile or is there a more mainstream way. Note that if I do
the latter then won't this conflict with the /platform compiler option
(assuming it's set to "anycpu"). Any insight would be welcome. Thanks.
 
Mark,

You would use the IntPtr struct, not the int32 or int64 structures. The
IntPtr will be sized according to the platform it is on (32 or 64 bit).

Hope this helps.
 
You would use the IntPtr struct, not the int32 or int64 structures.
The IntPtr will be sized according to the platform it is on (32 or 64
bit).

Ok, thanks. I'll have to read up on this in more detail. The docs on this
structure imply that it's used for pointers and handles. Presumably you can
pass around any 32 or 64 value but I'll have find some info on the proper
marshalling mechanics. I'm also still confused on the precise rules for
prototyping Win32 functions in .NET. I've been trying to prescisely match
the Win32 types to their corresponding .NET types as seen below but I find
examples where the rules are loosely applied even by MSFT. They sometimes
use "int" where a "uint" is (technically) required, "bool" where an "int"
is required (e.g., Win32 functions returning TRUE or FALSE which are
actually 32 bit integers), etc. You can apparently pass a "string" to a
function taking an LPCTSTR, "StringBuilder" for in-out string paramters,
etc. Where are all the rules spelled out however (for instance, do you have
to explicitily NULL terminate these strings, pre-allocate their size for out
strings, etc.). Do you know of any good links.
 

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