Jeff
Jeff

Reputation: 63

How to fix this error(convert wchar_t to BSTR)?

I am new to C++, and I am trying to compile this code, and get error as below, could anyone give me a guide to fix it? thanks a lot. i searched a lot on google, still cannot solve it

Error   C2664   'HRESULT IRegistrationInfo::put_Author(BSTR)': cannot convert argument 1 from 'const wchar_t [12]' to 'BSTR'    ConsoleApplication7 c:\users\hellojeff\source\repos\consoleapplication7\consoleapplication7\consoleapplication7.cpp 135 1   
Warning C4603   '_WIN32_DCOM': macro is not defined or definition is different after precompiled header use ConsoleApplication7 c:\users\hellojeff\source\repos\consoleapplication7\consoleapplication7\consoleapplication7.cpp 5   1   
Error (active)  E0167   argument of type "const wchar_t *" is incompatible with parameter of type "BSTR"    ConsoleApplication7 C:\Users\HelloJeff\source\repos\ConsoleApplication7\ConsoleApplication7\ConsoleApplication7.cpp 135 28

seems that const wchar_ cannot be converted to 'BSTR' for this line,

hr = pRegInfo->put_Author(L"Author Name");

the full code is at https://learn.microsoft.com/en-us/windows/win32/taskschd/logon-trigger-example--c---,

Upvotes: 4

Views: 2031

Answers (1)

M.M
M.M

Reputation: 141574

You can do:

hr = pRegInfo->put_Author(_bstr_t(L"Author Name"));

A BSTR is a different sort of string to a wide string literal. The _bstr_t class is a wrapper that , in this case, makes a temporary BSTR out of the literal in order to pass to the function.

See this article for more information

Upvotes: 3

Related Questions