I have the following C++ function (belongs to a COM interface, derived from IUnknown), which I want to call from C# code:
C++ declaration as documented:
HRESULT Function1([in] STRUCT1 *s1, [in, out] STRUCT2 *s2, [in] SIZE_T var1);
Declaration inside a working C++ program:
STDMETHOD(Function1)(
THIS_
__out STRUCT1 * s1,
__in_ecount_opt(var1) const STRUCT2 * s2,
SIZE_T var1
) PURE;
In C# territory I define the following:
[StructLayout(LayoutKind.Sequential)]
public struct STRUCT1
{
public uint member1; //HRESULT member1
public ulong member2; //SIZE_T member2
}
[StructLayout(LayoutKind.Sequential)]
public struct STRUCT2
{
public IntPtr member1; //VOID *member1;
public ulong member2; //SIZE_T member2;
public STRUCT3 member3; //STRUCT3 member3;
}
[StructLayout(LayoutKind.Sequential)]
public struct STRUCT3
{
public int member1; //int member1
}
I implement this function in C# as follows :
[ComImport, ComVisible(false), InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("…")]
public interface Iinterface1
{
……
uint Function1(ref STRUCT1 s1, ref STRUCT2 s2, ulong var1);
……
}
and I invoke the function like this:
STRUCT1 temp1 = new STRUCT1();
temp1.member1 = 0;
temp1.member2 = 0;
STRUCT2 temp2 = new STRUCT2();
STRUCT3 temp3 = new STRUCT3();
temp3.member1 = 0;
temp2.member1 = IntPtr.Zero;
temp2.member2 = 0;
temp2.member3 = temp3;
ulong var1 = 1;
res1 = COMobject.Function1(ref temp1, ref temp2, var1);
When function is executed I get an Access Violation Exception :
“An unhandled exception of type 'System.AccessViolationException' occurred in prog1.exe Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. “
I implemented most of functions of the same interface with no problems, as well as a lot of other interfaces in this application. This one really puzzles me.
I would really appreciate your help on this..
Thank you in advance.
IntPtrforSIZE_T(only necessary if it might run on a 32 bit processor, but good practice anyway). I would also suggest you double check if the AccessViolationException is happening in the marshaling code or from within the com method itself.