简体   繁体   中英

Convert CString to _bstr_t

I'm from Java so I confuse about many type of String in C++

I have a function input with:

functionTest(_bstr_t *params) {...}

Then I have a variable declared as:

CString paramsInput

How can I convert CString to _bstr_t to pass to the function?

CComBSTR has overloaded conversion functions.

CString paramsInput;
ATL::CComBSTR bstr = paramsInput;

/// Or, you can do it as ATL::CComBSTR bstr(paramsInput); functionTest(&bstr);

Please note that if the functionTest() test declares its param argument as out , then you need to watch out for a memory leak. See this for how to handle it:

Assuming you are compiling with UNICODE as the default.

#include <atlbase.h>
#include <atlcom.h>
#include <atlstr.h>

CComBSTR bstrParamsInput(paramsInput);
functionTest(&bstrParamsInput);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM