Create a C++DLL for use in VB and other languages

Submitted on: 1/2/2015 6:54:00 PM
By: Philip Leitch (from psc cd)  
Level: Intermediate
User Rating: By 9 Users
Compatibility: C++ (general)
Views: 464
     This article explains, with source, how to build a C++ DLL that exports functions that can be called from Visual Basic and other "C" DLL compatable programs.

This article has accompanying files
				If you are like me you like life to be easy. That is why I prefer VB for simple tasks - I have not found another product as quick and easy (although Delphi is also a personal favourite). However, as any VB programmer will tell you, VB is slow. Dam slow. Dam dam dam slow. It is also lacking in functionality compared to fully fledged programming environments such as C++. So, the simple solution is to create DLLs for VB to use. That way you get the speed of application and GUI development in VB with the speed of C++ in processing.
Obviously COM it the best solution….. most of the time. The problem with COM is that it is not always the easiest to learn, create or maintain – especially when versioning and instance counting and…. Anyway, let’s just say that good old C DLLs have their advantages. 
C++ DLLs are quite easy to create in Microsoft Visual C++. The only problem is, they are not easy to get into VB. If anyone has tried to do it there is very little documentation and most of it is misleading. It is a little known fact that if you leave the DLL as __cdecl (default) and them make the functions as extern “C” your VB program CAN use the DLL. However, if you are debugging in the VB IDE it comes up with error 49, Bad DLL calling convention (this only affects strings – or pointers, not numbers). This is an IDE problem only, if you compile the program and run it the error goes away (which is no real way to program). Even so, I think it does some memory leak or something if you call it this way.Here is the “official” MSDN article:
HOWTO: Call C Functions That Use the _cdecl Calling Convention
It is not possible to directly call a C function in a DLL if that function uses the _cdecl calling convention. This is because Visual Basic uses the _stdcall calling convention for calling functions. This is a problem because if _cdecl is used, the calling function is responsible for cleaning up the stack. However, if _stdcall is used, the called function is responsible for cleaning up the stack. 
If, however you go ahead and change the DLL to __stdcall the __declspec(dllexport) does not work properly. The answer is very simple – get rid of all of them, just use a .DEF file. This is the only solid way that I have found of making a C++ DLL for VB. I have created all the code, and one function in the DLL, and the “Declare” and call to that function in the visual basic project. The DLL was initially generated using the standard “Windows 32 DLL” app wizard – so there are still comments and files (readme.txt) left over from that operation – just ignore them.

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.