Declaring an array of pointers in Visual Basic (and putting and getting data from it) 
Author Message
 Declaring an array of pointers in Visual Basic (and putting and getting data from it)

Declaring an array of pointers in Visual Basic (and putting and getting data
from it)?

I have not succeeded in solving a problem calling a DLL from Visual Basic.
ANY help appreciated especially if it is impossible, or maybe it isn't???

The C declaration looks like this:

#define PBRETURN long int

PBRETURN WINAPI
pbEnrollWiz(OUT void  *templ[10]);

As I understand the parameter that receives data from the function is an
array of pointers. I also know that each entry in the array is a pointer to
either NULL or the pointer is referencing memory allocated by malloc(1868);
I also know that the DLL and function will use the allocated memory to write
binary data for each pointer (of the 10 possible) that is not NULL.

In Visual Basic I therefore need to create an array with 10 entries. Each
entry should be a pointer to a memory area of 1868 bytes or point to NULL
(no memory allocated). It's the duty of program to decide with of the 10
entries I want to receive data in.

There are several difficulties when trying to do this in VB:
1.    How do I declare an array of pointers? Can this even be done?
2.    How do I make sure that some pointers should be NULL and at least one
pointer I need to reference to an area of 1868 bytes that is allocated with
malloc(1868)? There is no malloc() in Visual Basic.
3.    The data received back is not text it's binary data. How can I store
this? This might be a clue when building the array. Byte datatype in Visual
Basic only allows me to store 1 byte. Maybe an array of bytes in a Type
Declaration?

I really really really hope someone has any clues. If you have anything to
add please do so. You do not need to come up with a complete solution.
Sometimes a few tips might help. I have some tips already but since
everything I try result in a DLL-crash I want to clear my mind totally and
start over with fresh idas.

Down below is an example in C calling the Function as described above.

PBRETURN ret_val;
void *enrolled_template[10] = {NULL, NULL, NULL, NULL, NULL,
                                 NULL, NULL, NULL, NULL, NULL}; // All
pointers point to NULL

enrolled_template[6] = malloc(1868);  // Pointer 6 is initialized with
malloc() to point to an area large enough for function pbEnrollWiz
ret_val = pbEnrollWiz(enrolled_template); // pbEnrollWiz will detect that
Pointer 6 has allocated space and will write back some data to the memory



Mon, 12 Aug 2002 03:00:00 GMT  
 Declaring an array of pointers in Visual Basic (and putting and getting data from it)


Quote:
> Declaring an array of pointers in Visual Basic (and putting and getting
data
> from it)?

I'm afraid your'e going to have some problems with this, as VB doesn't
provide any built in support for pointers, dynamicly allocated memory or
directly accessing memory. All you can do is get a pointer to IUnknown of an
object, (ObjPtr), a string (StrPtr), and to a variant (VarPtr). Apart from
that, you can store pointers in longs and pass them to DLLs. Thats it.

Stephen



Mon, 12 Aug 2002 03:00:00 GMT  
 Declaring an array of pointers in Visual Basic (and putting and getting data from it)
You would probably have better luck using separate parameters for each value
instead of passing an array.  In C++ you could do

long WINAPI MyFunc(char *rtn1,char *rtn2,char *rtn3... char *rtn10)
{
    build the 10 array entries
    memcpy(rtn1,the first array entry,1868);
    ....
   memcpy(rtn10,the 10th array entry,1868);
   return 1;

Quote:
}

In VB you could do

private declare myfunc lib "mylib.dll" (byval rtn1$,byval rtn2$,byval rtn3$
... byval rtn10$) as long

Then when you call the function in VB do:

dim rtn1$,rtn2$,rtn3$ ...,rtn10$

rtn1=space(1869)
rtn2=space(1869)
.... rtn10=space(1869)
if MyFunc(rtn1,rtn2 ...rtn10)<>0 then
    now you have your 10 1868 byte arrays in rtn1..rtn10.
endif

