我尝试使用 sendmessage 将消息从我的 c++ 应用程序传递到 c#
我的 C++ 代码是这样的
int _tmain(int argc, _TCHAR* argv[])
{
COPYDATASTRUCT cpd;
cpd.dwData = 0;
LPCWSTR strDataToSend = L"http://google.com";;
cpd.cbData = (wcslen(strDataToSend) + 1) * 2;
cpd.lpData = (PVOID)strDataToSend;
SendMessage((HWND)0x0020073C,5555,0,(LPARAM)&cpd);
return 0;
}
我尝试使用 SendMessageW 我也硬编码了正确的句柄我正在回拨但没有得到任何数据
我的 C# 应用程序是这样的
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
protected override void WndProc(ref Message m)
{
// Listen for operating system messages.
switch (m.Msg)
{
// The WM_ACTIVATEAPP message occurs when the application
// becomes the active application or becomes inactive.
case 5555:
COPYDATASTRUCT mystr = new COPYDATASTRUCT();
Type mytype = mystr.GetType();
mystr = (COPYDATASTRUCT)m.GetLParam(mytype);
MessageBox.Show ( mystr.cbData.ToString());
MessageBox.Show(mystr.lpData);
break;
}
base.WndProc(ref m);
}
我将消息框变为空白......我尝试不使用 COPYDATASTRUCT 只有我传递的字符串请任何人帮助我.. 提前致谢
int _tmain(int argc, _TCHAR* argv[])
{
COPYDATASTRUCT cpd;
cpd.dwData = 0;
LPCWSTR strDataToSend = L"http://google.com";;
cpd.cbData = (wcslen(strDataToSend) + 1) * 2;
cpd.lpData = (PVOID)strDataToSend;
SendMessage((HWND)0x0020073C,WM_COPYDATA,0,(LPARAM)&cpd);
return 0;
}
C# 代码
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
public const int WM_COPYDATA = 0x4A;
protected override void WndProc(ref Message m)
{
// Listen for operating system messages.
switch (m.Msg)
{
// The WM_ACTIVATEAPP message occurs when the application
// becomes the active application or becomes inactive.
case WM_COPYDATA:
COPYDATASTRUCT mystr = new COPYDATASTRUCT();
Type mytype = mystr.GetType();
mystr = (COPYDATASTRUCT)m.GetLParam(mytype);
MessageBox.Show ( mystr.cbData.ToString());
MessageBox.Show(mystr.lpData);
break;
}
base.WndProc(ref m);
}