Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

c++ dll being called from a vb program

Posted on 2002-06-06
12
Medium Priority
?
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 101

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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

Sometimes Administrators rights are not enough. These cases call for the SYSTEM account. The process in this article outlines the steps required to execute commands using the SYSTEM account.
IF you are either unfamiliar with rootkits, or want to know more about them, read on ....
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

604 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