I am using a C++ application to call a c sharp dll. Regasm turned my function from having two character arrays and returning an int into a function with two BSTRs and a long pointer as parameters. The two BSTRs were labeled as inputs and the long pointer was my original return value. The problem is that one of my character arrays was a true input and the other was error information being returned from the dll. Is there an identifier to let regasm know that that my second string parameter is actually an output? While debugging, I verified that the second parameter was being changed in the dll but didn't make it back to the calling function.
1 Answers
That is indeed the normal transformation of a .NET method to a COM method. BSTR is the standard string interop type, it gives very strong guarantees that a string can be properly marshaled across a module/language boundary without major memory management headaches. A plain string turns into a BSTR, a ref/out string turns into a BSTR* that allows you to pass a new string back. Much the same for the return type, a COM method must return an HRESULT to indicate success/failure so necessarily the original return value becomes an out argument in C# speak.
You'd be very unwise to tinker with this, lots of ways to blow your leg off. You can by applying the [MarshalAs] attribute to force a different argument type to be used. [PreserveSig] to suppress the return value transformation. The function call can no longer be marshaled across apartment boundaries when you do this. Again, be very careful with this, especially when you want to return strings. That's a guaranteed memory management problem. Dealing with BSTR or return values in C++ is not a problem, you can google plenty of example code.

- 922,412
- 146
- 1,693
- 2,536