I have a C++ struct like this:
struct TESTSTRUCT
{
DWORD dwTest;
WCHAR wszTest[ 256 ];
};
used in an interface like this:
virtual HRESULT Test(TESTSTRUCT *test) = 0;
The C# COM server interface is defined as:
void Test(ref TESTSTRUCT test);
and the struct is defined as:
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
public struct TESTSTRUCT
{
public UInt32 dwTest;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=255)] public String
sTest;
}
According to Microsoft, this is the correct way to handle fixed length
arrays in structures, but it does not seem to work. I'm trying to use
this as an in/out parameter, and values are making it into the C#
server just fine, but any modifications that are made there are not
reflected in the C++ client. If I remove the array from the struct,
everything works fine, so assume my interface definition is ok, it's
just my structure that is somehow wrong. When I look at the type
library for the C# assembly, the struct definition looks identical to
the C++ version, yet it still will not marshal correctly.
Any ideas?
Idael Cardoso - 12 Apr 2004 10:50 GMT
Apply In and Out attributes to your C# definition:
void Test([In, Out] ref TESTSTRUCT test);
It must work, the other think you can do is define it as:
void Test(IntPtr test);
and use Marshal.PtrToStructure and Marshal.StructureToPrt. I agree that's is
not a prety way but it can be used as a last resource.
Regards,
Idael
> I have a C++ struct like this:
>
[quoted text clipped - 33 lines]
>
> Any ideas?
Vince Gatto - 13 Apr 2004 06:51 GMT
Yep, that's it. I need to put "Out" in the method signature. I
figured it was something simple. Thanks.