开发者

Send wstring and ptime over MS RPC

开发者 https://www.devze.com 2023-04-13 03:36 出处:网络
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

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 开发者_如何学JAVAread 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消