简体   繁体   中英

Convert from BSTR to char*

I have seen:

  1. Convert BSTR to CHAR* and CHAR* to BSTR in C
  2. Problems converting BSTR to char *
  3. Convert BSTR to char*

using these question I am attempting to convert a BSTR to a char* via

#include "comutil.h"
STDMETHODIMP CServer::Initialise(BSTR strCmdFilePath, 
   VARIANT_BOOL bDiagErr, VARIANT_BOOL bProcErr, BSTR* RESULT)
{
   char *p = _com_util::ConvertBSTRToString(strCmdFilePath);
   ...
}

but I am getting:

Error 1 error LNK2019: unresolved external symbol "char * __stdcall _com_util::ConvertBSTRToString(wchar_t *)" (?ConvertBSTRToString@_com_util@@YGPADPA_W@Z) referenced in function "public: virtual long __stdcall CServer::Initialise(wchar_t *,short,short,wchar_t * *)" (?Initialise@CServer@@UAGJPA_WFFPAPA_W@Z)

Why am I getting this error?

Your project is not linking the required library. Which is comsuppw.lib for the Release build, comsuppwd.lib for the Debug build. Do note that you can always see the required library in the MSDN article . It is annotated at the bottom of the article. "Header" tells you what you need to #include, "Lib" tells you what you need to link.

There's an easier way for this library, the best way to get the linker instruction embedded so it is automatic is by #including the .h file that contains the #pragma comment. Fix:

#include <comdef.h>     // Added
#include <comutil.h>

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