User Define Message

You need total 3 steps to define your message

 

[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wPar, IntPtr lPar);


 

public const int UserMsg = 0x0600;
SendMessage(this.Parent.Handle, UserMsg, (IntPtr)1, (IntPtr)1);


 

protected override void WndProc(ref Message m)
{
            switch (m.Msg)
            {
                case 0x0600:
                    
                    
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The PostMessage function is used to post a message to the message queue of a thread. This function is one of the ways to communicate between different threads in Windows. To hook the PostMessage function, you can use the Detours library provided by Microsoft. Detours allows you to intercept function calls and redirect them to your own function. Here's an example of how to hook the PostMessage function using Detours: ```c++ #include <windows.h> #include <detours.h> // Define a function pointer for the original PostMessage function typedef BOOL (WINAPI *POSTMESSAGE)(HWND, UINT, WPARAM, LPARAM); // Define a global variable to hold the address of the original PostMessage function POSTMESSAGE g_pOrigPostMessage = NULL; // Define your own PostMessage function BOOL WINAPI MyPostMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { // Do something before calling the original PostMessage function // ... // Call the original PostMessage function BOOL bRet = g_pOrigPostMessage(hWnd, Msg, wParam, lParam); // Do something after calling the original PostMessage function // ... return bRet; } // Hook the PostMessage function BOOL HookPostMessage() { // Get the address of the original PostMessage function HMODULE hUser32 = GetModuleHandle("user32.dll"); g_pOrigPostMessage = (POSTMESSAGE)GetProcAddress(hUser32, "PostMessageA"); // Detour the PostMessage function to your own function if (DetourTransactionBegin() == NO_ERROR) { if (DetourUpdateThread(GetCurrentThread()) == NO_ERROR) { if (DetourAttach(&(PVOID&)g_pOrigPostMessage, MyPostMessage) == NO_ERROR) { if (DetourTransactionCommit() == NO_ERROR) { return TRUE; } } } } return FALSE; } // Unhook the PostMessage function BOOL UnhookPostMessage() { // Detour the PostMessage function back to the original function if (DetourTransactionBegin() == NO_ERROR) { if (DetourUpdateThread(GetCurrentThread()) == NO_ERROR) { if (DetourDetach(&(PVOID&)g_pOrigPostMessage, MyPostMessage) == NO_ERROR) { if (DetourTransactionCommit() == NO_ERROR) { return TRUE; } } } } return FALSE; } // Example usage int main() { // Hook the PostMessage function if (HookPostMessage()) { // Call the PostMessage function PostMessage(NULL, WM_USER, 0, 0); // Unhook the PostMessage function UnhookPostMessage(); } return 0; } ``` Note that the Detours library is not officially supported by Microsoft and is intended for research and experimentation purposes only. Use it at your own risk.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值