I can't remember if I've tried it before, but you may be able to do:
dim r$(10)
for i=1 to 10: r(i)=space(1869) : next
if MyFunc(r(1),r(2),r(3),...r(10)) <>0 then
    for i=1 to 10
        dowhateverwith r(i)
    next
endif

Another (cleaner) possibility:

In C++ do
    for (int i=0;i<10;i++,ix+=1868)
        memcpy(rtnValue+ix, array[i], 1868)

and in VB do
    dim r$
    r = space(1868*10)
    MyFunc r
    for i=1 to 1868*10 step 1868
        dowhateverwith mid(r,i,1868)

Good luck -Dwain


Quote:
> Declaring an array of pointers in Visual Basic (and putting and getting
data
> from it)?

> I have not succeeded in solving a problem calling a DLL from Visual Basic.
> ANY help appreciated especially if it is impossible, or maybe it isn't???

> The C declaration looks like this:

> #define PBRETURN long int

> PBRETURN WINAPI
> pbEnrollWiz(OUT void  *templ[10]);

> As I understand the parameter that receives data from the function is an
> array of pointers. I also know that each entry in the array is a pointer
to
> either NULL or the pointer is referencing memory allocated by
malloc(1868);
> I also know that the DLL and function will use the allocated memory to
write
> binary data for each pointer (of the 10 possible) that is not NULL.

> In Visual Basic I therefore need to create an array with 10 entries. Each
> entry should be a pointer to a memory area of 1868 bytes or point to NULL
> (no memory allocated). It's the duty of program to decide with of the 10
> entries I want to receive data in.

> There are several difficulties when trying to do this in VB:
> 1.    How do I declare an array of pointers? Can this even be done?
> 2.    How do I make sure that some pointers should be NULL and at least
one
> pointer I need to reference to an area of 1868 bytes that is allocated
with
> malloc(1868)? There is no malloc() in Visual Basic.
> 3.    The data received back is not text it's binary data. How can I store
> this? This might be a clue when building the array. Byte datatype in
Visual
> Basic only allows me to store 1 byte. Maybe an array of bytes in a Type
> Declaration?

> I really really really hope someone has any clues. If you have anything to
> add please do so. You do not need to come up with a complete solution.
> Sometimes a few tips might help. I have some tips already but since
> everything I try result in a DLL-crash I want to clear my mind totally and
> start over with fresh idas.

> Down below is an example in C calling the Function as described above.

> PBRETURN ret_val;
> void *enrolled_template[10] = {NULL, NULL, NULL, NULL, NULL,
>                                  NULL, NULL, NULL, NULL, NULL}; // All
> pointers point to NULL

> enrolled_template[6] = malloc(1868);  // Pointer 6 is initialized with
> malloc() to point to an area large enough for function pbEnrollWiz
> ret_val = pbEnrollWiz(enrolled_template); // pbEnrollWiz will detect that
> Pointer 6 has allocated space and will write back some data to the memory



Mon, 12 Aug 2002 03:00:00 GMT  
 Declaring an array of pointers in Visual Basic (and putting and getting data from it)
true, you can not declare (or use) pointers in VB, but if you are using VB6
you can use the AddressOf operator to pass the address to the C function.
you can look it up at MSDN online for more info on it.

Mr. X

Quote:



>> Declaring an array of pointers in Visual Basic (and putting and getting
>data
>> from it)?

>I'm afraid your'e going to have some problems with this, as VB doesn't
>provide any built in support for pointers, dynamicly allocated memory or
>directly accessing memory. All you can do is get a pointer to IUnknown of
an
>object, (ObjPtr), a string (StrPtr), and to a variant (VarPtr). Apart from
>that, you can store pointers in longs and pass them to DLLs. Thats it.

>Stephen



Tue, 13 Aug 2002 03:00:00 GMT  
 Declaring an array of pointers in Visual Basic (and putting and getting data from it)

Quote:

> Declaring an array of pointers in Visual Basic (and putting and getting data
> from it)?

> I have not succeeded in solving a problem calling a DLL from Visual Basic.
> ANY help appreciated especially if it is impossible, or maybe it isn't???

> The C declaration looks like this:

> #define PBRETURN long int

