Click to See Complete Forum and Search --> : marshal char[]


sammyh
October 21st, 2002, 11:18 PM
I am trying to interface with a dll that uses this typedef.

typedef struct{
char szGroupKey[64]; // grouping key
char szCoordSysKey[64]; // system key
char szDatumKey[64]; // datum key
char szUnitKey[64]; // unit key
} COORDSYSTEM;


I've tried
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=64 )]
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=64 )]

and god knows what else.

I am going slighty insane;
Any help would be GREATLY appreciated;

Thanks

Alex F
October 22nd, 2002, 03:36 AM
Try CharSet = CharSet.Ansi in DllImport directive.
Read also this article:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09192002.asp

sammyh
October 22nd, 2002, 10:41 AM
CharSet.Ansi gives me errors

This isn't throwing errors, but it still isn't working right:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto )]
public struct COORDSYSTEM
{
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=64 )]
public string szGroupKey; // grouping key
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=64 )]
public string szCoordSysKey; // system key
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=64 )]
public string szDatumKey; // datum key
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=64 )]
public string szUnitKey; // unit key
}


I've gotten it to work great in VB with this:

Type COORDSYSTEM
szGroupKey As String * 64
szCoordSysKey As String * 64
szDatumKey As String * 64
szUnitKey As String * 64
End Type

anmccall
February 23rd, 2007, 08:44 PM
Check this out: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=15225&SiteID=1