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 开发者_如何学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:
- 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.
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.
精彩评论