Passing a 2d array to a DLL written in C++

2004-04-20 Thread znakeeye
Hi!

I have made a DLL in C++/MFC. I have to implement a function that can
fill a LabView 2D-array with some data. How can that be done?

I suppose the function should look something like this:
int MyDllFunction(unsigned char **matrix, int width, int height)
{
   for (int r = 0; r < height; r++)
   {
  for (int c = 0; c < width; c++)
 matrix[r][c] = SomeData(r, c);
   }
   
   return 1;
}

Where should the memory of the array be declared? Maybe LabView can
allocate the memory itself and pass the function a char-array
(together with the width and height) to my function? How does it work?

Any help is really appreciated! Thanks!

/Chris



Re: Passing a 2d array to a DLL written in C++

2004-04-20 Thread znakeeye
Rolf <[EMAIL PROTECTED]> wrote in message news:<[EMAIL PROTECTED]>...
> znakeeye wrote:
> > Hi!
> > 
> > I have made a DLL in C++/MFC. I have to implement a function that can
> > fill a LabView 2D-array with some data. How can that be done?
> > 
> > I suppose the function should look something like this:
> > int MyDllFunction(unsigned char **matrix, int width, int height)
> > {
> >for (int r = 0; r < height; r++)
> >{
> >   for (int c = 0; c < width; c++)
> >  matrix[r][c] = SomeData(r, c);
> >}
> >
> >return 1;
> > }
> 
> int MyDllFunction(unsigned char *matrix, int width, int height)
> 
> In fact this is what LabVIEW will generate to be passed to the DLL for a 
> two dimensional uInt8 array and your function will have to look like this:
> 
> int MyDllFunction(unsigned char *matrix, int width, int height)
> {
> for (int r = 0; r < height; r++)
> {
>for (int c = 0; c < width; c++)
>   matrix[r * width + c] = SomeData(r, c);
> }
> 
> return 1;
> }
> 
> Two dimensional arrays in C are a little underspecified. Some see it as 
> a single pointer with column * row elements, others as a pointer to an 
> array of pointers. LabVIEW chooses for the first!
> 
> > Where should the memory of the array be declared? Maybe LabView can
> > allocate the memory itself and pass the function a char-array
> > (together with the width and height) to my function? How does it work?
> 
> The memory should always be allocated by the caller, e.g. LabVIEW in 
> this case. Just use an Initialize Array function to allocate the 2 
> dimensional array before passing it to the Call Library Function.
> 
> Rolf K

So basically my function is valid (although I have to switch rows and
columns for a valid result) and all I need is to make a LabView
program that allocates a 2d-array and passes it to this dll function?
Thanks for the information regarding rows and columns.
/Chris



Re: Passing a 2d array to a DLL written in C++

2004-04-20 Thread znakeeye
Evan <[EMAIL PROTECTED]> wrote in message news:<[EMAIL PROTECTED]>...
> Chris,
> 
> I just wanted to add my 2 cents on the topic.  Rolf has got you down
> the correct path, however there is an example that I think will really
> help you.
> 
> It ships with LabVIEW 7.0 and can be found at C:\Program
> Files\National Instruments\LabVIEW 7.0\examples\dll\data
> passing\Calling Native Code.llb
> 
> This example shows how to pass all sorts of data to DLLs.  It has the
> calling VI, the DLL, and the source for that DLL.  In addition there
> is the Using External Code in LabVIEW manual.
> 
> Evan
> National Instruments

Thanks! I'll take a look at it!



Re: Passing a 2d array to a DLL written in C++

2004-04-20 Thread znakeeye
rolf <[EMAIL PROTECTED]> wrote in message news:<[EMAIL PROTECTED]>...
> >So basically my function is valid (although I have to switch rows and
> >columns for a valid result) and all I need is to make a LabView
> >program that allocates a 2d-array and passes it to this dll function?
> 
> Well, not sure about switching columns and rows really, you will have
> to try this out. My change is in the type of the matrix parameter.
> Compare it to your original code.
> 
> >>int MyDllFunction(unsigned char **matrix, int width, int height)
> 
> int MyDllFunction(unsigned char *matrix, int width, int height)
> {
>  for (int r = 0; r < height; r++)
>  {
> for (int c = 0; c < width; c++)
>matrix[r * width + c] = SomeData(r, c);
>  }
>  return 1;
> }
> 
> Rolf K

