|
-
Dec 18th, 2001, 04:51 AM
#1
Passing a 2D array from VB to a C++ DLL
I'm trying to pass a large two-dimensional array of Byte to a C++ DLL, but keep getting illegal operations. Below is the code:
(Note: I need to pass it by reference because 1) it's very large and 2) I need the function to modify it.)
in the DLL:
Header:
extern "C" void __export Function (char**);
Program:
void __export Function (char** a) {
for (int x=0; x<=1000;x++)
for (int y=0;y<=1000;y++)
a[x][y]= // whatever calculations using a[x][y]
return;
}
--------
then in VB:
Declaration:
declare sub Function lib "whatever.dll" (byref a as any)
In code:
dim MyArray (0 to 999,0 to 999) as byte
'later...
Function MyArray (0,0)
-------
This code will work if the array is one-dimensional, but not two, so I must be doing something wrong, but I can't seem to find the error. I'd really appreciate any help on this one.
P.S. Another solution could be to make a one-dimensional array of pointers to one-dimensional arrays (i.e. char* a[1000]), but is that possible in VB???
-
Dec 18th, 2001, 08:30 AM
#2
I THINK you can do something like this:
Code:
void __export Function (char* a){...}
...
Dim myArr(10, 10);
Function myArr(10, 10);
What you are doing is passing in a pointer, but telling the DLL that you are passing a pointer to a pointer, not a 2D array. Inside of the DLL function, you need to use something like:
Code:
for(int i = 0; i < 10; ++i) {
for(int j = 0; j < 10; ++j) {
a[i*10+j] = x * j; } }
All of that SHOULD work, but Its all off the top of my head, so... Also, VB DLL functions must be __stdcall. Just something else to check out.
Z.
-
Dec 22nd, 2001, 02:18 PM
#3
Ok, that's something I've done for quite a while...
As Zaei said, use __stdcall.
Any VB array passed to C/C++ is one-dimensional. You can't use it like a two-dimensional array. (To act like yo could, use the thing Zaei has in the for-loop)
The value passed to C is char* (better would be "signed char*", since not all compilers automatically do this).
In VB, declare it as:
VB Code:
private declare sub func lib "dll.dll" (ByVal ar as long)
'call it:
Call func(VarPtr(YourArray(1,1)))
'provided that you have option base 1
And don't forget that arrays indexing starts at 0 in C
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|