I am trying to use the SendMessage API and debug why some calls seem to fail. I am starting to think that my original assumption that the API actually can return errors might be where I am going wrong though, which was the route I began with - getting the error code from the call, since something is going south. What I attempted in order to figure out how errors work, was to provide illegal input and see how the API reacts.
If I provide a negative handle, or a handle to a window that does not exist, the call returns false, and a call to
if (!SendMessage(new IntPtr(windowHandleId), WM_COPYDATA, 0, ref copyDataStruct))
var win32Error = Marshal.GetLastWin32Error();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool SendMessage(IntPtr windowHandle, int message, int wordParam, ref COPYDATASTRUCT longParam);
The documentation for
The return value specifies the result of the message processing; it depends on the message sent.
In other words, we must refer to the documentation of
WM_COPYDATA. It states:
If the receiving application processes this message, it should return TRUE; otherwise, it should return FALSE.
At no point in this is there any statement concerning
GetLastError. Which means that you cannot expect
GetLastError to yield an informative value. You'll just get whatever was passed in the most recent call to
SetLastError, quite possibly a call made before your call to
All that you can infer from a value of false being returned from
SendMessage is that the receiving application did not process the message.