我可以使用 WM_COPYDATA 复制非结构吗?
可以说我在 foobar-shared.lib 中有这个类:
class FooBar {
std::string m_helloWorld;
}
并且我使用 SendCopyData 在 foobar-from.exe 中进行了调用,如下所示:
extern HWND hMainWnd; // foobar-from.exe
{
FooBar fooBar;
HWND hWnd = FindAppWindow(); // foobar-to.exe
COPYDATASTRUCT cds;
cds.dwData = ('f'|('o'<<8)|('o'<<16));
cds.cbData = sizeof(FooBar);
cds.lpData = (LPVOID)fooBar;
SendCopyData(hWnd, (WPARAM)hMainWnd, (LPARAM)&cds);
}
当从 foobar-to.exe 时,我处理 OnCopyData:
BOOL CMainFrame::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct) {
if (pCopyDataStruct->dwData==('f'|('o'<<8)|('o'<<16))) {
FooBar fooBar = *(FooBar *)pCopyDataStruct->lpData;
}
}
当 FooBar 是一个结构时,这工作得很好,但现在它是一个类,我收到此错误:
First-chance exception at 0x0064ef81 in foobar-to.exe: 0xC0000005:
Access violation reading location 0x0231dd7c.
我最初认为这是因为我的 fooBar 实例位于堆栈上,所以我尝试将其移动到堆中,但得到了一个略有不同的错误(我如果需要的话可以在这里发布结果)。
根据 MSDN,“正在传递的数据不得包含接收数据的应用程序无法访问的对象的指针或其他引用。”所以我怀疑这只能用结构数据来实现。我说得对吗?
Lets say I have this class in foobar-shared.lib:
class FooBar {
std::string m_helloWorld;
}
And I have a call in foobar-from.exe using SendCopyData like so:
extern HWND hMainWnd; // foobar-from.exe
{
FooBar fooBar;
HWND hWnd = FindAppWindow(); // foobar-to.exe
COPYDATASTRUCT cds;
cds.dwData = ('f'|('o'<<8)|('o'<<16));
cds.cbData = sizeof(FooBar);
cds.lpData = (LPVOID)fooBar;
SendCopyData(hWnd, (WPARAM)hMainWnd, (LPARAM)&cds);
}
When from a foobar-to.exe, I handle OnCopyData:
BOOL CMainFrame::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct) {
if (pCopyDataStruct->dwData==('f'|('o'<<8)|('o'<<16))) {
FooBar fooBar = *(FooBar *)pCopyDataStruct->lpData;
}
}
This worked fine when FooBar was a struct, but now that it's a class I get this error:
First-chance exception at 0x0064ef81 in foobar-to.exe: 0xC0000005:
Access violation reading location 0x0231dd7c.
I assumed originally that this was because my fooBar
instance is on the stack, so I tried moving it to the heap but got a slightly different error (I can post the result here if necessary).
According to MSDN, "The data being passed must not contain pointers or other references to objects not accessible to the application receiving the data." so I suspect that this only possible with struct data. Am I correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你既正确又不正确。
你的问题是你不知道 std::string 的实现细节。不幸的是,这个(标准)类似乎使用动态分配的缓冲区来存储其字符数据。这就是为什么
WM_COPYDATA
不适用于它。但是,如果您的类不包含指向任何外部数据的指针(如文档中所建议的那样),那么使用
WM_COPYDATA
复制它是完全有效的。不幸的是,这极大地限制了班级成员的可能类型。(认为
WM_COPYDATA
就像通过网络发送数据:您应该在将类发送出去之前先对其进行序列化......)you are both correct and incorrect.
your problem here is that you don't know the implementation details of std::string. unfortunately, it seems this (standard) class uses a dynamicaly allocated buffer to store its character data. that's why
WM_COPYDATA
doesn't work with it.but if your class does not contain a pointer to any external data, as suggested in the documentation, then it would be perfectly valid to copy it using
WM_COPYDATA
. unfortunately, this greatly limits the possible types for members of your class.(think
WM_COPYDATA
is like sending data over a network: you should take care of serializing your class before sending it out in the wild...)