C++ 使用 WM_COPYDATA 在进程间发送数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2451103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:31:58  来源:igfitidea点击:

Use WM_COPYDATA to send data between processes

c++windowsipcwm-copydata

提问by Charles Gargent

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_COPYDATAcase is executed, but again I dont know what to do with the COPYDATASTRUCT.

我不知道如何构建COPYDATASTRUCT,我刚刚放入了一些似乎有效的东西。调试时WM_COPYDATA执行案例,但我再次不知道如何处理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.

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

回答by Tadmas

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.

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

The dwDatamember 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.

dwData成员由您定义。把它想象成一个你可以定义的数据类型枚举。它是您想要用来识别数据是某某字符串的任何内容。

The cbDatamember 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.

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

The lpDatamember points to the data you want to copy.

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

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...
}

回答by Dila Gurung

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;
}

回答by Un Peu

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

这不是真正的答案,而是调试SendMessage(WM_COPYDATA...

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

好吧,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. 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.
  1. 测试它是否在目标进程(窗口)上工作 [ctrl+f,Windows]。
  2. WM_COPYDATA上设置第二个消息过滤器。... 和
  3. “查看\始终在最前面”也非常方便。

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

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