Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BYREF in API Problem from VB to VC! 1

Status
Not open for further replies.

MagiusCHE

Programmer
Aug 23, 2001
1
IT

So.. i need to send from VB6 a STRUCTURED ARRAY into a VC++ DLL!!

i just prepared this 2 file:

VB6 EXE FILE
------------
public type secondarr
value1 as long
value2 as long
end type

publit type strarr
superarr() as secondarr
value3 as long
end type

Private Declare Function tryout Lib &quot;<myc++lib>&quot; (Byref PArr as strarr) as long

public sub PassArray()
dim myarr(0) as starr
redim myarr(0).superarr(0)
myarr(0).value3=3
myarr(0).superarr(0).value1=1
myarr(0).superarr(0).value2=2
msgbox tryout(myarr(0))
end sub

VC
----
[this is a Dynamic-DLL]
[....]
//only function and struct here

[in h]
struct secondarr
{
int value1;
int value2;
};

struct strarr
{
secondarr* superarr;
int value3;
};

[in cpp]
int tryout(strarr * myarr)
{
return strarr[0].superarr[0].value1; // <-- don't work
return stratt[0].value3; // <-- work correctly...
};


...................

so where is the problem????

i need to read a substructired array but... in this case i only operate on first level of array....

any one has other solution???????

thx....
 
This may be unintuitive, but do you think it could be because the strarr in VB consists of a UDT array and a long, while the cooresponding strarr in C consists of a pointer to a struct (not a reference to struct) and an int? I think the value secondarr* superarr in the struct strarr will contain a number instead of an actual structure reference like the tryout function is expecting to handle. In other words, you may try making struct strarr like this:

struct strarr {
secondarr superarr;
strarrptr* superarr;
int value3;
};

The first element is more like the VB UDT you've created, and there's still a pointer to the array so u can pass reference to it to the function.

Hope that helps!

-Mike Difference between a madman and a genius:
A madman uses his genius destructively,
A genius uses his madness constructively.
 
hi mike

i face the same problem as magius. i dont understand your solution to that.

struct strarr {
secondarr superarr;
strarrptr* superarr;
int value3;
};

what is that &quot;strarrptr* superarr&quot;??

i've just started using vc++

reply asap

thanks

sathish




 
I have to admit my first post is quite inaccurate, since pointers and arrays are the same thing. In MagiusCHE's code above I noticed something else in the VB function:

public sub PassArray()
dim myarr(0) as starr
redim myarr(0).superarr(0)
myarr(0).value3=3
myarr(0).superarr(0).value1=1
myarr(0).superarr(0).value2=2
msgbox tryout(myarr(0))
end sub

The statement &quot;redim myarr(0).superarr(0)&quot; may declare superarr(0) as a variant in VB, not a structure, since there's no data type specified. It may be worth a try to change it to read &quot;redim myarr(0).superarr(0) As secondarr&quot; and see if C++ will recognize it then. I would like to recall the suggestion I made above, as the rest of MagiusCHE's code seems to be fine.

rsathish, if you're asking why I recommended that he declare 2 variables with the same name (secondarr superarr, and strarrptr* superarr), it was a mistake on my part, sorry. If you're asking what the astrisk * is, it only means the variable is a pointer of that data type, not a variable of that data type. A pointer holds a memory address and allows parts of your code to change the value in that address, rather than a copy of that value. A perfect example is passing values to a function. By default this makes a copy of the variable to pass to the function and whatever changes the function makes to the variable won't be seen after the function returns:
----------------------------------
int Example(){
int MyVar = 4;
Test(MyVar);
return MyVar;
}

void Test(int Value){
Value = 5;
return;
}
-------------------------------
The function Example() will return 4, not 5, because the function didn't actually change MyVar. It changed a copy of it. Here's an example of how to fix this with a pointer:
----------------------------------
int Example(){
int MyVar = 4;
Test(&MyVar);
return MyVar;
}

void Test(int* Value){
*Value = 5;
return;
}
-------------------------------
Now when I call Test() I pass it the &quot;address of&quot; MyVar, and the function now expects to handle a pointer to an integer. Now whenever Test() changes that value, it's changing the variable that the pointer points to.

Hopefully that makes sense, and if not there's a lot of tutorials on the i-net that are better than what I can come up with in just a few minutes.

Hope that helps!

~Mike
Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
hi mike thanks for the reply.

actually i used safearray technique to solvethat problem and it worked fine.

anyway will try out ur solution too and let u know the comments.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top