3
I began to study the methods of SendMessage
and PostMessage
of User32.dll
and I came up with some questions. I will put down the code I tested and soon after the questions, if anyone can help me.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public extern static int SendMessage(IntPtr hwnd, uint msg, uint wParam, uint lParam);
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public extern static int PostMessage(IntPtr hwnd, uint msg, uint wParam, uint lParam);
void FechaNotepad()
{
IntPtr handle = FindWindow("Notepad", "Sem título - Bloco de Notas");
const int WM_QUIT = 0x0012;
const int WM_CLOSE = 0x0010;
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
const int WM_COMMAND = 0x111;
const int WM_LBUTTONDOWN = 0x201;
const int WM_LBUTTONUP = 0x202;
const int WM_LBUTTONDBLCLK = 0x203;
const int WM_RBUTTONDOWN = 0x204;
const int WM_RBUTTONUP = 0x205;
const int WM_RBUTTONDBLCLK = 0x206;
SendMessage(handle, WM_CLOSE, 0, 0);
SendMessage(handle, WM_QUIT, 0, 0);
PostMessage(handle, WM_CLOSE, 0, 0);
PostMessage(handle, WM_QUIT, 0, 0);
}
Doubts
In order for me to actually execute the commands in the found process, I have to execute a SendMessage()
and soon after a PostMessage()
? It doesn’t just work by giving one PostMessage()
?
Where I find addresses of messages that can be sent, such as sending the message to press the key F1
keyboard?
Yes, it only works with Postmessage(), but you should analyze your need, and for that you can check the difference between both. The function Sendmessage is synchronous, and the function Postmessage is asynchronous. Here is a tutorial where you can evaluate this difference: https://www.autohotkey.com/docs/misc/SendMessage.htm. The message address you can find in the winapi documentation. h, in the case of the F1 key = 0x70, as you can see here: https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
– Paulo Santos
Formulate your comment as a response so I can choose it after the tests performed friend.
– Victor Laio