Re: syntax for fixed array in C#?
From: Daniel O'Connell [C# MVP] (onyxkirx_at_--NOSPAM--comcast.net)
Date: 08/03/04
- Next message: zz: "Will Microsoft provide a .NET MMC designer like that for VB6?"
- Previous message: Jon: "Yukon & .Net v2"
- In reply to: PHil Coveney: "syntax for fixed array in C#?"
- Next in thread: Austin Ehlers: "Re: syntax for fixed array in C#?"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 2 Aug 2004 20:21:48 -0500
"PHil Coveney" <PHilCoveney@discussions.microsoft.com> wrote in message
news:CAA56029-7CFC-433F-ADC6-8F17D866B65F@microsoft.com...
> 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.
>
AFAIK, there is no way in C# 1.0/1.1 to create a fixed array. The only way I
can think of off the top of my head would be a byte array and manually
parsing out your values into the structure, PITA, but it should work.
However, C# 2.0 does address this with the fixed keyword, but I'm afraid
that won't be of any help to you for quite some time as its not expected
until next year.
> 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,
>
> --
> PC
- Next message: zz: "Will Microsoft provide a .NET MMC designer like that for VB6?"
- Previous message: Jon: "Yukon & .Net v2"
- In reply to: PHil Coveney: "syntax for fixed array in C#?"
- Next in thread: Austin Ehlers: "Re: syntax for fixed array in C#?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|