[Mono-list] Marshalling byte[]

Neale.Ferguson@SoftwareAG-USA.com Neale.Ferguson@SoftwareAG-USA.com
Wed, 27 Oct 2004 10:20:37 -0400


Thanks Dmitry,
 I tried this and am getting the following when compiling this simple =
test case:

using System;
using System.Text;
using System.Runtime.InteropServices;

public class main {

                [DllImport("libname", EntryPoint =3D "scumbag")]
                private unsafe static extern int scumbag(ref int X,
                [MarshalAs(UnmanagedType.ByValArray, =
SizeParamIndex=3D2)] ref byte[] fb,
                long  lFb,
                [MarshalAs(UnmanagedType.ByValArray, =
SizeParamIndex=3D4)] ref byte[] rb,
                long  lRb,
                [MarshalAs(UnmanagedType.ByValArray, =
SizeParamIndex=3D6)] ref byte[] sb,
                long  lSb,
                [MarshalAs(UnmanagedType.ByValArray, =
SizeParamIndex=3D8)] ref byte[] vb,
                long  lVb,
                [MarshalAs(UnmanagedType.ByValArray, =
SizeParamIndex=3D10)] ref byte[] ib,
                long  lIb);

        static void Main() {
                int rc;
                Console.WriteLine("Waiting..");
                string line =3D Console.ReadLine();
        }

}

> mcs marsh.cs -unsafe
Unhandled Exception: System.NullReferenceException: Object reference not =
set to an instance of an object
in <0x0000034c> Mono.CSharp.Attribute:GetMarshal ()
in <0x00000054> Mono.CSharp.ParameterBase:ApplyAttributeBuilder =
(Mono.CSharp.Attribute,System.Reflection.Emit.CustomAttributeBuilder)
in <0x0000009c> Mono.CSharp.Parameter:ApplyAttributeBuilder =
(Mono.CSharp.Attribute,System.Reflection.Emit.CustomAttributeBuilder)
in <0x000001a4> Mono.CSharp.Attribute:Emit =
(Mono.CSharp.EmitContext,Mono.CSharp.Attributable,System.Collections.Spec=
ialized.ListDictionary)
in <0x0000011c> Mono.CSharp.Attributes:Emit =
(Mono.CSharp.EmitContext,Mono.CSharp.Attributable)
in <0x0000012c> Mono.CSharp.Parameter:DefineParameter =
(Mono.CSharp.EmitContext,System.Reflection.Emit.MethodBuilder,System.Refl=
ection.Emit.ConstructorBuilder,int,Mono.CSharp.Location)
in <0x00000158> Mono.CSharp.Parameters:LabelParameters =
(Mono.CSharp.EmitContext,System.Reflection.MethodBase,Mono.CSharp.Locatio=
n)
in <0x0000027a> Mono.CSharp.MethodData:Emit =
(Mono.CSharp.TypeContainer,Mono.CSharp.Attributable)
in <0x00000032> Mono.CSharp.Method:Emit ()
in <0x000007d2> Mono.CSharp.TypeContainer:EmitType ()
in <0x00000672> Mono.CSharp.RootContext:EmitCode ()
in <0x00001334> Mono.CSharp.Driver:MainDriver (string[])
in <0x00000026> Mono.CSharp.Driver:Main (string[])

-----Original Message-----
Hello, Neale.

You could use:

extern static void f([MarshalAs(UnmanagedType.LPArray] byte[] p, int =
size);

C function:
void f(char* p, int size) {}

C# call:

byte[] val =3D new byte[1024];

f(val, val.Length);