Thanks a lot! I'll try it out. To me it seems quite strange to be
using a single pointer though. But, then again, I don't really know
how LabView works =).



Re: Passing a 2d array to a DLL written in C++

2004-04-13 Thread Greg McKaskle
> Thanks a lot! I'll try it out. To me it seems quite strange to be
> using a single pointer though. But, then again, I don't really know
> how LabView works =).

As Rolf pointed out, this isn't a LV issue, but a C one.  In C, 2D 
arrays can be defined with [][], in which case the code generated by the 
compiler cannot do any of the work for you.  You must access the array 
as a single pointer like Rolf shows.  You can define the array as [x][y] 
in which case the compiler knows the size of each row and can do the 
math for you.  Anyway, this is all C and the only reason you may not 
have seen it before is because you dealt with fixed dimension arrays.

Greg Mckaskle




Re: Passing a 2d array to a DLL written in C++

2004-04-13 Thread Evan
Chris,

I just wanted to add my 2 cents on the topic.  Rolf has got you down
the correct path, however there is an example that I think will really
help you.

It ships with LabVIEW 7.0 and can be found at C:\Program
Files\National Instruments\LabVIEW 7.0\examples\dll\data
passing\Calling Native Code.llb

This example shows how to pass all sorts of data to DLLs.  It has the
calling VI, the DLL, and the source for that DLL.  In addition there
is the Using External Code in LabVIEW manual.

Evan
National Instruments



Re: Passing a 2d array to a DLL written in C++

2004-04-13 Thread rolf
>So basically my function is valid (although I have to switch rows and
>columns for a valid result) and all I need is to make a LabView
>program that allocates a 2d-array and passes it to this dll function?

Well, not sure about switching columns and rows really, you will have
to try this out. My change is in the type of the matrix parameter.
Compare it to your original code.

>>int MyDllFunction(unsigned char **matrix, int width, int height)

int MyDllFunction(unsigned char *matrix, int width, int height)
{
 for (int r = 0; r < height; r++)
 {
for (int c = 0; c < width; c++)
   matrix[r * width + c] = SomeData(r, c);
 }
 return 1;
}

Rolf K



Re: Passing a 2d array to a DLL written in C++

2004-04-13 Thread Rolf
znakeeye wrote:
> Hi!
> 
> I have made a DLL in C++/MFC. I have to implement a function that can
> fill a LabView 2D-array with some data. How can that be done?
> 
> I suppose the function should look something like this:
> int MyDllFunction(unsigned char **matrix, int width, int height)
> {
>for (int r = 0; r < height; r++)
>{
>   for (int c = 0; c < width; c++)
>  matrix[r][c] = SomeData(r, c);
>}
>
>return 1;
> }

int MyDllFunction(unsigned char *matrix, int width, int height)

In fact this is what LabVIEW will generate to be passed to the DLL for a 
two dimensional uInt8 array and your function will have to look like this:

int MyDllFunction(unsigned char *matrix, int width, int height)
{
for (int r = 0; r < height; r++)
{
   for (int c = 0; c < width; c++)
  matrix[r * width + c] = SomeData(r, c);
}

return 1;
}

Two dimensional arrays in C are a little underspecified. Some see it as 
a single pointer with column * row elements, others as a pointer to an 
array of pointers. LabVIEW chooses for the first!

> Where should the memory of the array be declared? Maybe LabView can
> allocate the memory itself and pass the function a char-array
> (together with the width and height) to my function? How does it work?

The memory should always be allocated by the caller, e.g. LabVIEW in 
this case. Just use an Initialize Array function to allocate the 2 
dimensional array before passing it to the Call Library Function.

Rolf K