syntax for fixed array in C#?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: PHil Coveney (PHilCoveney_at_discussions.microsoft.com)
Date: 08/02/04


Date: Mon, 2 Aug 2004 16:01:15 -0700

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,

--
PC


Relevant Pages

  • Re: Help needed in dereferencing a union
    ... it complains about a missing field name in bit. ... It's not the missing field name my compiler is complaining ...
    (comp.lang.c)
  • Re: Indirect visibility of private part in child packages
    ... package body P1.P3 is ... complains there are not selector Low_Level_Data for T1_Type. ... Converting to T1_Type should work. ... think that the compiler gonna complain that Set is a primitive of ...
    (comp.lang.ada)
  • bug in eVC 4.0 using static variables?
    ... If I compile it for release then the compiler tries to inline the function ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Difficult example?
    ... I used an old Sun Fortran 90 compiler. ... It complains about the ...
    (comp.lang.fortran)
  • Re: Inner classes and method signatures
    ... >> public class Foo { ... >>I've perused the Java Language Specification, but haven't found a reason ... >>why my code snippet does not compile. ... Does anyone know why the compiler ...
    (comp.lang.java.programmer)