Solved

c++ dll being called from a vb program

Posted on 2002-06-06
12
338 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

OnPage: Incident management and secure messaging on your smartphone
Many businesses neglect disaster recovery and treat it as an after-thought. I can tell you first hand that data will be lost, hard drives die, servers will be hacked, and careless (or malicious) employees can ruin your data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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…

733 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