Title

怎么将C#通过发消息传递给指定C++窗口

C# 发送方

复制代码
//引入 Interop 库
using System.Runtime.InteropServices;

//定义 SendMessage API
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

//定义发送消息的常量
static uint WM_USER = 0x0400;

//在需要发送消息的位置调用 SendMessage API
SendMessage(hWnd, WM_USER, IntPtr.Zero, IntPtr.Zero);
//如果是字符串
SendMessage(hwnd, WM_USER, Marshal.StringToHGlobalAnsi(""), (IntPtr)0);
复制代码

C++接收方

//如果是接收字符串
LPCSTR str = reinterpret_cast<LPCSTR>(wParam);
CString cstr = CString(str);
//如果是int类型
int code = wParam

hwnd为C++指定窗口

 

posted @   WAASSTT  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
Title
点击右上角即可分享
微信分享提示