-3

I have my code like below

[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern uint SendMessage(IntPtr hWnd, int unMsg, IntPtr wParam, 
    IntPtr lParam);

Fxcop error : "As it is declared in your code, the return type of P/Invoke 'SendMessage(IntPtr, int, IntPtr, IntPtr)' will be 4 bytes wide on 64-bit platforms. This is not correct, as the actual native declaration of this API indicates it should be 8 bytes wide on 64-bit platforms. Consult the MSDN Platform SDK documentation for help determining what data type should be used instead of 'uint'"

So to fix the above error do i need to change the return type UIntPtr ?

Please help me in fixing this and give me valid reasons.

Thanks, VMG

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
user3766691
  • 143
  • 1
  • 3
  • 7
  • 1
    It is an entirely correct warning. The return type must be `IntPtr`. – Hans Passant Jun 23 '14 at 10:31
  • `SendMessage` must be one of the most fequently P/Invoked methods in the native API. As such, the notes over on [pinvoke.net](http://www.pinvoke.net/default.aspx/user32/sendmessage.html) seem to be quite good and accurate. – Damien_The_Unbeliever Jun 23 '14 at 10:32

1 Answers1

1

The message is correct. The function returns LRESULT which is a signed, pointer sized type. So in C# you should use IntPtr.

Strictly speaking, the message parameter is unsigned, WPARAM is unsigned and LPARAM is signed, so if you wish to follow suit then you would have:

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SendMessage(
    IntPtr hWnd, 
    uint Msg, 
    UIntPtr wParam, 
    IntPtr lParam
);

This information is all documented:

However, because SendMessage is a one size fits all kind of a function, it's not worth getting too hung up on signed and unsigned types. Different messages treat the parameters differently, using casts. So feel free to use the types that are most convenient, modulo the fact that they must clearly be the right size.

norbjd
  • 10,166
  • 4
  • 45
  • 80
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490