DLL: Passing an array as a parameter to a DLL 
Author Message
 DLL: Passing an array as a parameter to a DLL

Hi all,

This one has me floored!!! I can get a successful result from trying
to modify a value in an array passed from VB into a DLL. Here is my
code:

C++ DLL function code (snip)

extern "C" void _export CALLBACK Func2(short* n_var)
{      
  MessageBox(NULL,"In DLL","Test DLL",MB_OK);
  n_var[0] = 6;
  n_var[1] = 7;

Quote:
}

VB Call code (snip)

Private Declare Sub Func2 Lib "D:\DLL\TEST.DLL" (ByRef M() As Integer)

Dim z(2) As Integer
z(1) = 5
z(2) = 5

Func2 z()

MsgBox (z(1))
MsgBox (z(2))

I would expect the values z(1) and z(2) to be  6 and 7 respectively
-but- it simply does not work!!! Does anybody see something amuck with
the above code snippets???


Thanks in advance!

Marc L.
Oracle Support Canada



Sun, 03 May 1998 03:00:00 GMT  
 DLL: Passing an array as a parameter to a DLL

Quote:

>This one has me floored!!! I can get a successful result from trying
>to modify a value in an array passed from VB into a DLL. Here is my
>code:
> ... omitted ...
>I would expect the values z(1) and z(2) to be  6 and 7 respectively
>-but- it simply does not work!!! Does anybody see something amuck with
>the above code snippets???

I finally got this to work by using a workaround (thanks to Francis
Kostella for the original pointer). It turns out that VB arrays are
not compatible with C arrays (they are actually based on an OLE
struct). I decided to try to address the memory addresses directly and
this worked here's my code:

C++ DLL code (snip)

extern "C" void _export CALLBACK Func2(int* n_var, int col, int row)
{      
  int i_row = 0;
  int i_col = 0;
  int* t_n_var[10][10];
  for (i_row=0; i_row<row; i_row++)
   {
    for (i_col=0; i_col<col; i_col++)
     {
      t_n_var[i_row][i_col] = &n_var[i_col + (i_row * col)];
     }
   }
// Make a change to see the effect
*t_n_var[1][1] = 8;

Quote:
}

VB 4.0 Code (snip)

Private Declare Sub Func2 Lib "D:\PROGS\DLL\DLL.DLL" (ByRef M As
Integer, ByVal row As Integer, ByVal col As Integer)

Dim z(2, 1) As Integer
z(0, 0) = 1
z(0, 1) = 2
z(1, 0) = 3
z(1, 1) = 4
z(2, 0) = 5
z(2, 1) = 6

Func2 z(0, 0), 3, 2
MsgBox (z(1, 1))

This does the trick.

Marc Lavergne



Sun, 03 May 1998 03:00:00 GMT  
 DLL: Passing an array as a parameter to a DLL

Quote:

>VB Call code (snip)

>Private Declare Sub Func2 Lib "D:\DLL\TEST.DLL" (ByRef M() As Integer)

>Dim z(2) As Integer
>z(1) = 5
>z(2) = 5

>Func2 z()

See if this works better:

  Private Declare Sub Func2 Lib "D:\DLL\TEST.DLL" (ByRef M As Integer)
                                                        ^^^

  ...

  Func2 z(1)
        ^^^^

-- Rick
--

(Rick Rutt is a system architect living and working in Midland, Michigan.)



Sun, 03 May 1998 03:00:00 GMT  
 DLL: Passing an array as a parameter to a DLL

Quote:

>VB Call code (snip)
>Private Declare Sub Func2 Lib "D:\DLL\TEST.DLL" (ByRef M() As Integer)

Lose the "()" after the M, and don't worry about the ByRef, since that's
the default.

Luke
--
Luke Webber

* Note: The opinions expressed by Luke Webber are in no way supported *
*       by his employers, Luke Webber Consulting Services             *



Mon, 04 May 1998 03:00:00 GMT  
 DLL: Passing an array as a parameter to a DLL

Let me close this thread off by saying thanks for all the suggestions
and I'll give a resume of my findings. Essentially, the arrays used in
VB (unlike straight variables) are NOT compatible with C arrays. They
are actually a structure called an LPSAFEARRAY (see VB4DLL.TXT for
further info). IMy workaround (writing to memory directly in the DLL)
works fine for arrays of fixed length variables (eg. ints) which was
what I was after. However, if you want to use an array of strings (for
exmaple) you must adhere to the instructions given in VB4DLL.TXT.

Thanks for all your advice,

Marc L.
Oracle Support Canada



Mon, 04 May 1998 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. String Array Passed as a Parameter to a DLL

2. passing 2Dim array to DLL swaps rows/cols (continued DLL thing)

3. Passing a 2-D array from one dll to another dll

4. passing 2Dim array to DLL swaps rows/cols (continued DLL thing)

5. Passing array from VB to DLL and returning array

6. DLL DLL DLL DLL DLL DLL

7. Passing Parameters to Non MS DLL with VBP

8. Passing and returning VB Objects as parameters to VC++ DLL

9. Passing string parameter from visual basic to c++ dll

10. Passing parameters from VB to a DLL function

11. Passing Parameters to Non MS DLL

12. VB/C++ DLL parameter passing

 

 
Powered by phpBB® Forum Software