简体   繁体   中英

Return a string value from C# to c++

How to pass the string from C# to COM C++ ? I have done for int and it is working fine.

My COM C++ code for int return type:

void ServiceAdapter::GetErrorCode1234()
{
long x;
m_server->GetErrorCodeOnLoginFailure(&x);
//So I get my return code value in x;
}

My C# code for int return type: So from C# iam calling like this:

int GetErrorCodeOnLoginFailure()
{
 return 3 //some return code
}

But if I do the same for String return , the code is getting crashed at run time.

My COM C++ code for string return type:

BSTR ServiceAdapter::GetVersion1234()
{
BSTR x = :SysAllocString(CCTIUtils:stringToBSTR(L""));
m_server->GetAdapterVersion(&x); // The code is crashing here.
return x;
}

My C# code for string return type:

string GetAdapterVersion() { return "6.4.23"; }

What is the issue?

Will this example work for you..? if not Sunil.. then I don't know how to consult on this issue

You need to pass the string by reference so the C# code gets an updated string:

int GetString(String^% ValueData)

or better yet:

String^ GetString() 
{
    char buffer[666];  // Hmya, what to pick?
    unmanagedGetString(buffer);
    return System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)buffer);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM