Connecting Tech Pros Worldwide Forums | Help | Site Map

Return Array from DLL

Newbie
 
Join Date: Jun 2007
Posts: 1
#1: Jun 18 '07
I'm trying to return an array from a DLL.

Expand|Select|Wrap|Line Numbers
  1. extern "C" __declspec (dllexport) BYTE* _stdcall ReturnBytes(void)
  2. {
  3.     BYTE v[5];
  4.     BYTE * p;
  5.  
  6.     p = v;
  7.  
  8.     *p++ = 9;
  9.     *p++ = 0;
  10.     *p++ = 2;
  11.     *p++ = 1;
  12.     *p++ = 0;
  13.  
  14.     return p;
  15. }

How do I get these values when calling the DLL from VB.NET. Can I use the Marshal class to recall the pointer?

Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,370
#2: Jun 18 '07

re: Return Array from DLL


You can't do this.
Quote:

Originally Posted by SamOlson

BYTE v[5];
BYTE * p;

p = v;
...
return p;

The array v is a local variable. It get's destroyed when the function completes.

You have to allocate your array on the heap. Then you must remember to delete later. I suggest you use a handle class (smart pointer) for this. There is an article about this in the C/C++ Articles Forum.
Reply