Solved

C++ and Visual Basic

Posted on 1998-11-01
14
173 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
  • 7
  • 7
14 Comments
 
LVL 22

Expert Comment

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

Author Comment

by:flfmdll
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> 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
Comment Utility
Need a job in Cincinnati, Ohio?
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
Comment Utility
>>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
Comment Utility
Need a job in Cincinnati, Ohio?
0
 

Author Comment

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 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

16 Experts available now in Live!

Get 1:1 Help Now