通过 MS RPC 发送 wstring 和 ptime

发布于 2024-12-10 08:30:05 字数 202 浏览 0 评论 0 原文

我正在使用 Microsoft RPC,我需要传输具有 std::wstring 和 boost::ptime 类型字段的自定义结构。在idl中没有这样的数据类型。发送该结构的最佳解决方案是什么?阅读有关使用 RPC 进行序列化的内容。但是ms序列化也是基于idl文件的,所以我无法使用wstring和ptime在idl文件中定义结构体。

Iam using Microsoft RPC and i need to transfer my custom structure that have fields of type std::wstring and boost::ptime. In idl there is no such data types. What is best solution to send that struct. In read about serialization with RPC. But ms serialization is also based on idl file, so i cant define struct in idl file with wstring and ptime.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬花如无物 2024-12-17 08:30:05

IDL 具有一组有限的基本类型,并且它无法传输完整的 C++ 对象,因为接收器可能根本不是用 C++ 编写的。因此,您必须进行一些转换,但是使用您提到的类型进行转换并不是很复杂。

从 wstring 开始,您可以选择以下选项:

  1. 将 ac 字符串作为 [in, string] wchar_t* 传递。 wchar_t* 是调用 std::wstring.c_str() 时得到的,因此您可以轻松调用接口,无需进一步转换。
  2. 将 ac 字符串 作为数组传递 个字符。没有真正的理由这样做,只是说这是可能的。
  3. 将 ac 字符串作为 BSTR 传递。现在,BSTR 不再是基本 IDL 的一部分,而是 OLE 自动化扩展,在 COM 中广泛使用。使用它可能需要其他配置BSTR 基本上是一个 wchar_t*,但其大小位于缓冲区的开头。您可以使用AllocSysString 创建BSTR 并使用SysFreeString 释放它。或者,您可以使用 ATL 的 CComBSTR_bstr_t 类来管理 BSTRing。两者都在其构造函数中接受 wchar_t*,因此转换 wstring 不会出现问题。

现在,对于 ptime,我不太熟悉该类型,因此可能还有其他选项,但我能够找到这两个:

  1. ptime 转换为 int64,然后使用 IDL __int64 类型来传递它的价值。
  2. 使用 to_iso_stringptime 转换为字符串,并按照上面的建议传递(请注意,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:

  1. Pass a c string as [in, string] wchar_t*. wchar_t* is what you get when calling std::wstring.c_str(), so you can easily call the interface without further conversions.
  2. Pass a c string as an array of chars. No real reason to do that, just saying it's possible.
  3. Pass a c string as a BSTR. Now, 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 a wchar_t*, but with its size at the beginning of the buffer. You can create BSTR using AllocSysString and free it using SysFreeString. Or, you can use ATL's CComBSTR or the _bstr_t class to manage the BSTRings. Both accept wchar_t* in their constructor, so converting the wstring 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:

  1. Convert the ptime to an int64, and then use the IDL's __int64 type to pass its value.
  2. Use to_iso_string to convert the ptime to a string, and pass as suggested above (note that to_iso_string gets you a regular std::string and not a std::wstring). On the other side, use from_iso_string to get the ptime back.
怀念你的温柔 2024-12-17 08:30:05

您还可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文