获取c#输出字符串参数值为c++ BSTR*

Get C# out string paramater value to C++ BSTR*?

本文关键字:c++ BSTR 参数 输出 字符串 获取      更新时间:2023-10-16

我有一个方法在c# DLL有一个输入和两个输出参数。

我想从firebreath c++中调用该函数。我如何从c++中传递BSTR*值?我如何从c#中得到这些值?

我已经试过使用

BSTR* userKey =NULL;
*userKey = ::SysAllocString(L"Hello Managed World");

但是它不工作。

c#功能:

public void GetPublicKeyforEncryption(String ToUserEmailAddress, out String userKey , out String mySharedKey)
调用函数:
std::wstring& ToUserEmailaddress;
BYTE **bEncryptData = NULL;
BSTR bsData = SysAllocStringLen(ToUserEmailaddress.data(), ToUserEmailaddress.size());
BSTR* userKey =NULL;
BSTR* mySharedKey =NULL;
CSharpInterface->GetPublicKeyforEncryption(bsData,userKey ,mySharedKey );

在上面的c++ getpublickeyforencryptionbsdata值被正确传递,我将如何分配内存和传递

   userKey ,mySharedKey in the BSTR* type

谁能解决这个问题?帮助我。

谢谢桑珠抱

这不是正确的代码,BSTR已经是指针类型。您需要传递一个指向BSTR变量的指针,以便被调用的方法可以对其进行赋值。假设只有一个out string参数的泛型代码:

BSTR retval = NULL;
HRESULT hr = CSharpInterface->SomeMethod(&retval);
if (SUCCEEDED(hr)) {
    // Use retval
    //...
    SysFreeString(retval);
}