Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using an old-style dll

Status
Not open for further replies.

davewelsh

Programmer
Jun 26, 2001
71
CA
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:

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;
}
or
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);
or
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?
 
Try Reviewing the hints at the following link:

I am also having problems calling into an old dll with C# or VB. I am using StringBuilder but with no success.

Here is what I am using:
<DllImport("Gpib-32.dll", EntryPoint:="Receive", SetLastError:=True, CharSet:=CharSet.Auto, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Sub Receive32s(ByVal ud As Integer, ByVal addr As Integer, <MarshalAs(UnmanagedType.LPStr)> ByRef bufAddress As StringBuilder, ByVal cnt As Long, ByVal term As Integer)
End Sub

I have tried using ByVal or ByRef and also pinning the address and passing it in. All that I get back are a couple of control characters which happen to be CR LF. Does anyone know if stringBuilder emptys itself when it sees a CRLF?

Art
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top