使用WM_COPYDATA在进程之间发送数据

发布于 2024-08-24 21:04:14 字数 565 浏览 4 评论 0原文

我希望在进程之间发送文本。我发现了很多这样的例子,但没有一个我可以工作。这是我到目前为止所拥有的:

对于发送部分:

COPYDATASTRUCT CDS;
CDS.dwData = 1;
CDS.cbData = 8;
CDS.lpData = NULL;
SendMessage(hwnd, WM_COPYDATA , (WPARAM)hwnd, (LPARAM) (LPVOID) &CDS);

接收部分:

case WM_COPYDATA:
COPYDATASTRUCT* cds = (COPYDATASTRUCT*) lParam;

我不知道如何构造 COPYDATASTRUCT ,我刚刚放入了一些似乎有效的东西。调试时执行 WM_COPYDATA 情况,但我再次不知道如何处理 COPYDATASTRUCT

我想在两个进程之间发送文本。

你可能会说我才刚刚开始,我在 Code::Blocks 中使用 GNU GCC 编译器,我试图避免 MFC 和依赖项。

I wish to send text between processes. I have found lots of examples of this but none that I can get working. Here is what I have so far:

for the sending part:

COPYDATASTRUCT CDS;
CDS.dwData = 1;
CDS.cbData = 8;
CDS.lpData = NULL;
SendMessage(hwnd, WM_COPYDATA , (WPARAM)hwnd, (LPARAM) (LPVOID) &CDS);

the receiving part:

case WM_COPYDATA:
COPYDATASTRUCT* cds = (COPYDATASTRUCT*) lParam;

I dont know how to construct the COPYDATASTRUCT, I have just put something in that seems to work. When debugging the WM_COPYDATA case is executed, but again I dont know what to do with the COPYDATASTRUCT.

I would like to send text between the two processes.

As you can probably tell I am just starting out, I am using GNU GCC Compiler in Code::Blocks, I am trying to avoid MFC and dependencies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゃ懵逼小萝莉 2024-08-31 21:04:14

有关如何使用该消息的示例,请参阅 http:// /msdn.microsoft.com/en-us/library/ms649009(VS.85).aspx。您可能还想查看http://www.flounder.com/wm_copydata.htm

dwData 成员由您定义。将其视为您可以定义的数据类型枚举。它是您想要用来识别数据是这样那样的字符串的任何东西。

cbData 成员是 lpData 指向的数据的大小(以字节为单位)。在您的情况下,它将是字符串的大小(以字节为单位)。

lpData 成员指向要复制的数据。

因此,要传输单个字符串......

LPCTSTR lpszString = ...;
COPYDATASTRUCT cds;
cds.dwData = 1; // can be anything
cds.cbData = sizeof(TCHAR) * (_tcslen(lpszString) + 1);
cds.lpData = lpszString;
SendMessage(hwnd, WM_COPYDATA, (WPARAM)hwnd, (LPARAM)(LPVOID)&cds);

然后,接收它......

COPYDATASTRUCT* pcds = (COPYDATASTRUCT*)lParam;
if (pcds->dwData == 1)
{
    LPCTSTR lpszString = (LPCTSTR)(pcds->lpData);
    // do something with lpszString...
}

For an example of how to use the message, see http://msdn.microsoft.com/en-us/library/ms649009(VS.85).aspx. You may also want to look at http://www.flounder.com/wm_copydata.htm.

The dwData member is defined by you. Think of it like a data type enum that you get to define. It is whatever you want to use to identify that the data is a such-and-such string.

The cbData member is the size in bytes of the data pointed to by lpData. In your case, it will be the size of the string in bytes.

The lpData member points to the data you want to copy.

So, to transfer a single string....

LPCTSTR lpszString = ...;
COPYDATASTRUCT cds;
cds.dwData = 1; // can be anything
cds.cbData = sizeof(TCHAR) * (_tcslen(lpszString) + 1);
cds.lpData = lpszString;
SendMessage(hwnd, WM_COPYDATA, (WPARAM)hwnd, (LPARAM)(LPVOID)&cds);

Then, to receive it....

COPYDATASTRUCT* pcds = (COPYDATASTRUCT*)lParam;
if (pcds->dwData == 1)
{
    LPCTSTR lpszString = (LPCTSTR)(pcds->lpData);
    // do something with lpszString...
}
Saygoodbye 2024-08-31 21:04:14
Use the following code.

