convert ATL::CComBSTR to BSTR*
convert ATL::CComBSTR to BSTR*
我是c++新手。我创建了我的c# DLL。我创建了Managed c++ DLL并在我的c#项目中引用它。我想从c# dll返回char*
中的字符串值问题是,我不能将CComBSTR
转换为BSTR
?
UINT CHandler::GetData( UINT idx, char* lName)
{
HRESULT hRes= m_p->GetData(idx, CComBSTR(lName));
}
Error: Fehler by CComBSTR(lNmae): 977 IntelliSense: It is no possible conversion of ""ATL::CComBSTR"" in ""BSTR *"" available.
我的c#函数有第二个参数类型为BSTR*
你需要这样做…
CHandler::GetData调用COM接口获取char* lName
查看分配的内存如何被释放。
UINT CHandler::GetData(UINT idx, char* lName)
{
BSTR bstrName;
HRESULT hRes= m_p->GetData(&bstrName);
char *p= _com_util::ConvertBSTRToString(bstrName);
strcpy(lName,p); //lName needs to be large enough to hold the string pointed to by p
delete[] p; //ConvertBSTRToString allocates a new string you will need to free
//free the memory for the string
::SysFreeString(bstrName);
}
COM接口方法定义
语言:c++,请翻译成c# 基本上,你需要在c#方法中分配BSTR。
查看COM方法如何分配和返回内存
HRESULT CComClass::GetData(long idx, BSTR* pbstr)
{
try
{
//Let's say that m_str is CString
*pbstr = m_str.AllocSysString();
//...
}
catch (...)
{
return E_OUTOFMEMORY;
}
// The client is now responsible for freeing pbstr.
return(S_OK);
}
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- C++ Singleton - Prevent ::instance() to variable
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- Python str to C++ to Python str
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- "no matching function for call to 'Vector::Vector'"错误
- vector<vector<double>> to mxArray using memcpy
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 当覆盖存在时调用基本虚拟"binded to object"函数
- OpenGL VBO Indexing ( How to compute Index Array)
- 为什么我会" void value not ignored as it ought to be"?
- convert ATL::CComBSTR to BSTR*