如何通过 COPYDATASTRUCT 发送 VARIANT_bstr
我需要使用 COPYDATASTRUCT 将 VARIANT 值发送到另一个应用程序。这是我用来发送消息的结构。
struct {
int i_MsgId;
VARIANT variant_Value;
}Message;
在我的代码中,我将 VARIANT 初始化为 BSTR 类型并分配一个字符串,如下所示。
Message structMessage;
VariantInit(&structMessage.variant_Value);
structMessage.var_Value.vt = VT_BSTR;
structMessage.variant_Value.bstrVal = ::SysAllocString(L"I am a happy BSTR");
然后我使用 COPYDATASTRUCT 发送此内容,如下所示。
COPYDATASTRUCT structCDS;
structCDS.cbData = sizeof(structMessage);
structCDS.dwData = 12;
structCDS.lpData = (LPVOID)(&structMessage);
::SendMessage(this->m_RemoteWindow,WM_COPYDATA,(WPARAM)this->GetSafeHwnd(),(LPARAM)&structCDS);
此消息成功接收到我的第二个应用程序,但是,当我将其转换回原始结构时,“bstrVal”表示错误的指针。
我正在努力解决这个错误,因此期待您的宝贵帮助。请注意,其他 varinat 类型(int、double)可以成功转换回来,并且此错误仅在 bstr 中出现。 :(
谢谢
I need to send a VARIANT value to another application using COPYDATASTRUCT. Here is the structure i'm using to send messages.
struct {
int i_MsgId;
VARIANT variant_Value;
}Message;
In my code I initialize the VARIANT to type BSTR and allocate a string as follows.
Message structMessage;
VariantInit(&structMessage.variant_Value);
structMessage.var_Value.vt = VT_BSTR;
structMessage.variant_Value.bstrVal = ::SysAllocString(L"I am a happy BSTR");
Then I send this using COPYDATASTRUCT as follows.
COPYDATASTRUCT structCDS;
structCDS.cbData = sizeof(structMessage);
structCDS.dwData = 12;
structCDS.lpData = (LPVOID)(&structMessage);
::SendMessage(this->m_RemoteWindow,WM_COPYDATA,(WPARAM)this->GetSafeHwnd(),(LPARAM)&structCDS);
This message successfully receives to my second application, HOWEVER, when I cast it back to the original struct, "bstrVal" indicates a bad pointer.
I'm struggling with this error, so expecting your valuable help. Please note that other varinat types (int, double) can be successfully convert back, and this error ocus only with bstr. :(
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WM_COPYDATA 将与其他应用程序共享您的数据结构(lpData 引用的数据结构)。数据结构中包含的任何内容都可以被其他应用程序访问。但是,bstrVal 是一个引用应用程序中内存的指针,当其他应用程序尝试引用它时,它将失败。
这是一种解决方案;它确实需要双方额外的工作。
在发送方,当您分配数据结构 (structMessage) 时,请添加足够的额外空间来保存字符串。将字符串附加到数据结构的末尾(并确保将 dwData 增加适当的大小)。
在接收方,您可以检索字符串并在此时使用 SysAllocString 调用来分配 bstrVal。完成后不要忘记调用 SysFree。
WM_COPYDATA will share your data structure (the one referenced by lpData) with the other application. Anything that is contained inside the data structure will be accessible by the other app. However, bstrVal is a pointer that references memory in your application, and when the other app tries to reference it, it is going to fail.
Here's one solution; it does require extra work on both the sides.
On the sender side, when you allocate your data structure (structMessage), add enough extra space to hold your string. Append the string to the end of the data structure (and be sure to increase dwData by the appropriate size).
On the receiver side, you can retrieve the string and use the SysAllocString call at that point to assign the bstrVal. Don't forget to call SysFree when you're done.