Solved

c++ dll being called from a vb program

Posted on 2002-06-06
12
325 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
Comment Utility
First bit of advice would be to delete this and place it in the VB forum !!!
0
 

Author Comment

by:aparna-learner
Comment Utility
Thanks. I have done that.
0
 

Author Comment

by:aparna-learner
Comment Utility
Thanks. I have done that.
0
 
LVL 100

Expert Comment

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

mlmcc
0
 

Expert Comment

by:HobbitHouse
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Expert Comment

by:HobbitHouse
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

SHARE your personal details only on a NEED to basis. Take CHARGE and SECURE your IDENTITY. How do I then PROTECT myself and stay in charge of my own Personal details (and) - MY own WAY...
This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now