C#与C++通信
# C#与C++相互发送消息 #
## C#端: ##
namespace CshapMessage
{
///
///
public partial class MainWindow : Window
{
IntPtr hwnd;
const int WM_COPYDATA = 0x004A;
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
[DllImport("User32.dll")]
public static extern int SendMessage(int hwnd, int msg, int wParam, ref COPYDATASTRUCT IParam);
[DllImport("User32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
public MainWindow()
{
InitializeComponent();
this.Title = "CshapMessage";
this.Loaded += MainWindow_Loaded;
this.Closed += MainWindow_Closed;
}
private void MainWindow_Closed(object sender, EventArgs e)
{
try
{
HwndSource.FromHwnd(hwnd).RemoveHook(WndProc);
}
catch (Exception) { }
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
hwnd = new WindowInteropHelper(this).Handle;
HwndSource.FromHwnd(hwnd).AddHook(new HwndSourceHook(WndProc));
}
///
///
///
///
///
private bool CshapSendMessage(int nMessgeId, String strSend)
{
int WINDOW_HANDLE = FindWindow(null, "VcMessage");//VcMessage为向C++程序发送的窗口名称
if (WINDOW_HANDLE != 0)
{
COPYDATASTRUCT cdata;
cdata.dwData = (IntPtr)100;//这里可以传入一些自定义的数据,但只能是4字节整数
cdata.lpData = strSend;//消息字符串
cdata.cData = System.Text.Encoding.Default.GetBytes(strSend).Length+1;//注意,这里的长度是按字节来算的
SendMessage(WINDOW_HANDLE, WM_COPYDATA, 0, ref cdata);
}
else
{
return false;
}
return true;
}
private void button_Click(object sender, RoutedEventArgs e)
{
String strSend = "C#发送的信息";
int nMessageId = 100;
if (CshapSendMessage(nMessageId,strSend))
{
MessageBox.Show("发送消息成功");
}
else
{
MessageBox.Show("消息发送失败,请打开VcMessage程序");
}
}
//接收消息。
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_COPYDATA)
{
COPYDATASTRUCT cdata = new COPYDATASTRUCT();
Type mytype = cdata.GetType();
cdata = (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, mytype);
switch (cdata.dwData.ToInt32())
{
case 1:
{
string strRecv = cdata.lpData;
break;
}
default:
break;
}
}
return IntPtr.Zero;
}
}
}
## C++端: ##
BOOL CVcMessageDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//接收C#发送来的数据
switch (pCopyDataStruct->dwData)
{
case 100:
{
CStringA strRecv = (char*)pCopyDataStruct->lpData;
break;
}
default:
{
break;
}
}
return CDialogEx::OnCopyData(pWnd, pCopyDataStruct);
}
void CVcMessageDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CStringA strSend = "VC发送的数据";
if (VCSendMessage(1,strSend))
{
AfxMessageBox(_T("消息发送成功"));
}
else
{
AfxMessageBox(_T("消息发送失败"));
}
}
//************************************
// Method: VCSendMessage
// FullName: CVcMessageDlg::VCSendMessage
// Access: public
// Returns: BOOL
// Qualifier: 向C#程序 CshapMessage发送消息
// Parameter: int nMessgeId
// Parameter: CStringA strSend
//************************************
BOOL CVcMessageDlg::VCSendMessage(int nMessgeId,CStringA strSend)
{
HWND hSendWindow = this->m_hWnd;
if (hSendWindow == NULL)
{
return FALSE;
}
CWnd *phwnd = FindWindow(NULL, _T("CshapMessage"));
HWND hRecvWindow = NULL;
if (phwnd == NULL)
{
return FALSE;
}
hRecvWindow = phwnd->GetSafeHwnd();
if (hRecvWindow == NULL)
{
return FALSE;
}
COPYDATASTRUCT CopyData;
CopyData.dwData = nMessgeId;
CopyData.cbData = strSend.GetLength()+1;
CopyData.lpData = (PVOID)strSend.GetBuffer(CopyData.cbData);
::SendMessage(hRecvWindow, WM_COPYDATA, (WPARAM)hSendWindow, (LPARAM)&CopyData);
return TRUE;
}
最新文章
- asp.net core获取HttpContext相关操作
- BZOJ3073 : [Pa2011]Journeys
- 避免写出IE Bug
- yii2 增加新的目录结构
- UBIFS FAQ and HOWTO
- 根据DateTime来获取当天是周几(已完结)
- Codeforces Round #206 (Div. 1)B(记忆化)
- curl 使用简介
- Unity 3D 文件导入出错误解决方法以及unity圣典离线版下载地址
- 初学c++
- PowerShell文件系统(一)前言
- 在cmd中运行android.bat报出空指针异常
- c# 岛2 小辅助~~~ 钓鱼 连击
- 201621123050 《Java程序设计》第6周学习总结
- XML自学笔记
- MySql的主从复制以及读写分离详解
- babel-plugin-import配置babel按需引入antd模块,编译后报错.bezierEasingMixin()
- solidity fallback函数
- MongoDB数据库中查询数据(下)
- BZOJ4008 : [HNOI2015]亚瑟王(期望dp)