Reputation: 1022
I have c++ code that has a parameter like this:
STDMETHODIMP TVinaImpl::test(BSTR Param1)
{
try
{
Param1=(L"test1");
}
catch(Exception &e)
{
return Error(e.Message.c_str(), IID_IVina);
}
return S_OK;
}
I use c++ builder:
When I call this com dll function in c# it shows me the error:
IntPtr a = new IntPtr();
vina.test(a);
it is null and did not get the value.
How can I pass variable from C# to c++ com and pass back it?
Upvotes: 0
Views: 337
Reputation: 6040
I'd recommend you declare the argument Param1 as [out, retval]
. You are not using Param1 as any kind of input.
STDMETHODIMP TVinaImpl::test(BSTR* Param1)
{
try
{
*Param1= SysAllocString(L"test1");
}
catch(Exception &e)
{
return Error(e.Message.c_str(), IID_IVina);
}
return S_OK;
}
Then when you call the function from C# it is just,
string s = vina.test();
The .Net runtime manages marshalling of data from .Net to COM and vice versa.
Upvotes: 0
Reputation: 1354
Since Param1
is declared as an [in, out]
parameter, you need to declare it as a pointer to a BSTR: STDMETHODIMP TVinaImpl::test(BSTR* Param1)
Furthermore, you cannot simply assign a string literal to a BSTR. The correct way is to allocate memory using SysAllocString
: *Param1 = SysAllocString(L"test1");
Upvotes: 2