article

Dealing with C DLLs

Email
Submitted on: 2/16/2015 7:04:00 PM
By: Arne Elster (from psc cd)  
Level: Advanced
User Rating: By 4 Users
Compatibility: VB 6.0
Views: 795
 
     Ever stumbled upon the error "Bad DLL Calling Convention"? Was it when you tried to declare a function contained in a DLL? Then this might be a solution for you.

This article has accompanying files
 
				

Calling Conventions and VB

Suppose you have written some functions in C/C++ and you exported them to a DLL.
If not explicitly changed, they will have the calling convetion "cdecl". A calling convention specifies in which order arguments
are pushed on the stack and who removes them from it after the called function returns, either the caller or the callee.
VB only supports the calling convention "stdcall" (actually, cdecl too, we will get back to that), so how would you call these functions,
if they aren't compatible to VB?
If you have the source code to the C functions, you change the calling convention, look into the manual of your compiler, or search for
something like "stdcall C++".
But what to do if you do not? In this case you have various options.

  1. Write a Wrapper DLL in C/C++, where each function has the calling convention stdcall and internally calls the cdecl function.
  2. Write a Wrapper DLL in VB!
  3. Use some precompiled machine code to call the functions.

1. Writing a Wrapper DLL in C/C++

This is a trivial task if you know some C.

2. Writing a Wrapper DLL in VB

Yes, this is actually possible. VB also supports cdecl!
But only compiled to native code.
You can create a new Active-X DLL with an empty class, write your declares, and add some functions which call these functions.
For example:

Option Explicit

Private Declare Function my_c_function Lib "my_c.dll" (ByVal param1 As Long, ByVal param2 As Long) As Long

Public Function my_c_function_wrapper(ByVal param1 As Long, ByVal param2 As Long) As Long
    my_c_function_wrapper = my_c_function(param1, param2)
End Function

After you're finished, compile the DLL (make sure you compile to Native Code, as P-Code won't work!) and reference it
in a new Standard Exe project. Now you can call the functions without any problems.
But beware! VB does not support cdecl callbacks!
This can be solved with:

3. Assembler for cdecl calls and callbacks

The way of of including machine code I used is pretty easy. You reserve executable memory with VirtualAlloc,
write your machine code into it, and call it using some function which allows the execution of a function pointer,
e.g. CallWindowProc().
The bad thing about this way is, you have to know how your arguments look on the stack.
The good thing is, you don't need additional components.
Anyways, you can also combine my function CreateCdeclCbWrap() with method #2 (writing a Wrapper DLL in VB)
and just ignore CallCdecl(). For some code look at the attachment (the example shows how to use the function "qsort"
of the Microsoft Visual C Runtime (msvcrt.dll)).

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.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com


Other 22 submission(s) by this author

 


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 Advanced 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.