1

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?

Community
  • 1
  • 1
MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • Check this [answer](http://stackoverflow.com/questions/4302364/convert-bstr-to-const-char) – Mohit Jain Apr 10 '14 at 11:11
  • I have seen that answer too. This does not explain why I am getting the error above... – MoonKnight Apr 10 '14 at 11:13
  • It appears you haven't linked the com support library, and depending on the version of visual studio you're building with (which you should include in your question), you may be experiencing a bug in the pragma comments for pulling in the right lib. If so, it is correctable , but first: Out of curiosity, does the same problem happen for both debug *and* release builds ? – WhozCraig Apr 10 '14 at 11:14
  • Working VS2013 and yes this also occurs for the Release build as well as the Debug build. – MoonKnight Apr 10 '14 at 11:19
  • Ok, lastly, are you building with `wchar_t` as a native type? I ask because I ran into a similar problem. [This may help](http://msdn.microsoft.com/en-us/library/dh8che7s.aspx). – WhozCraig Apr 10 '14 at 11:20
  • ... or `comsuppwd.lib` for debug builds. @HansPassant yeah, I had a similar issue with VS2010 a couple years back. Was a little surprised it wasn't handled automatically by the pragmas. – WhozCraig Apr 10 '14 at 11:23
  • @HansPassant that was the answer. If you want to write that I will accept... – MoonKnight Apr 10 '14 at 11:24

1 Answers1

4

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>
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536