Ryo
Ryo

Reputation: 1035

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?

Upvotes: 0

Views: 1012

Answers (2)

selbie
selbie

Reputation: 104589

Assuming you are compiling with UNICODE as the default.

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

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

Upvotes: 1

Gardener
Gardener

Reputation: 2660

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:

Upvotes: 1

Related Questions