I have a dll that I used to call from C++ using exten "C". It's been a while since wrote the code (since I migrated the code to VB and have been updating it from there). Now I want to bring it over to C#.
I've got a start on it but I keep running into problems marshalling.
Let's call the function old_func(). It takes a pointer to a struct and an integer (BTW all integers are 2-byte).
here's the type definition:
and the prototype:
I've done several things in my C# project to try to get this to work. First, this is my C# structure to be passed to the dll (I've tried it two ways):
or
I think I don't need all the extra meta-data in the second struct. "short" seems to be good enough.
Now for the prototype in C# (I've tried it two ways too):
or
Now from this I still get "Can not marshal field foundcode". Does anyone know what I'm doing wrong?
I've got a start on it but I keep running into problems marshalling.
Let's call the function old_func(). It takes a pointer to a struct and an integer (BTW all integers are 2-byte).
here's the type definition:
Code:
typedef struct
{
int reclen;
int st1;
int st1len;
int st2;
int st2len;
int city;
int citylen;
int prov;
int provlen;
int fsa;
int ldu;
char found_code[6];
int quality;
int count;
char record[500];
} POSTREC;
and the prototype:
Code:
void old_func(POSTREC *postrec, int parm);
I've done several things in my C# project to try to get this to work. First, this is my C# structure to be passed to the dll (I've tried it two ways):
Code:
[StructLayout(LayoutKind.Sequential)]
public struct POSTREC
{
public short reclen;
public short st1;
public short st1len;
public short st2;
public short st2len;
public short city;
public short citylen;
public short prov;
public short provlen;
public short fsa;
public short ldu;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=6)]
public char []foundcode;
public short quality;
public short count;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=500)]
public char []record;
}
Code:
[StructLayout(LayoutKind.Sequential)]
public struct POSTREC
{
[MarshalAs(UnmanagedType.I2)]
public short reclen;
[MarshalAs(UnmanagedType.I2)]
public short st1;
[MarshalAs(UnmanagedType.I2)]
public short st1len;
[MarshalAs(UnmanagedType.I2)]
public short st2;
[MarshalAs(UnmanagedType.I2)]
public short st2len;
[MarshalAs(UnmanagedType.I2)]
public short city;
[MarshalAs(UnmanagedType.I2)]
public short citylen;
[MarshalAs(UnmanagedType.I2)]
public short prov;
[MarshalAs(UnmanagedType.I2)]
public short provlen;
[MarshalAs(UnmanagedType.I2)]
public short fsa;
[MarshalAs(UnmanagedType.I2)]
public short ldu;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=6)]
public char []foundcode;
[MarshalAs(UnmanagedType.I2)]
public short quality;
[MarshalAs(UnmanagedType.I2)]
public short count;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=500)]
public char []record;
}
I think I don't need all the extra meta-data in the second struct. "short" seems to be good enough.
Now for the prototype in C# (I've tried it two ways too):
Code:
[DllImport "c:\\dir\\mydll.dll",EntryPoint="old_func",CallingConvention=CallingConvention.Cdecl)]
private static extern void old_func(ref POSTREC postrec, short call_type);
Code:
[DllImport "c:\\dir\\mydll.dll",EntryPoint="old_func",CallingConvention=CallingConvention.Cdecl)]
private static extern void old_func([MarshalAs(UnmanagedType.LPStruct)]
ref POSTREC postrec, short call_type);
Now from this I still get "Can not marshal field foundcode". Does anyone know what I'm doing wrong?