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
.

- 2,148
- 5
- 30
- 53
-
Turn wstring into a BSTR I guess. Not sure about ptime but I imagine it is an integer of some sort behind the scenes. – David Heffernan Oct 16 '11 at 15:53
2 Answers
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:
- Pass a c string as
[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. - Pass a c string as an array of chars. No real reason to do that, just saying it's possible.
- 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 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:
- Convert the
ptime
to an int64, and then use the IDL's __int64 type to pass its value. - Use
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.
-
What do you think about serialize with boost::serialization and send as array of bytes? – userbb Oct 16 '11 at 21:00
-
@userbb, I don't know enough about boost::serialization, but sending an array of bytes is definitively a reasonable possibility (and not a hard one to implement). If all you're dealing with are those two arguments, serialization might add an overhead, and make the passed content harder to debug (being an array of bytes), but that might not be a real problem for you. – Eran Oct 16 '11 at 21:51
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.
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.

- 1,877
- 19
- 24