Re: syntax for fixed array in C#?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Daniel O'Connell [C# MVP] (onyxkirx_at_--NOSPAM--comcast.net)
Date: 08/03/04


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



Relevant Pages

  • Re: Marshalling Question: Array of Complex Structs?
    ... I've used no managed types within the struct. ... the following is the code used to call the DLL function. ... C style arrays don't have any information about the length of the array, ... byte buffers to fill with character data while the marshalling code ...
    (microsoft.public.dotnet.languages.csharp)
  • consuming unmanaged DLL from managed C#
    ... The DLL is "ae766.dll". ... This array ... What C# code do I need to write, to "translate" the struct and to import the function? ... need to write the whole code for me, because there are repeated data types (inside the struct, for ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: pinvoke - C++ struct array to C#
    ... I'm authoring a C++ .dll & C# .dll and need a little help. ... a struct array in one of the parameters to C#? ... public struct MyStruct ...
    (microsoft.public.dotnet.framework.interop)
  • Processing many objects taking too long
    ... The array represents an unmanaged ... struct containing a series of header fields plus a variable sized array of ... Current version of c# doesn't support this sort of struct ... Should I write a c++ dll? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Passing an array of structuresfrom a pointer?
    ... to only declare structs in headers and then define the ... the struct should be declared ... what if you have a simple array like this: ... In the header we would declare? ...
    (microsoft.public.vc.language)