> PBRETURN WINAPI
> pbEnrollWiz(OUT void  *templ[10]);

[big snip of difficulties had already]

If the C interface is frozen, you have an impossible
task.  If that interface was intended to be useful to
the VB programmer, the person who wrote it needs
an education.  If that person sold you the DLL with
the claim it was usable from VB, get a refund.

The basic problem (pun intended) is that VB is a
programmer-proof environment.  Untyped pointers
are simply not allowed and the only "untyped" type
is a type-tagged data object.  (Variant)  Arrays are
represented using a structure with appended bounds
and a dynamically sized data block, (SAFEARRAY),
except for fixed size arrays embedded in UDT's.

If you have control of that C interface, redefine it to
be compatible with VB data types.  You have much
more flexibility dealing with those from the C end
than dealing with untyped data from the VB end.

If you do not have control of the interface, VB is
simply the wrong tool for running that DLL.

--
Larry Brasfield
Above opinions may be mine alone.



Tue, 13 Aug 2002 03:00:00 GMT  
 Declaring an array of pointers in Visual Basic (and putting and getting data from it)
Well, I like using pointers in C, but I don't like them in VB.
Anyway, nearly everything is possible...
You can use CopyMemory API to move memory around.

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
(Destination As Any, Source As Any, ByVal Length As Long)

To copy the memory where a pointer points to, pass the pointer ByVal.

Hope this helps

Alex



Quote:
> > Declaring an array of pointers in Visual Basic (and putting and getting
data
> > from it)?

> > I have not succeeded in solving a problem calling a DLL from Visual
Basic.
> > ANY help appreciated especially if it is impossible, or maybe it
isn't???

> > The C declaration looks like this:

> > #define PBRETURN long int

> > PBRETURN WINAPI
> > pbEnrollWiz(OUT void  *templ[10]);

> [big snip of difficulties had already]

> If the C interface is frozen, you have an impossible
> task.  If that interface was intended to be useful to
> the VB programmer, the person who wrote it needs
> an education.  If that person sold you the DLL with
> the claim it was usable from VB, get a refund.

> The basic problem (pun intended) is that VB is a
> programmer-proof environment.  Untyped pointers
> are simply not allowed and the only "untyped" type
> is a type-tagged data object.  (Variant)  Arrays are
> represented using a structure with appended bounds
> and a dynamically sized data block, (SAFEARRAY),
> except for fixed size arrays embedded in UDT's.

> If you have control of that C interface, redefine it to
> be compatible with VB data types.  You have much
> more flexibility dealing with those from the C end
> than dealing with untyped data from the VB end.

> If you do not have control of the interface, VB is
> simply the wrong tool for running that DLL.

> --
> Larry Brasfield
> Above opinions may be mine alone.




Tue, 13 Aug 2002 03:00:00 GMT  
 Declaring an array of pointers in Visual Basic (and putting and getting data from it)
From the top of my head (code based in C sample):

'PBRETURN WINAPI
'pbEnrollWiz(OUT void  *templ[10]);
'I assume PBRETURN is a long
Declare Function pbEnrollWiz lib "IDontKnow.dll" (lPointers() As Long) As
Long

Dim abPosition1() As Byte
.
.
.
Dim abPosition10() As Byte

'void *enrolled_template[10] = {NULL, NULL, NULL, NULL, NULL,NULL, NULL,
NULL, NULL, NULL}; // All
Dim abaArrayToPassToTheFunction(1 To 10) As long

'enrolled_template[6] = malloc(1868);  // Pointer 6 is initialized with
'malloc() to point to an area large enough for function pbEnrollWiz
Redim abPosition6(1868)

abaArrayToPassToTheFunction(1) = 0
.
.
abaArrayToPassToTheFunction(6) = VarPtr(abPosition6(0))
.
.
abaArrayToPassToTheFunction(10) =

Quote:
>ret_val = pbEnrollWiz(enrolled_template); // pbEnrollWiz will detect that
>Pointer 6 has allocated space and will write back some data to the memory

lRet = pbEnrollWiz(abaArrayToPassToTheFunction())