//Message Sender Class( for the demonstration purpose put the following code in //button click event)
    CString strWindowTitle= _T("InterProcessCommunicationExample");
    CString dataToSend =_T("Originate from Windows");

    LRESULT copyDataResult;
    CWnd *pOtherWnd=CWnd::FindWindowW(NULL, strWindowTitle);

    if(pOtherWnd)
    {
        COPYDATASTRUCT cpd;
        cpd.dwData=0;
        cpd.cbData=dataToSend.GetLength();
        //cpd.cbData=_tcslen(dataToSend)+1;
        cpd.lpData=(void*)dataToSend.GetBuffer(cpd.cbData);
        AfxMessageBox((LPCTSTR)cpd.lpData);
        //cpd.lpData=(void*)((LPCTSTR)cpd.cbData);
        copyDataResult=pOtherWnd->SendMessage(WM_COPYDATA,(WPARAM)AfxGetApp()->m_pMainWnd->GetSafeHwnd(),(LPARAM) &cpd);

        dataToSend.ReleaseBuffer();


    }
    else
    {
        AfxMessageBox(L"Hwllo World");

    }


//Message Receiver Process
BOOL CMessageReceiverClass::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct) 
{
    CString copiedData=(LPCTSTR)(pCopyDataStruct->lpData);
    AfxMessageBox((LPCTSTR)(pCopyDataStruct->lpData));
//  return CDialog::OnCopyData(pWnd, pCopyDataStruct);
    return TRUE;
}
Use the following code.

//Message Sender Class( for the demonstration purpose put the following code in //button click event)
    CString strWindowTitle= _T("InterProcessCommunicationExample");
    CString dataToSend =_T("Originate from Windows");

    LRESULT copyDataResult;
    CWnd *pOtherWnd=CWnd::FindWindowW(NULL, strWindowTitle);

    if(pOtherWnd)
    {
        COPYDATASTRUCT cpd;
        cpd.dwData=0;
        cpd.cbData=dataToSend.GetLength();
        //cpd.cbData=_tcslen(dataToSend)+1;
        cpd.lpData=(void*)dataToSend.GetBuffer(cpd.cbData);
        AfxMessageBox((LPCTSTR)cpd.lpData);
        //cpd.lpData=(void*)((LPCTSTR)cpd.cbData);
        copyDataResult=pOtherWnd->SendMessage(WM_COPYDATA,(WPARAM)AfxGetApp()->m_pMainWnd->GetSafeHwnd(),(LPARAM) &cpd);

        dataToSend.ReleaseBuffer();


    }
    else
    {
        AfxMessageBox(L"Hwllo World");

    }


//Message Receiver Process
BOOL CMessageReceiverClass::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct) 
{
    CString copiedData=(LPCTSTR)(pCopyDataStruct->lpData);
    AfxMessageBox((LPCTSTR)(pCopyDataStruct->lpData));
//  return CDialog::OnCopyData(pWnd, pCopyDataStruct);
    return TRUE;
}
止于盛夏 2024-08-31 21:04:14

这并不是真正的答案,而是调试时有用的提示 SendMessage(WM_COPYDATA...

好吧,Microsoft Spy++ 可能真的会派上用场。
您可以在这里找到它:

c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\spyxx_amd64.exe
c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\spyxx.exe
  1. 测试它是否正在目标进程(窗口)[ctrl+f,Windows] 上运行。
  2. 第二次在WM_COPYDATA上设置消息过滤器。
    ...并且
  3. “查看\始终位于顶部”也非常方便。

快乐的 C++ 学习 - 特别是在 C# 中,API 可以是真正的“乐趣”。 ;)

That's not really an answer but useful hint when debugging SendMessage(WM_COPYDATA...

Well Microsoft Spy++ might really come in handy.
You may find it here:

c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\spyxx_amd64.exe
c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\spyxx.exe
  1. Test that it's working on the target process(window) [ctrl+f,Windows].
  2. Second set message filter on WM_COPYDATA.
    ... and
  3. 'View\Always on top' is also really handy.

Happy C++'ing - especially in C# that API can be real 'fun'. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文