I have a C# Winforms application that makes a call to a COM class. When debugging using Visual Studio it steps into the c++ code and returns S_OK but when the function returns visual studio hangs and the application crashes. I have to end process on the VS process to stop the program from running. If i run the app outside of visual studio the application simply crashes.
Everything was working fine and i have no idea what i may have done to cause this problem.
Any help is appreciated. Thanks
Sj
This is the interface definition
typedef struct
{
long ExpiryData
BSTR IssuedBy;
} LicenceData;
[
object,
uuid (5A734F95-EABE-440B-8B7E-0F73538A24AC),
pointer_default(unique),
helpstring("ILicenceReader Interface"),
]
interface ILicenceReader : IDispatch
{
HRESULT ReadLicenceFile ([in, out]LicenceData* plicenceData, LPCTSTR filePath);
};
[
uuid(C2833A21-6586-4648-ABC8-D42BC3225699)
]
coclass LicenceReader
{
[default] interface ILicenceReader;
};
I have referenced the COM dll and allowed VS to generate the Interop and the usage in the c# application:
LicenceData data = new LicenceData();
ILicenceReader reader = new LicenceReader();
reader.ReadLicenceFile(ref data, filePath);
Thanks for your help.
I'll bet that the COM subsystem is trying to unmarshall a BSTR allocated on the stack, or perhaps allocated with a smart pointer on the stack.
BSTRs have to be allocated with SysAllocString. The result from this can be returned as-is as it's not on the stack and nothing will try to free it erroneously.
If you use a smart pointer BSTR class such as CComBSTR or _bstr_t then you'll need to set the IssuedBy member via a Detach. CComBSTR::Detach() will return the pointer to the BSTR and not try to free it when that local instance of CComBSTR goes out of scope.
plicenceData->IssuedBy = CComBSTR("Some Dude").Detach();
Another possibility is that you try to do something like plicenceData = new plicenceData within your COM class, overwriting the instance passed in. That won't work.
In the end, pretty much the only reason a COM function fails after it's finished and returned is because of marshalling issues. It's the layer between your C# code and the called C++ that's trying to translate the data across apartment and possibly process boundaries. You need to ensure that you follow the COM rules to the letter to allow marshalling to do it's job.
So, double check all your pointers. Are they on the stack or on the heap? They need to be on the heap. Are all BSTRs allocated appropriately? Using smart BSTR classes will usually help significantly, but just remember that you can't return the raw members. Use these classes as they're expected to be used.