Windows 下 c# 给 c++ 发送消息,记录下来。 这里是 c# 发送消息的代码: using System.Runtime.InteropServices; public class Useful { public const int MWM_TEST = 6688; [DllImport("user32.dll", EntryPoint = "FindWindow")] public static extern IntPtr FindWindow(String strClassName, String strWindowName); [DllImport("user32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam); } public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void buttonSend_Click(object sender, EventArgs e) { int iId = Convert.ToInt32(textId.Text); int iOp = comboOperate.SelectedIndex; IntPtr hwnd = IntPtr.Zero; hwnd = Useful.FindWindow(null, "Cpp_HandleMsg"); if( IntPtr.Zero != hwnd ) { Useful.SendMessage(hwnd, Useful.MWM_TEST, (IntPtr)iId, (IntPtr)iOp); } } } 这里是 c++ 接收消息的代码: afx_msg LRESULT OnTest( WPARAM wParam, LPARAM lParam ); ON_MESSAGE(MWM_TEST, &CMainFrame::OnTest) LRESULT CMainFrame::OnTest( WPARAM wParam, LPARAM lParam ) { int id = (int)wParam; int op = (int)lParam; CString strOperate; if( op ) { strOperate.Format( _T("%d inserted !"), id ); } else strOperate.Format( _T("%d delete !"), id ); AfxMessageBox( strOperate ); return 0; } 结果图如下: