Dominik Dominik - 9 months ago 102
Java Question

using SendMessage from user32.dll with jna in java - error

I try to use the:

LRESULT WINAPI SendMessage(_In_ HWND hWnd, _In_ UINT Msg,
_In_ WPARAM wParam, _In_ LPARAM lParam);

in Java with jna and i keep getting a error:

Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: Error looking up function 'SendMessage'

this is my interface:

public interface User32 extends StdCallLibrary {
Pointer GetForegroundWindow();
int SendMessage(Pointer hWnd, int msg, int num1, int num2);

and i call it like that:

Pointer hW = user32.GetForegroundWindow();
user32.SendMessage(hW, 0x0201, 0, 0);
user32.SendMessage(hW, 0x0202, 0, 0);

the hWnd is right. where is my mistake?

Answer Source

JNA can not find the function "SendMessage" in user32.dll because there no function of that name exported.

This is because SendMessage is an old name that is automatically mapped by the other compilers to the matching ANSI or UNICODE version of the function - SendMessageA and SendMessageW.

Using tools that show the exported functions of a DLL like DependencyWalker you can see that user32.dll of Windows 7 for example only knows both functions SendMessageA and SendMessageW but no SendMessage.

The function definition you use looks like the ANSI version, hence you should use SendMessageA instead.

BTW. It doesn't make any difference if you are using 32bit or 64bit Java and user32.dll. What I wrote is true for both versions.