Solved

c++ dll being called from a vb program

Posted on 2002-06-06
12
339 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
Threat Trends for MSPs to Watch

See the findings.
Despite its humble beginnings, phishing has come a long way since those first crudely constructed emails. Today, phishing sites can appear and disappear in the length of a coffee break, and it takes more than a little know-how to keep your clients secure.

 
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Keystroke loggers have been around for a very long time. While the threat is old, some of the remedies are new!
The conference as a whole was very interesting, although if one has to make a choice between this one and some others, you may want to check out the others.  This conference is aimed mainly at government agencies.  So it addresses the various compli…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

726 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