我见过:
- Convert BSTR to CHAR* and CHAR* to BSTR in C
- Problems converting BSTR to char *
- Convert BSTR to char*
使用这些问题,我试图通过
将BSTR
转换为 char*
#include "comutil.h"
STDMETHODIMP CServer::Initialise(BSTR strCmdFilePath,
VARIANT_BOOL bDiagErr, VARIANT_BOOL bProcErr, BSTR* RESULT)
{
char *p = _com_util::ConvertBSTRToString(strCmdFilePath);
...
}
但我得到:
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)
为什么会出现此错误?
最佳答案
您的项目没有链接所需的库。对于 Release 版本是 comsuppw.lib
,对于 Debug 版本是 comsuppwd.lib
。请注意,您始终可以在 the MSDN article 中看到所需的库.文章底部有注释。 “Header”告诉您需要#include 的内容,“Lib”告诉您需要链接的内容。
这个库有一个更简单的方法,嵌入链接器指令以使其自动执行的最佳方法是通过#including 包含#pragma 注释的 .h 文件。修复:
#include <comdef.h> // Added
#include <comutil.h>
关于c++ - 从 BSTR 转换为 char*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22986315/