M
marco_segurini
Hi,
I have a c++ dll that exports a function with this signature:
extern "C" void WINAPI Dump(int * pvInt, int iLen);
Calling this function from C# code is not a problem if I want to pass
the whole array:
....
[DllImport("PartialArrayPassingDll.dll")]
public extern static void Dump(int[] vInt, int iLen);
....
int[] vInt = new int[5];
Dump(vInt, vInt.Length);
....
Now I like to now if there is a way to do a call like:
'Dump(vInt+2, vInt.Length-2)' //this is pseudo code
instead to declare a wrapper function and use a GCHandle
like shown in my example.
thanks.
Marco.
//Code sample start here
using System;
using System.Runtime.InteropServices;
namespace PartialArrayPassing
{
class Class1
{
[DllImport("PartialArrayPassingDll.dll")]
public extern static void Dump(int[] vInt, int iLen);
//extra function I like to avoid
[DllImport("PartialArrayPassingDll.dll")]
public extern static void DumpIP(IntPtr vInt, int iLen);
[STAThread]
static void Main()
{
int[] vInt = new int[5];
for (int iPos=0; iPos<vInt.Length; ++iPos)
vInt[iPos] = iPos;
//Whole array passed
Dump(vInt, vInt.Length);
//Partial array passed
const int iStartPos = 2;
GCHandle gch = GCHandle.Alloc(vInt, GCHandleType.Pinned);
DumpIP((IntPtr)((int)gch.AddrOfPinnedObject()+Marshal.SizeOf(typeof(int))*iStartPos),
vInt.Length-iStartPos);
gch.Free();
}
}
}
//Code sample end here
I have a c++ dll that exports a function with this signature:
extern "C" void WINAPI Dump(int * pvInt, int iLen);
Calling this function from C# code is not a problem if I want to pass
the whole array:
....
[DllImport("PartialArrayPassingDll.dll")]
public extern static void Dump(int[] vInt, int iLen);
....
int[] vInt = new int[5];
Dump(vInt, vInt.Length);
....
Now I like to now if there is a way to do a call like:
'Dump(vInt+2, vInt.Length-2)' //this is pseudo code
instead to declare a wrapper function and use a GCHandle
like shown in my example.
thanks.
Marco.
//Code sample start here
using System;
using System.Runtime.InteropServices;
namespace PartialArrayPassing
{
class Class1
{
[DllImport("PartialArrayPassingDll.dll")]
public extern static void Dump(int[] vInt, int iLen);
//extra function I like to avoid
[DllImport("PartialArrayPassingDll.dll")]
public extern static void DumpIP(IntPtr vInt, int iLen);
[STAThread]
static void Main()
{
int[] vInt = new int[5];
for (int iPos=0; iPos<vInt.Length; ++iPos)
vInt[iPos] = iPos;
//Whole array passed
Dump(vInt, vInt.Length);
//Partial array passed
const int iStartPos = 2;
GCHandle gch = GCHandle.Alloc(vInt, GCHandleType.Pinned);
DumpIP((IntPtr)((int)gch.AddrOfPinnedObject()+Marshal.SizeOf(typeof(int))*iStartPos),
vInt.Length-iStartPos);
gch.Free();
}
}
}
//Code sample end here