[Mono-list] Marshalling byte[]

Dmitry Kostenko bis0n@mail.ru
Wed, 27 Oct 2004 19:48:05 +0300


Hello, Neale.

You should not use ByValArray enumeration member on function arguments. MS.NET 
SDK documentation says that ByValArray should be used only for structure members.

Are you passing byte arrays as in or out arguments?

What is the prototype of your C function?
1. int scumbag( int* X, char** fb, int lFb, ...)
2. int scumbag( int* X, char* fb, int lFb...)
3. int scumbag( int* X, char** fb, int* lFb...)

Neale.Ferguson@SoftwareAG-USA.com wrote:

> 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 = "scumbag")]
>                 private unsafe static extern int scumbag(ref int X,
>                 [MarshalAs(UnmanagedType.ByValArray, SizeParamIndex=2)] ref byte[] fb,
>                 long  lFb,
>                 [MarshalAs(UnmanagedType.ByValArray, SizeParamIndex=4)] ref byte[] rb,
>                 long  lRb,
>                 [MarshalAs(UnmanagedType.ByValArray, SizeParamIndex=6)] ref byte[] sb,
>                 long  lSb,
>                 [MarshalAs(UnmanagedType.ByValArray, SizeParamIndex=8)] ref byte[] vb,
>                 long  lVb,
>                 [MarshalAs(UnmanagedType.ByValArray, SizeParamIndex=10)] ref byte[] ib,
>                 long  lIb);
> 
>         static void Main() {
>                 int rc;
>                 Console.WriteLine("Waiting..");
>                 string line = 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.Specialized.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.Reflection.Emit.ConstructorBuilder,int,Mono.CSharp.Location)
> in <0x00000158> Mono.CSharp.Parameters:LabelParameters (Mono.CSharp.EmitContext,System.Reflection.MethodBase,Mono.CSharp.Location)
> 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 = new byte[1024];
> 
> f(val, val.Length);
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
> 
> 

-- 
Dmitry Kostenko
.NET Developer