通过 MS RPC 发送 wstring 和 ptime
我正在使用 Microsoft RPC,我需要传输具有 std::wstring 和 boost::ptime 类型字段的自定义结构。在idl
中没有这样的数据类型。发送该结构的最佳解决方案是什么?阅读有关使用 RPC
进行序列化的内容。但是ms序列化也是基于idl文件的,所以我无法使用wstring和ptime在idl文件中定义结构体。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IDL 具有一组有限的基本类型,并且它无法传输完整的 C++ 对象,因为接收器可能根本不是用 C++ 编写的。因此,您必须进行一些转换,但是使用您提到的类型进行转换并不是很复杂。
从 wstring 开始,您可以选择以下选项:
[in, string] wchar_t*
传递。wchar_t*
是调用std::wstring.c_str()
时得到的,因此您可以轻松调用接口,无需进一步转换。BSTR
不再是基本 IDL 的一部分,而是 OLE 自动化扩展,在 COM 中广泛使用。使用它可能需要其他配置。BSTR
基本上是一个wchar_t*
,但其大小位于缓冲区的开头。您可以使用AllocSysString
创建BSTR
并使用SysFreeString
释放它。或者,您可以使用 ATL 的 CComBSTR 或 _bstr_t 类来管理 BSTRing。两者都在其构造函数中接受wchar_t*
,因此转换wstring
不会出现问题。现在,对于
ptime
,我不太熟悉该类型,因此可能还有其他选项,但我能够找到这两个:ptime
转换为 int64,然后使用 IDL __int64 类型来传递它的价值。to_iso_string
将ptime
转换为字符串,并按照上面的建议传递(请注意,to_iso_string
会为您提供常规的std:: string
而不是std::wstring
)。另一方面,使用from_iso_string
获取ptime
。The IDL has a limited set of basic types, and it can't transfer complete c++ objects, as the receiver might not be written in c++ at all. So, you'll have to do some conversions, but doing so with the types you mention is not very complicated.
Starting with the wstring, here are your options:
[in, string] wchar_t*
.wchar_t*
is what you get when callingstd::wstring.c_str()
, so you can easily call the interface without further conversions.BSTR
is not a part of the basic IDL, but an OLE automation extension, widely used in COM. Using it might require additional configuration.BSTR
is basically awchar_t*
, but with its size at the beginning of the buffer. You can createBSTR
usingAllocSysString
and free it usingSysFreeString
. Or, you can use ATL's CComBSTR or the _bstr_t class to manage the BSTRings. Both acceptwchar_t*
in their constructor, so converting thewstring
won't be a problem.Now, as for the
ptime
, I'm not really familiar with that type so there might be other options, but I was able to find these two:ptime
to an int64, and then use the IDL's __int64 type to pass its value.to_iso_string
to convert theptime
to a string, and pass as suggested above (note thatto_iso_string
gets you a regularstd::string
and not astd::wstring
). On the other side, usefrom_iso_string
to get theptime
back.您还可以使用 VARIANT 类型,它为您提供了关于传递数据类型的大量选项。在您的情况下,它将是 VT_BSTR 类型的 VARIANT VT_DATE。
这对我个人来说效果很好,因为我可以传递 SAFEARRAY,我可以用它来传递 STL 类型,如 std::map。
封送 OLE 数据类型:
关于上述 MSDN 站点的注意事项:在 IDL 中添加 VARIANT 类型时,上面的链接提到导入“objidl.idl”。这仍然给了我一个编译错误,相反导入“oaidl.idl”对我有用。
You can also use VARIANT types, which gives you a heap of options in terms of what type of data is passed. In your case, it would be VARIANTs of type VT_BSTR & VT_DATE.
This personally worked well for me because I could then pass SAFEARRAYs, which I could use for passing STL types like std::map.
Marshaling OLE Data Types:
Note about above MSDN site: When adding VARIANT types in your IDL, the above link mentions importing "objidl.idl". That still gave me a compile error, and instead importing "oaidl.idl" worked for me.