Let me know if it worked.

Martin

Quote:

>Declaring an array of pointers in Visual Basic (and putting and getting
data
>from it)?

>I have not succeeded in solving a problem calling a DLL from Visual Basic.
>ANY help appreciated especially if it is impossible, or maybe it isn't???

>The C declaration looks like this:

>#define PBRETURN long int

>PBRETURN WINAPI
>pbEnrollWiz(OUT void  *templ[10]);

>As I understand the parameter that receives data from the function is an
>array of pointers. I also know that each entry in the array is a pointer to
>either NULL or the pointer is referencing memory allocated by malloc(1868);
>I also know that the DLL and function will use the allocated memory to
write
>binary data for each pointer (of the 10 possible) that is not NULL.

>In Visual Basic I therefore need to create an array with 10 entries. Each
>entry should be a pointer to a memory area of 1868 bytes or point to NULL
>(no memory allocated). It's the duty of program to decide with of the 10
>entries I want to receive data in.

>There are several difficulties when trying to do this in VB:
>1.    How do I declare an array of pointers? Can this even be done?
>2.    How do I make sure that some pointers should be NULL and at least one
>pointer I need to reference to an area of 1868 bytes that is allocated with
>malloc(1868)? There is no malloc() in Visual Basic.
>3.    The data received back is not text it's binary data. How can I store
>this? This might be a clue when building the array. Byte datatype in Visual
>Basic only allows me to store 1 byte. Maybe an array of bytes in a Type
>Declaration?

>I really really really hope someone has any clues. If you have anything to
>add please do so. You do not need to come up with a complete solution.
>Sometimes a few tips might help. I have some tips already but since
>everything I try result in a DLL-crash I want to clear my mind totally and
>start over with fresh idas.

>Down below is an example in C calling the Function as described above.

>PBRETURN ret_val;
>void *enrolled_template[10] = {NULL, NULL, NULL, NULL, NULL,
>                                 NULL, NULL, NULL, NULL, NULL}; // All
>pointers point to NULL

>enrolled_template[6] = malloc(1868);  // Pointer 6 is initialized with
>malloc() to point to an area large enough for function pbEnrollWiz
>ret_val = pbEnrollWiz(enrolled_template); // pbEnrollWiz will detect that
>Pointer 6 has allocated space and will write back some data to the memory



Wed, 14 Aug 2002 03:00:00 GMT  
 Declaring an array of pointers in Visual Basic (and putting and getting data from it)
You are absolutely right! I thank you a lot. It was not 100% accurate but
99% at least:-). But you should have all credit for the success. VarPtr()
opened up a lot of possibilites. Too bad VarPtr() is not documented in VB so
I took some time reading about how the function and it's siblings work.
There are more hidden functions for you guys to use when dealing with
strings and objects or other complex datastructures. I hade to make a few
changes before it worked but you really gave me the clue in how to bend the
memory to the max in Visual Basic. I was so e{*filter*}d I forgot to go to sleep
last night efter you gave me the tip. Now I have 100% functionality of my
application. I can tell you that the API that I had to call required more
use of VarPtr() so I was really thrilled when I was reminded of it. I have
not really used VB this intense since 2 years ago but I'm pretty sure that I
have been using VarPtr() before when making a callback function with the
AddressOf operator among others.
---------------
For you new into VB make sure that when you define arrays check if Option
Base is 0 or 1. It defaults to 0 so if you write Dim x(10) you get one array
with 11 elements and not 10 as you might think. This is important to know so
you don't unaccidentally overwrite something in the memory and cause a
program crash. It happened to me as I was working with VarPtr().
---------------

Anyway this is how it should look like:

Public Declare MyFunction lib="libraryname" (ByVal lPointers As Long) As
Long
// Important to declare as ByVal if not VB will send the reference since
ByRef is default (this was one of the things I have to change) and also I
decided not to write lPointers() as an array it did not work so I changed to
sending the pointer to the array instead as you can see down below.
---------------

