Solved

c++ dll being called from a vb program

Posted on 2002-06-06
12
337 Views
Last Modified: 2012-05-04
I am developing a c++ dll which will be called by a vb program. The c++ dll has classes which are mirrorred as structures in vb. For example for the c++  class Asset
class Asset
{
     double val1;
     double val2;
     char str[15];
//and so on
}
class Bond : public Asset
{
     double val3;
     double val4;
     Bond();
     ~Bond();
}

struct Trust
{
     Bond objBond[3]
     //....
}TDS;

there is a mirror structure in vb like
Type ASSET
val1 as Double
val2 as Double
str as String * 15
End Type

Type BOND
structAsset as ASSET
val3 as double
val4 as double
End Type

Type TRUST
structBond() as BOND
End Type

Now when the cpp dll exports the MyProc function as void  __stdcall  MyProc (TDS &lpStruct)
which VB calls using Public Declare Sub MyProc Lib "D:\MyProj\MyDll.dll" (lpStruct As TRUST)
But when I populate the Bond structure and pass it to the c++ dll  using
////////////////////////////////////////
Dim obj As TRUST

'setting bond values
   Dim i As Integer
    ReDim obj.structBond(2)
    For i = 0 To 2
    obj.structBond(i).val3 = 1000 + i
    obj.structBond(i).val4 = 6.5
   Next i
   
    'passing the structure to MyProc in cpp dll
   MyProc obj
////////////////////////////////////////
the values in c++ dll is shown as zero. One requirement I understood is that the struct being passed should be identical (esp in terms of memory allocated) in vb and c++ (as a bitwise copy is done,  I think). Now while debugging c++ program I see in the watch window that in the Bond object objBond[0], there is a memory allocated like __vfptr 0x001364e8 and under that [0] 0x00000001. This memory is not mirrored in vb, so could this be the problem? What am I doing wrong? This is the very first VB program that I have ever attempted and am less than a beginner in VB. Experts, please advice.

0
Comment
Question by:aparna-learner
12 Comments
 
LVL 23

Expert Comment

by:Tim Holman
ID: 7058927
First bit of advice would be to delete this and place it in the VB forum !!!
0
 

Author Comment

by:aparna-learner
ID: 7059117
Thanks. I have done that.
0
 

Author Comment

by:aparna-learner
ID: 7059134
Thanks. I have done that.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 100

Expert Comment

by:mlmcc
ID: 7059212
Have you tried giving the val1 and val2 objects a value in VB before calling the C++ program?

mlmcc
0
 

Expert Comment

by:HobbitHouse
ID: 7059258
don't use a class in c++, just use a structure.  I'm not positive but I think classes have some overhead that defeats the linkage and I KNOW that I've read somewhere that passing classes from C++ DLLs to VB doesn't work.

I know from direct experience that passing structures is no problem at all.
0
 

Expert Comment

by:HobbitHouse
ID: 7059264
FYI I do not know the truth of the statement about classes not being passable, just that I know I've read it somewhere.  The person who wrote it could be wrong.
0
 

Author Comment

by:aparna-learner
ID: 7059326
Thanks for the responses.
The val1 and val2 are not objects but variables for obj.structBond(i) that I am setting in vb before passing them to vc through calling MyProc.
I require to have classes in c++ and can't have mirror structures, which is at the root of my problem.
One more thing is that I found out that __vfptr is the memory allocated for the virtual function that is defined in the Bond class in c++. Now does this cause the memory allocations in vb and c++ to vary (and hence not mirror each other?)
0
 

Author Comment

by:aparna-learner
ID: 7059479
Thanks for the responses.
The val1 and val2 are not objects but variables for obj.structBond(i) that I am setting in vb before passing them to vc through calling MyProc.
I require to have classes in c++ and can't have mirror structures, which is at the root of my problem.
One more thing is that I found out that __vfptr is the memory allocated for the virtual function that is defined in the Bond class in c++. Now does this cause the memory allocations in vb and c++ to vary (and hence not mirror each other?)
0
 

Author Comment

by:aparna-learner
ID: 7059505
Thanks for the responses.
The val1 and val2 are not objects but variables for obj.structBond(i) that I am setting in vb before passing them to vc through calling MyProc.
I require to have classes in c++ and can't have mirror structures, which is at the root of my problem.
One more thing is that I found out that __vfptr is the memory allocated for the virtual function that is defined in the Bond class in c++. Now does this cause the memory allocations in vb and c++ to vary (and hence not mirror each other?)
0
 

Accepted Solution

by:
aparna-learner earned 0 total points
ID: 9025308
I am answering my own question here under:
The classes in c++ can indeed be used to get the values being passed by VB structure. But the condition is that it should not be defined as a _virtual_ class. In this case, there is the virtual pointer which is additionally allocated on c++ side which has not corresponding memory allocation on VB side, which will result in memory block mismatch. Otherwise, structs in vb can pass values to c++ classes and vice versa.
Thanks. I am closing this question.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Knowing where your website is hosted is as important as the features you receive, the monthly fee, and the support you receive. Due diligence should be done when choosing your next hosting provider.
The related questions "How do I recover the passwords for my Q-See DVR" and "How can I reset my Q-See DVR to eliminate a password" are seen several times a week.  Here we discuss the grim reality of the situation.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question