c++ - 从 BSTR 转换为 char*

标签 c++ com type-conversion

我见过:

  1. Convert BSTR to CHAR* and CHAR* to BSTR in C
  2. Problems converting BSTR to char *
  3. 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/

相关文章:

c++ - "Static polymorphism with Qt signal/slot: What is wrong?"

c++ - 使用 gcc/g++ 使 C++ 程序在没有窗口的情况下运行?

c# - 如何在 C# 程序中以 RAII 样式管理 COM 对象运行时?

r - as.list.Date(R 编程语言)中的潜在错误

string - 如何在 Go 中将 uint16 转换为 2 字节字符串?

c++ - 在 std::map 中使用 2 个键

.net - 为什么 COM 对象不使用 IDisposable?

Python Win32Com & Excel.Application 无法保存在调度程序上

python - 如何将unicode dict转换为字符串

c++ - 数组 vector 是否连续?