Solved

C++ and Visual Basic

Posted on 1998-11-01
14
189 Views
Last Modified: 2013-11-25
Is there an easy way to call a C++ or MFC DLL class or function from Visual Basic. I know I can look at the DLL through QuickView and get its mangled name. Is that the only way?

Also, it seems that even through QuickView, I don't see the mangled names for some classes in the DLLs. How do I call these?
0
Comment
Question by:flfmdll
[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
  • 7
  • 7
14 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1176715
You can use the extern "C" declaration to disable name mangling on a function, for example

extern "C" __declspec(dllexport) int IncrementFunction(int i)
{
   return i+1;
};

Witht he extern C"C" the function will be exported without its mangled name.
0
 

Author Comment

by:flfmdll
ID: 1176716
I know you are probably addressing what you see as the question. Your answers are always good. But I'm unclear how this helps if I'm using a class or in the cases where I don't see the names in QuickView.

Incidentally, I'm using VC6.0. I don't know if things are any different than when I was using VC5.0.

Also, I should have said I know about extern. But I guess I don't see how that helps when I'm using classes or .cpp extensions.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176717
>> I don't see the mangled names for some classes in the DLLs.
Class names won't be exported, it is class member functions that are exported. (I should say may be exported, inline functions might not be exported and and obviously functions that don't have the __declspec(dllexport) won't be exported.)

Note that (as far as I know) VB cannot call non-static C++ member functions.  Non-static member functions are called using the "this call" calling convention.  Only C++ supports this (I goess other OO languages could, but I don't think any do.)   There are ways around this but they aren't really pleasant.

Let me know if you have any questions.


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:flfmdll
ID: 1176718
I'll repeat back what I understand...

I should construct my classes normally except the public functions will also have the dllexport macro in front of them? This will make them visible as callable functions in VB?

I don't understand the "this call" part. Maybe that is something I don't do. Do you need more points to explain it?

When you say non-static, are you saying virtual functions?

Is it possible to show me the interface code required to do what you are suggesting?

You must be busy. A few months ago you weren't at the top of the experts like you are now (were you?). But your answers have always been good.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1176719
>> But I'm unclear how this helps if I'm using a class
As I said, I don't think VB or any other language is capable of calling C++ member functions.  (It would be possible, but none do--at least not that I know of).

You have to get around this using "handles".  When Vb need to create a c++ object, it calls a non-member function that creates the object (using new) and returns a pointer to the object.  VB would use this value as a handle not a pointer.  That is, as far as VB is concerned it would uniquely identify an object, but it does not point to anything in particula (anything that VB can understand).  It would probably be declared as the VB equivlent of a void * pointer.  Whe Vb needs to use the object, it calls a C++ non-member "interface" function that cooresponds to the member function it would need to call.  This function would take an extra parameter, which is the handle to the object that it needs to work with.  The interface function would then use this handle as a pointer and call the member function it needs to call.

continues.


0
 
LVL 22

Expert Comment

by:nietod
ID: 1176720
An example would be

__declspec(dllexport) class Cls
{
   int I;

public:
SetI(int NewI)
{
   I = NewI;
}

}:

__declspec(dllexport) Cls *CreateCls();
{
   return new Cls;
}

__declspec(dllexport) void SetClsI(Cls *C,int NewI);
{
   C->SetI(NewI);
}

__declspec(dllexport) void DeleteClsI(Cls *C,);
{
  delete  C;
}




0
 

Author Comment

by:flfmdll
ID: 1176721
That falls into the ick category. Don't think I'll want to do that much. I guess the alternative is just to use regular C functions or an ActiveX control.

Submit an answer to get the points. Thanks.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176722
>> I should construct my classes normally except the public functions
>> will also have the dllexport macro in front of them? This will make
>> them visible as callable functions in VB?
That makes them callable from any EXE or DLL that uses your DLL.  BUT that EXE or DLL has to use them correctly.  That means it has to call them using the right calling conventions for passing parameters and taking return values and preserving registers and stack frames.  the problem is that C++ member function are called using a calling convention that is called "this call".  I believe This is similar to the calling convention that VB uses (stdcall), but the difference is that C++ passes a pointer to the object the procedure is being called for in the ECX register.  (This is the "this" pointer).  VB doesn't do this.  In fact, it doesn't have any syntax to indicate you want to call a member function instead of a regular function (right?)

>>When you say non-static, are you saying virtual functions?
No, non-static member functions are like "normal" functions, that is one's that are not called "through" (using) an object.  for example.

class Cls
{
   static void StaticF() {};
   void NonStaticF() {};
}

Cls C;
  C->NonStaticF();  // Non-static call made using an object.
   Cls::StaticF();  // Satic call made without an object.

Not that VB can call "regular" functions, that is, without using an object.  It can't call non-static member functions because VB has no way for you to specify the object to call with and it has no way of specifying the object to work on (the "this" pointer to be passed in ECX).
0
 

Author Comment

by:flfmdll
ID: 1176723
Need a job in Cincinnati, Ohio?
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1176724
>>That falls into the ick category.

If you are doing the bulk of the work in C++ and using classes makes it easier to do it in C++, and you just need to provide simple access to the functions from VB, then it might make sense. It just depends on the circumstances.  This solution is used in many cases.  Note that this is largely the sulution that windows itself uses.  (although the code there is largely C not C++).  Windows returns handles to you.  These handles are pointers to stuctures (or offests into tables) that windows maintains for you.  since you don't know what the format is of the thing the handle points to, you call functions windows provides and pass the handle as a parameter.  Windows converts the handle back to a pointer and uses it in the function.  Being forced to used these handles by windows, I would have to say it is an "icky" solution...
0
 

Author Comment

by:flfmdll
ID: 1176725
Need a job in Cincinnati, Ohio?
0
 

Author Comment

by:flfmdll
ID: 1176726
Thanks again. We have a job opening for a programmer such as yourself. Of course it is the retail world and so the pay may not be what you expect.

David
0
 

Author Comment

by:flfmdll
ID: 1176727
Thanks again. We have a job opening for a programmer such as yourself. Of course it is the retail world and so the pay may not be what you expect.

David
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176728
Funny you should mention that.  My wife might be moving us to Cincinnati.  That or possibly Canton (near Cleveland/Akron).  (Or one of about 10 other places :- (  )

But my job is portable...  thanks though.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

617 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