Re: Array Pointers in C#, calling C++ DLL
From: BMermuys (someone_at_someone.com)
Date: 03/20/04
- Next message: Carlos Guzmán Álvarez: "Re: How can I convert 2D array to 1D array"
- Previous message: Vanessa: "Re: Last control on focus"
- In reply to: Chucker: "Re: Array Pointers in C#, calling C++ DLL"
- Next in thread: BMermuys: "Re: Array Pointers in C#, calling C++ DLL"
- Reply: BMermuys: "Re: Array Pointers in C#, calling C++ DLL"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 20 Mar 2004 13:45:06 +0100
Hi,
[inline]
"Chucker" <anonymous@discussions.microsoft.com> wrote in message
news:C82F36E7-1752-4B7F-AA3B-D30CCC1EB643@microsoft.com...
> Hi BMermuys,
>
> thanks for your effort.
> The Array in **data represents 4-D Data in a 1-D Array
> by the value in m_type I can see if it´s Double or Float Data
> in the Array. In res[4] I get the Dimensions of the Array
> this means xres, yres, zres and tres. So I can calculate
> the boundaries of the array.
>
> Its xres*yres*zres*tres*(sizeoffloat or sizeofdouble)
>
> When using the dll from c++ I delete the pointer to the
> array in my calling function after use. But you are
> right, I don´t know how to do this in C# either.
>
> Will be a problem anyway since the dll runs with
> vc6 runtime and uses a different heap.
>
> But I can fix the deallocation problem in the dll.
> But I still can´t call it!
>
> So if I know the size of the Array (after the call)
> of the dll function, how to create my variables?
public class FileIO
{
int[] resolution;
float[] length;
double[] data;
[DllImport("MYLIB.DLL", SetLastError=true)]
public static extern int dll_dbl_read(
[MarshalAs(UnmanagedType.LPStr)] string fname,
ref int[] res,
ref float[] len,
ref IntPtr data);
public int dbl_read(string fname)
{
int result = 0;
resolution = new int[] {0,0,0,0};
length = new float[] {0,0,0,0 };
IntPtr pData = IntPtr.Zero;
result = dll_dbl_read( fname, ref resolution, ref length, ref pData );
int n = ... ; // calculate _number of elements_ in data
data = new double[n];
Marshal.Copy ( pData, data, 0, n );
// here you should free the memory pointed by pData...
return result;
}
}
- Passing a null-pointer by reference which means passing a pointer to a
null-pointer.
- The dll function sets this null pointer into a valid pointer by allocating
memory for the data.
- After return copy the unmanaged data into managed data.
Note that Marshal.Copy is overloaded for different data types, so you should
give the number of elements not the byte size.
HTH,
greetings
>
> Thanks in advance
>
> Martin
>
>
>
>
>
> ----- BMermuys wrote: -----
>
> Hi,
> [inline]
>
> "Chucker" <anonymous@discussions.microsoft.com> wrote in message
> news:F42503F1-5E25-4302-8679-CB2A740B47F2@microsoft.com...
> > I got a DLL that reads some RAW Double Data from a File to an
Array.
> > The DLL works fine.
> >> This is the Header of the double_read function:
> >> int dll_dbl_read (const char * fnam,
> > /* File name to read from */
> > int * res,
> > /* returns int[4]*/
> > float * len,
> > /* returns float[4]*/
> > double ** data);
> > /* returns double[unknown size]*/
> >> This is how I call the function from C++:
>
> Two problems:
> 1) At least you must know the size of the array when the function
returns,
> unless it's 0 terminated maybe. How else could you safely use it,
even in
> c/c++ ?
>
> 2) To have a clean/safe solution, the DLL should also export a
function
> which de-allocates the memory it has allocated to 'data' when the
function
> (dll_dbl_read) was called. Something like "void dll_free_data(
double*
> data );".
>
> HTH,
> greetings
>
> >> char * myfname;
> > int res[4];
> > float len[4];
> > double * data;
> > int m_type;
> >> int DataStack::dbl_read(const char * fname)
> > {
> > m_type = dll_dbl_read(fname,res,len,&data);
> > myfname = new char[strlen(fname)+1];
> > strcpy(myfname, fname);
> >> return m_type;
> > }
> >> This works fine and I would like to know how to call this function
from
> C#?????
> >> This is what I try in C# and what doesn´t work:
> >> using System;
> > using System.Runtime.InteropServices;
> >> namespace mine
> > {
> > /// <summary>> /// Summary description for FileIO.
> > /// </summary>>>> public unsafe class FileIO
> > {
> > public int m_type;
> > public char* myfname;
> > public int * res;
> > public float * len;
> > public double ** data;
> >> [DllImport("MYLIB.DLL", EntryPoint="dll_dbl_read",
> SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true,
> CallingConvention=CallingConvention.StdCall)]
> > public static unsafe extern int dll_dbl_read(char[] fnam,
int* res,
> float* len, double** data);
> >> public int dbl_read(string fname)
> > {
> > int result;
> > int[] resolution = {0,0,0,0};
> > float[] length = {0,0,0,0};
> > double[] mydata = {0};
> > myfname = &fname.ToCharArray();
> > fixed (int* res = resolution)
> > {
> > fixed (float* len = length)
> > {
> > fixed (double* data = mydata)
> > {
> > result = dll_dbl_read(myfname,res,len,&data);
> > /*Just for Testing*/
> > int x = res[0];
> > float xl = len[0];
> > double test = data[100];
> > test = data[23];
> > /*Everything is empty here :o(*/
> > }
> > }
> > }
> >> return result;
> > }
> >> public FileIO()
> > {
> > }
> > }
> >> Can Anybody tell me how to Create my Variables to pass to the
function
> > dll_dbl_read? So that I find my data in there and not only 0 or
NULL?
> >> Thanks in Advance
> >> Martin
>
>
>
- Next message: Carlos Guzmán Álvarez: "Re: How can I convert 2D array to 1D array"
- Previous message: Vanessa: "Re: Last control on focus"
- In reply to: Chucker: "Re: Array Pointers in C#, calling C++ DLL"
- Next in thread: BMermuys: "Re: Array Pointers in C#, calling C++ DLL"
- Reply: BMermuys: "Re: Array Pointers in C#, calling C++ DLL"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|