我有两个 Windows 应用程序。第一个是 Windows 窗体应用程序。第二个是控制台应用程序。我必须从控制台应用程序向表单应用程序发送一条消息。我注册了广播消息并使用 ::PostMessage 函数能够将数字消息发送到表单应用程序。但我无法发送字符串消息。请在下面找到我的代码:-
// Sending Message (Console Application)
#define RT_UI_MSG (WM_USER+1)
UINT msg_id = RegisterWindowMessage("Test-UI");
LPTSTR lpszString = "Hello";
COPYDATASTRUCT cds;
cds.dwData = 1; // can be anything
cds.cbData = sizeof(TCHAR) * (_tcslen(lpszString) + 1);
cds.lpData = &lpszString;
::PostMessage(HWND_BROADCAST, msg_id, WM_COPYDATA, (LPARAM)(LPVOID)&cds);
// Receiving Message (Forms Application)
BEGIN_MESSAGE_MAP(CRTUIDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, &CRTUIDlg::OnBnClickedOk)
ON_REGISTERED_MESSAGE(temp, OnRTUIMsg)
ON_BN_CLICKED(IDC_BUTTON1, &CRTUIDlg::OnBnClickedButton1)
END_MESSAGE_MAP()
UINT temp = RegisterWindowMessage(L"Test-UI");
LRESULT CRTUIDlg::OnRTUIMsg(WPARAM wParam, LPARAM lParam)
{
COPYDATASTRUCT* pcds = (COPYDATASTRUCT*)lParam;
if (pcds->dwData == 1)
{
LPCTSTR lpszString = (LPCTSTR)(pcds->lpData);
// do something with lpszString...
}
return 0;
}
请帮助我了解我做错了什么并使其正常工作