G
Guest
Hello,
I am making calls into a legacy DLL. One function in this DLL expects as a parameter a struct with several fields, each of which is a 40-char array. The effect of this function is to copy characters into these char array fields.
I am calling this function with (roughly) the following:
[StructLayout(LayoutKind.Explicit, Size=120, CharSet=CharSet.Auto)]
public class Foo
{
[FieldOffset(0)] public byte A;
[FieldOffset(40)] public byte B;
[FieldOffset(80)] public byte C;
}
[DllImport("Legacy.dll")]
public static extern int copyChars (Foo fooVar);
N.B. I am using defining them as byte fields because the DLL returns 8-bit chars, and its easier to see what's going on in the debugger that way.
The calls appear to be successful, as the debugger shows enough information to see that the expected data is going to the expected fields.
My problem is getting those characters into a C# string. If I use
char dst = new char[41];
int nResult = new ASCIIEncoding().GetChars(fooVar.A, 0, 40, dst, 0);
the compiler complains that it cannot convert a byte field (fooVar.A) to a byte array. If, on the other hand, I avoid this complaint by changing the formal declaration of fooVar.A from
[FieldOffset(0)] public byte A;
to
[FieldOffset(0)] public byte[] A;
the compiler is happy, but I get a runtime exception complaining that A cannot be marshaled. (I don't know what the issue is there.)
I realize the details of this are fairly esoteric. I suspect there is some simple casting syntax where I can tell the compiler that, yes, A is a byte array, but the 4000 variations I have tried have failed.
On the other hand, if there is a way to accomplish what I'm trying to without jumping through this particular hoop, I'd welcome hearing baout it.
Thanks in advance,
I am making calls into a legacy DLL. One function in this DLL expects as a parameter a struct with several fields, each of which is a 40-char array. The effect of this function is to copy characters into these char array fields.
I am calling this function with (roughly) the following:
[StructLayout(LayoutKind.Explicit, Size=120, CharSet=CharSet.Auto)]
public class Foo
{
[FieldOffset(0)] public byte A;
[FieldOffset(40)] public byte B;
[FieldOffset(80)] public byte C;
}
[DllImport("Legacy.dll")]
public static extern int copyChars (Foo fooVar);
N.B. I am using defining them as byte fields because the DLL returns 8-bit chars, and its easier to see what's going on in the debugger that way.
The calls appear to be successful, as the debugger shows enough information to see that the expected data is going to the expected fields.
My problem is getting those characters into a C# string. If I use
char dst = new char[41];
int nResult = new ASCIIEncoding().GetChars(fooVar.A, 0, 40, dst, 0);
the compiler complains that it cannot convert a byte field (fooVar.A) to a byte array. If, on the other hand, I avoid this complaint by changing the formal declaration of fooVar.A from
[FieldOffset(0)] public byte A;
to
[FieldOffset(0)] public byte[] A;
the compiler is happy, but I get a runtime exception complaining that A cannot be marshaled. (I don't know what the issue is there.)
I realize the details of this are fairly esoteric. I suspect there is some simple casting syntax where I can tell the compiler that, yes, A is a byte array, but the 4000 variations I have tried have failed.
On the other hand, if there is a way to accomplish what I'm trying to without jumping through this particular hoop, I'd welcome hearing baout it.
Thanks in advance,