发送自定义Windows消息...自定义数据编组 [英] Sending a Custom windows message...custom data marshalling

查看:91
本文介绍了发送自定义Windows消息...自定义数据编组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,需要与另一个应用程序交互.

I have a WPF application that needs to interface with another application.

此应用程序具有大约20个自定义Windows消息(WM_USER + 50 ... WM_USER + 70).

This application has about 20 custom Windows Messages (WM_USER+50...WM_USER+70).

我要完成的工作摘要:

WPF应用程序-> SendMessage->第三方应用程序

WPF Application -> SendMessage -> ThirdParty application

我的问题是所有消息都是自定义消息.因此,我必须实现自己的数据封送处理.

The problem I have is that all of the Messages are CUSTOM messages. Therefore I have to implement my own data marshaling.

请参见 http://msdn.microsoft.com/zh-CN/library/windows/desktop/ms644950(v=vs.85).aspx

看来我需要经历的过程是:

It seems that the process I need to go through is:

  1. 抓取该进程并将其打开以供所有访问. User32.GetWindowThreadProcessId(windowHandle,out pId);

  1. Grab the process and open it for all access. User32.GetWindowThreadProcessId(windowHandle, out pId);

//以所有访问权限打开进程
someprocess = OpenProcess((0x1F0FFF),false,(int)pId);

// Open the process with all access
someprocess = OpenProcess((0x1F0FFF), false, (int)pId);

在进程中分配一个缓冲区: IntPtr缓冲区= VirtualAllocEx(hProcess,IntPtr.Zero,1024,0x1000,0x04);

Allocate a buffer in the process: IntPtr buffer = VirtualAllocEx( hProcess, IntPtr.Zero, 1024, 0x1000, 0x04 );

填充某种要写入#2中创建的缓冲区的结构吗?

Fill up some sort of struct that will be written to the buffer created in #2?

将#3复制到远程缓冲区是#2? WriteProcessMemory ??

Copy #3 to the remote buffer is #2? WriteProcessMemory??

发送自定义消息(SendMessage(windowhandle,customMsg,0,#2的缓冲区?)

Send the custom message ( SendMessage(windowhandle, customMsg, 0, buffer from #2?)

将结构从远程进程缓冲区读回本地缓冲区

Read the struct back in from the remote process buffer into a local buffer

将此数据编组为托管类型. (这是一个C#.Net应用程序)

Marshal this data to a managed type. (This is a C# .Net application)

我真的可以利用一些见解.到目前为止,我还没有很多运气.我认为最困扰的部分是要发送到WriteProcessMemory的结构类型是什么?

I could really use some insight. I haven't had much luck thus far. I think the part that I'm most stuck on is what type of struct to send to the WriteProcessMemory?

推荐答案

WM_COPYDATA绝对是最简单的方法. WM_COPYDATA使您可以将两个不同的数据项发送到另一个进程-DWORD值和任意大小的数据块.因此,对于您的实现,您可能会执行以下操作:

WM_COPYDATA is definitely the easiest way to do this. WM_COPYDATA lets you send two distinct items of data to another process - a DWORD value, and an arbitrarily-sized chunk of data. So for your implementation you would probably do something like this:

COPYDATASTRUCT cds;
cds.dwData = WM_USER + 50; // the "message" you want to send
cds.cbData = sizeof(MyDataForMessage50); // the size of the chunk of data
cds.lpData = lpMessage50Data; // a pointer to the chunk of data
SendMessage(hwndTarget, WM_COPYDATA, reinterpret_cast<WPARAM>(hwndSender),
            reinterpret_cast<LPARAM>(&cds));

请注意,hwndTarget是另一个过程中的目标窗口,而hwndSender发送过程中的窗口.目标窗口接收相同的参数,因此可以使用wParam了解谁发送了消息,因此可以在需要时发送答复.

Note that hwndTarget is the target window in the other process, and hwndSender is a window in the sending process. The target window receives the same parameters and so can use wParam to learn who sent the message, and can therefore send a reply if needed.

在接收端的WndProc中:

In the WndProc at the receiving end:

if (uMsg == WM_COPYDATA)
{
    HWND hwndSender = reinterpret_cast<HWND>(wParam);
    LPCOPYDATASTRUCT pcds = reinterpret_cast<LPCOPYDATASTRUCT>(lParam);
    DWORD dwCustomMsg = pcds->dwData;
    LPVOID pCustomData = pcds->lpData;
    DWORD dwCustomDataSize = pcds->cbData;

    // do something with the custom message

    // return TRUE to indicate message received
    return TRUE;
}

还请注意文档中针对WM_COPYDATA的重要说明:

Also note the important note in the docs for WM_COPYDATA:

接收应用程序应将数据视为只读数据.这 lParam参数仅在处理消息期间有效. 接收方应用程序不应释放由以下对象引用的内存 帕拉姆如果接收方的应用程序必须在之后访问数据 SendMessage返回,必须将数据复制到本地缓冲区

The receiving application should consider the data read-only. The lParam parameter is valid only during the processing of the message. The receiving application should not free the memory referenced by lParam. If the receiving application must access the data after SendMessage returns, it must copy the data into a local buffer

这篇关于发送自定义Windows消息...自定义数据编组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