Help reqd on SendMessage function!

CGK

Member
Joined
Jul 31, 2007
Messages
7
Programming Experience
Beginner

Hi!

I'm using the SendMessage function in the code. But the following error is occuring.

"A call to PInvoke function 'XXXXXXX::SendMessage' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature." :confused:

The function declaration is like this:

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

The function is called like this:

Index = SendMessage(ctrl.Handle, CB_FINDSTRING, -1, ctrl.Text)

Where Index is 'Long' type variable and ctrl.Handle refers to a combo box handler.


Any pointers/Suggestions would be very helpful.
TIA
CGK :cool:
 
That's a VB6 signature. PInvoke calls generally expect 32-bit numbers. In VB6 the Long type was 32-bit, but in VB.NET the Long type is 64-bit and the Integer type is 32-bit. Change all your Longs to Integers and you should be OK.

I suggest that you download the API Viewer and set the language option to VB.NET. That will then give you valid VB.NET signatures.
 
Note also that PostMessage can be better to use than SendMessage - if the destination window has hung, SendMessage will hang your app too..
 
Er.. In my code they are either IntPtr, UInteger or HandleRef?

http://www.pinvoke.net/default.aspx/user32/SendMessage.html

There are generally numerous ways you can declare the same API function. Various managed types can be converted to the same unmanaged types implicitly. Here's some of my current code:
VB.NET:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd,
				uint wMsg,
				int wParam,
				IntPtr lParam);

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd,
				uint wMsg,
				int wParam,
				int lParam);

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd,
				uint wMsg,
				int wParam,
				string lParam);
The data can be passed to the lParam argument as an IntPtr, an Integer or a String, plus it would probably bear other types too.

I would tend to use IntPtr or UInteger most of the time but if the OP is already using Long then switching to Integer will require no other code changes, where some of the others might.
 
Back
Top