// I need to declare the store of bytes
Dim byteStore() As Byte    // Byte store
Dim myArray() As Long    // The array which will contain references to the
byte stores defined below
Dim lReturn As Long
Redim byteStore(1868 -1)    // Subract one since option base is 0 so we get
an interval of 0-1867
Redim myArray(10 - 1)        // I want 10 entries (one for each finger on
your 2 hands)

myArray(6) = VarPtr(byteStore(0))    // Make sure that array will include
the pointer to the byteStore before calling function
                                                        // All other entries
in myArray will be 0 and not point to anything

lReturn = MyFunction(VarPtr(myArray(0)))    // This is the call and the API
will be notified of the address of myArray
                                                                        //
And find that index 6 has a pointer to byteStore which contains 1868 bytes
                                                                        //
ready to recieve data

                                                                        //
Once again Martin this I had to change to VarPtr() again
                                                                        //
since the array did not seem
                                                                        //
to be acknowledged by the API.

Once again, thank you Martin for you excellent knowledge!
---------------
Hans Rasmussen

Quote:

> Declaring an array of pointers in Visual Basic (and putting and getting data
> from it)?

> I have not succeeded in solving a problem calling a DLL from Visual Basic.
> ANY help appreciated especially if it is impossible, or maybe it isn't???

> The C declaration looks like this:

> #define PBRETURN long int

> PBRETURN WINAPI
> pbEnrollWiz(OUT void  *templ[10]);

> As I understand the parameter that receives data from the function is an
> array of pointers. I also know that each entry in the array is a pointer to
> either NULL or the pointer is referencing memory allocated by malloc(1868);
> I also know that the DLL and function will use the allocated memory to write
> binary data for each pointer (of the 10 possible) that is not NULL.

> In Visual Basic I therefore need to create an array with 10 entries. Each
> entry should be a pointer to a memory area of 1868 bytes or point to NULL
> (no memory allocated). It's the duty of program to decide with of the 10
> entries I want to receive data in.

> There are several difficulties when trying to do this in VB:
> 1.    How do I declare an array of pointers? Can this even be done?
> 2.    How do I make sure that some pointers should be NULL and at least one
> pointer I need to reference to an area of 1868 bytes that is allocated with
> malloc(1868)? There is no malloc() in Visual Basic.
> 3.    The data received back is not text it's binary data. How can I store
> this? This might be a clue when building the array. Byte datatype in Visual
> Basic only allows me to store 1 byte. Maybe an array of bytes in a Type
> Declaration?

> I really really really hope someone has any clues. If you have anything to
> add please do so. You do not need to come up with a complete solution.
> Sometimes a few tips might help. I have some tips already but since
> everything I try result in a DLL-crash I want to clear my mind totally and
> start over with fresh idas.

> Down below is an example in C calling the Function as described above.

> PBRETURN ret_val;
> void *enrolled_template[10] = {NULL, NULL, NULL, NULL, NULL,
>                                  NULL, NULL, NULL, NULL, NULL}; // All
> pointers point to NULL

> enrolled_template[6] = malloc(1868);  // Pointer 6 is initialized with
> malloc() to point to an area large enough for function pbEnrollWiz
> ret_val = pbEnrollWiz(enrolled_template); // pbEnrollWiz will detect that
> Pointer 6 has allocated space and will write back some data to the memory



Fri, 16 Aug 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Declaring an array of pointers in Visual Basic (and putting and getting data from it)

2. BASIC, VISUAL BASIC, C++, DELPHI SOFTWARE FRO CHEAP PRICE

3. Declaring C-Language Union and Bit-field data types in Visual Basic

4. Declaring C-Language Union and Bit-field data types in Visual Basic

5. How to run my dos batch file fro visual basic

6. Getting data from Visual Basic 2005 to Excel

7. Accessing Visual C++ pointers with Visual Basic variables

8. Fun with APIs- Getting an array of records with pointers to strings

9. Getting an array of records with pointers to strings from an API

10. Getting file info using a file list box and putting in an array

11. Getting Types that were Put from an Array

12. Getting at data in structure returned as a long pointer (Win API)

 

 
Powered by phpBB® Forum Software