Search code examples
c++winapirpcidl

Send wstring and ptime over MS RPC


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.


Solution

  • 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.