[Mono-list] (no subject)
Lloyd Dupont
lloyd@galador.net
Mon, 1 Oct 2001 15:25:22 +0200
This is a multi-part message in MIME format.
------=_NextPart_000_019B_01C14A8D.492FAD30
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
i have a lot of interop code and some C function with callback using ponter (let say int * for example) as argument.
i want to make a very tiny wrapper (avoiding unecessary memory block copying) so my "int *" becomes "int[]", but i can't manage....
i gives you here my tiny test project, if someone could make it work, this would be great .....
------=_NextPart_000_019B_01C14A8D.492FAD30
Content-Type: application/octet-stream;
name="makefile"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="makefile"
all: Main.exe cutil.dll
main
Main.exe: Main.cs
csc Main.cs
cutil.dll: main.c
gcc -shared -o cutil.dll main.c -loleaut32
------=_NextPart_000_019B_01C14A8D.492FAD30
Content-Type: application/octet-stream;
name="Main.cs"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="Main.cs"
// project created on 01/10/2001 at 11:21
using System;
using System.Runtime.InteropServices;
class MainClass
{
public static void Main(string[] args)
{
int[] tab = new int[] { 1, 2, 3, 4, 5 };
showTab(tab);
doSomething(new callback(incTab), tab, 3);
showTab(tab);
}
static void showTab(int[] tab)
{
System.Console.WriteLine("tab.Length = " + tab.Length);
for(int i=0; i<tab.Length; i++) {
System.Console.WriteLine("tab["+i+"] = " + tab[i]);
}
}
public delegate void callback(int[] tab);
static void incTab(int[] tab)
{
System.Console.WriteLine("incTab(tab.Length = "+tab.Length+")");
int i;
for(i=0; i<tab.Length; i++)
tab[i] += 100;
}
[DllImport("cutil")]
public static extern void doSomething(callback fct, int[] tab, int len);
}
------=_NextPart_000_019B_01C14A8D.492FAD30
Content-Type: application/octet-stream;
name="main.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="main.c"
// created on 01/10/2001 at 11:23
#include <stdlib.h>
// gcc -shared -o cutil.dll main.c -loleaut32
#include <windows.h>
#include <wtypes.h>
#include <unknwn.h>
#include <oleauto.h>
SAFEARRAY * I32Pointer(int * c, int len)
{
SAFEARRAYBOUND bound;
SAFEARRAY * ret;
bound.lLbound = 0;
bound.cElements = 10;
ret = SafeArrayCreate(VT_I4, 1, &bound);
return ret;
ret->pvData = c;
ret->rgsabound[0].cElements = len;
return ret;
}
void doSomething(void (* fct) (SAFEARRAY *), int * c, int len)
{
fct(I32Pointer(c, len));
printf("c(0) = %d\n", c[0]);
}
------=_NextPart_000_019B_01C14A8D.492FAD30--