VB calling C++ DLL returning an array?? 
Author Message
 VB calling C++ DLL returning an array??

Hello-

I am having problems returing an array in a VB application when I a function
in a c++ DLL.  I get a referenced memory error.  I know the DLL is being
called correctly, but an error occurs at the point of returning the array.
Here is a snipit of code from the VB and VC++ applications.  Thanks in
advance for any help!

'*********************VB6.0
Code*****************************************************************
Dim RetArray() As Integer

ReDim RetArray(intNumBytes)                'redim the array that is expected
RetArray = ReturnArray(intNumBytes)    'call function in dll, return
type=array of integers
'***************************************************************************
*************************

//*********************VC++6.0
Code*************************************************************

int * Add::ReturnArray(int numberbytes)
{
 int Value[100];

 for(short int i=0;i<numberbytes;i++)
 {
  Value[i] = i;
 }

return(Value);
//**************************************************************************
**************************

Best Regards,
Matt Van Bergen



Mon, 21 Jan 2002 03:00:00 GMT  
 VB calling C++ DLL returning an array??
I'm pretty sure this code won't compile in VB5. I haven't tested it too
extensively with VB6. I guess there could be something new. But for
starters, how can you call a C++ method from VB? I'd have thought the name
mangling alone would be a major pain. Also, unless it is a static function,
it needs the hidden "this" pointer to be passed, which VB obviously do.

Beyond that, VB uses SAFEARRAYs for arrays. I have no idea how you declared
the function you are calling but if VB is expecting it to return an array,
it'd be expecting a SAFEARRAY and not just a pointer to an array of ints.

Further, the DLL function declares the array on the stack and that data is
invalid as soon as the function returns. It may work once or twice but you
are asking for trouble here.

If that's not enough, C++ ints are 32-bits and VB Integers are 16-bit so you
also have a mismatch there.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com


Quote:
> Hello-

> I am having problems returing an array in a VB application when I a
function
> in a c++ DLL.  I get a referenced memory error.  I know the DLL is being
> called correctly, but an error occurs at the point of returning the array.
> Here is a snipit of code from the VB and VC++ applications.  Thanks in
> advance for any help!

> '*********************VB6.0
> Code*****************************************************************
> Dim RetArray() As Integer

> ReDim RetArray(intNumBytes)                'redim the array that is
expected
> RetArray = ReturnArray(intNumBytes)    'call function in dll, return
> type=array of integers

'***************************************************************************

- Show quoted text -

Quote:
> *************************

> file://*********************VC++6.0
> Code*************************************************************

> int * Add::ReturnArray(int numberbytes)
> {
>  int Value[100];

>  for(short int i=0;i<numberbytes;i++)
>  {
>   Value[i] = i;
>  }

> return(Value);

file://*********************************************************************
*****

- Show quoted text -

Quote:
> **************************

> Best Regards,
> Matt Van Bergen



Mon, 21 Jan 2002 03:00:00 GMT  
 VB calling C++ DLL returning an array??
Matt,

Try something like this:

Private Sub MyArrExample lib "MyDLL.dll" (byval NumItems as Integer, ByRef
RetArray As Integer)

and use something like:

    Redim RetArr(14) as Integer
    MyArrExample 15, RetArr(0)

The C++ end would be something like:

extern "C" void WINAPI MyArrExample(short iNumItems, short *lpsRetArray)
{
    short            i;

    for (i = 0; i < iNumItems; i++)
        lpsRetArray[i] = i;

Quote:
}

If it is really important for you to return an array, you can do it.  You
use a Variant in VB and a VARIANT in C.  It is fairly messy though and I
don't have an example immediately to hand.  Post back if you really want to
see this and I'll try and dig up an example.

HTH

Peter


Quote:
> Hello-

> I am having problems returing an array in a VB application when I a
function
> in a c++ DLL.  I get a referenced memory error.  I know the DLL is being
> called correctly, but an error occurs at the point of returning the array.
> Here is a snipit of code from the VB and VC++ applications.  Thanks in
> advance for any help!

> '*********************VB6.0
> Code*****************************************************************
> Dim RetArray() As Integer

> ReDim RetArray(intNumBytes)                'redim the array that is
expected
> RetArray = ReturnArray(intNumBytes)    'call function in dll, return
> type=array of integers

'***************************************************************************

- Show quoted text -

Quote:
> *************************

> file://*********************VC++6.0
> Code*************************************************************

> int * Add::ReturnArray(int numberbytes)
> {
>  int Value[100];

>  for(short int i=0;i<numberbytes;i++)
>  {
>   Value[i] = i;
>  }

> return(Value);

file://*********************************************************************
*****

- Show quoted text -

Quote:
> **************************

> Best Regards,
> Matt Van Bergen



Tue, 22 Jan 2002 03:00:00 GMT  
 VB calling C++ DLL returning an array??
Try this URL:
http://support.microsoft.com/support/kb/articles/Q
177/2/18.ASP


Quote:
> Matt,

> Try something like this:

> Private Sub MyArrExample lib "MyDLL.dll" (byval

NumItems as Integer, ByRef
Quote:
> RetArray As Integer)

> and use something like:

>     Redim RetArr(14) as Integer
>     MyArrExample 15, RetArr(0)

> The C++ end would be something like:

> extern "C" void WINAPI MyArrExample(short

iNumItems, short *lpsRetArray)
Quote:
> {
>     short            i;

>     for (i = 0; i < iNumItems; i++)
>         lpsRetArray[i] = i;
> }

> If it is really important for you to return an

array, you can do it.  You
Quote:
> use a Variant in VB and a VARIANT in C.  It is

fairly messy though and I
Quote:
> don't have an example immediately to hand.

Post back if you really want to
Quote:
> see this and I'll try and dig up an example.

> HTH

> Peter



> > Hello-

> > I am having problems returing an array in a

VB application when I a
Quote:
> function
> > in a c++ DLL.  I get a referenced memory

error.  I know the DLL is being
Quote:
> > called correctly, but an error occurs at the

point of returning the array.
Quote:
> > Here is a snipit of code from the VB and VC++

applications.  Thanks in
Quote:
> > advance for any help!

> > '*********************VB6.0

Code**********************************************
*******************
Quote:
> > Dim RetArray() As Integer

> > ReDim RetArray

(intNumBytes)                'redim the array
that is
Quote:
> expected
> > RetArray = ReturnArray(intNumBytes)    'call

function in dll, return
Quote:
> > type=array of integers

> '***********************************************

****************************
Quote:
> > *************************

> > file://*********************VC++6.0

Code**********************************************
***************
Quote:

> > int * Add::ReturnArray(int numberbytes)
> > {
> >  int Value[100];

> >  for(short int i=0;i<numberbytes;i++)
> >  {
> >   Value[i] = i;
> >  }

> > return(Value);

file://*******************************************
**************************

Quote:
> *****
> > **************************

> > Best Regards,
> > Matt Van Bergen

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


Mon, 18 Feb 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. VB-Error when calling c++ DLL that returns a string

2. VB 5.0 calling C++ 5.0 DLL that calls winsock.dll

3. Returning an array of UDTs from a C++ DLL

4. Holding a pointer to a C++ object being returned from a DLL call

5. Passing array from VB to DLL and returning array

6. Problem with returning array from a VB COM+ object to C++

7. C++ DLL crashing on Return to VB

8. Calling a C++ DLL function which takes an C++ object as parameter

9. Help How to get MS C++ DLL function to return string to VB

10. Returning strings from VB COM components to C++ Isapi DLL

11. DLL-Interface Borland C++ to VB - array problems

12. How to pass a string Array from VB to a C++ DLL

 

 
Powered by phpBB® Forum Software