Important alert: (current site time 7/16/2013 2:23:33 AM EDT)
 

winzip icon

Universal dll function caller, CDECL & stdcall with .bas/.cls/.frm/.ctl callbacks

Email
Submitted on: 3/10/2008 6:42:36 PM
By: Paul Caton 
Level: Advanced
User Rating: By 22 Users
Compatibility: VB 6.0
Views: 23289
 
     I received a report recently that my 2003 CDECL submission had generated DEP errors on Server 2003. I've updated that submission, but whilst doing so, I saw the potential for some improvements. Enhancements that really needed a new submission. Features: Transparent CDECL/stdcall function calling. Call functions by ordinal or name. Module (.bas) callbacks for CDECL functions. Object (.cls/.frm/.ctl) callbacks for CDECL and stdcall functions. Support for multiple callbacks. Support for multiple cCallFunc.cls instances. Support unicode path\module names. NEW SUBMISSION: 70195

 
winzip iconDownload code

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

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.


Other 13 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 code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

12/7/2007 10:35:33 AMTracy Dryden

The rewrite makes this class even easier to use and more capable. You really CAN call cdecl routines from VB! Paul saved me a ton of work with this one!
(If this comment was disrespectful, please report it.)

 
12/8/2007 6:19:41 AMmnn

Thx, but it don't work for me. I'm sure that my DLL exists, that it exports required functions correctly, but your code fails.
(If this comment was disrespectful, please report it.)

 
12/9/2007 3:43:20 PMPaul Caton

mnn, what doesn't work for you? Does CallFunc raise a VB error? Is the function name mangled? If so, have you tried by ordinal? Is the dll in the path? Is the dll fastcall? I don't currently support that convention as it's rare and not standardised between different vendors implementations e.g. Microsoft/GCC, Borland/Delphi, Watcom, TopSpeed/Clarion/JPI
(If this comment was disrespectful, please report it.)

 
12/11/2007 4:28:23 PMJoe Jordan

Your submissions along with LaVolpe's submissions are single-handedly extending the life of VB6. Keep up the excellent work.
(If this comment was disrespectful, please report it.)

 
12/11/2007 5:51:00 PMzaz

Great work! A small suggestion though. How about adding the ability to call function pointers not just from a dll?
I tried a quick hack by adding another function similiar to the dll function, but it seems to break the code. If I modify the existing one it seems to be working.
But i need to investigate it a bit further. It was just a quick hack.
Anyway, 5 globes :)
(If this comment was disrespectful, please report it.)

 
12/12/2007 6:48:43 PMPaul Caton

thx people... @zaz: interesting thought, I'll look into that for you for the next update (soon-ish) which will add support for MS/GCC __fastcall function calling.
(If this comment was disrespectful, please report it.)

 
12/14/2007 2:53:09 AMYogi Yang

Great work keep it up.
(If this comment was disrespectful, please report it.)

 
12/22/2007 12:24:39 AMDavid J. Fritts

Hey there Pauly! Long time no see, I'll get straight to the point. I was working on a bot for a battle.net game client. Part of the bot required loading a dll and calling a fastcall function. Although the code is specifically made for one purpose only, it could be easily ported to an universal fastcall function caller.

code location: http://forum.valhallalegends.com/index.php?topic=7248.15
(If this comment was disrespectful, please report it.)

 
1/7/2008 5:25:28 PMzorbasoft

!!! Your and LaVolpe submissions are single-handedly extending the life of VB6 !!!
(If this comment was disrespectful, please report it.)

 
2/1/2008 3:13:07 PMMark Osborn

Does the job nicely. However, what to do about functions returning a double - or did I miss something.
(If this comment was disrespectful, please report it.)

 
2/8/2008 8:47:43 AMMark Osborn

As a follow up to the question on functions returning a double. I've added some sample code. The return value seems to be neither a double nor a valid pointer. Any suggestions, anyone - especially since callbacks, char* all work fine.

extern "C" __declspec(dllexport) double GetDouble(){return 1.1;}

Private Declare Function GetDouble Lib "MyDLL" () As Double
Public Sub MyTest()
Dim ccf As New cCallFunc
Dim d As Double
Dim l As Long
d = GetDouble()
Debug.Print "d ="; d
d = ccf.CallFunc("MyDLL", "GetDouble")
l = ccf.CallFunc("MyDLL", "GetDouble")
Debug.Print "d ="; d; ", l = "; l
End Sub

>>d = 1.1
>>d =-858993460 , l = -858993460




(If this comment was disrespectful, please report it.)

 
2/13/2008 6:54:31 PMPaul Caton

I will make a new submission soon that adds support for __fastcall, call by pointer and a complete set of return types... namely: Byte, Integer, Long, Int64 (Currency), Single, Double and no return value (ala a VB sub or "void" in 'C'). Demonstrates also how to pass ByVal parameters that are larger than Long.. eg. Double and Currency. Soon.
(If this comment was disrespectful, please report it.)

 
3/10/2008 9:22:22 PMJoehel V. del Rosario

nice code dude

pls vote my new code submission

http://pscode.com/vb/scripts/ShowCode.asp?txtCodeId=70230&lngWId=1

tnx

(If this comment was disrespectful, please report it.)

 
3/12/2008 4:01:15 AMDane

Can any one help me? I was trying to call a function by using a string variable / joining 2 string to form a function name, is this possible in VB? I can do this in Clipper and I find this useful.
(If this comment was disrespectful, please report it.)

 
5/28/2008 5:46:32 AMkostas

I give u "Excellent" for this.
Helped me a lot.
(If this comment was disrespectful, please report it.)

 
3/25/2010 10:20:41 AMCynx

i was looking to use windowsAccessBridge.dll in VBA...when I came across you class, i believe i found the way out ! I did try to call functions of that dll, however One of the function which returns vmID and Accessbility content, call that with help of this class crashes the VBA editor !!

Can you please point out if i am doing anything wrong...or that is the limitation of use of class in VBA ?
(If this comment was disrespectful, please report it.)

 

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 code, please click here instead.)
 

To post feedback, first please login.