Important alert: (current site time 7/15/2013 4:44:01 PM EDT)
 

article

An almost complete DLL guide!

Email
Submitted on: 9/3/2003 10:42:42 PM
By: D Davis  
Level: Intermediate
User Rating: By 49 Users
Compatibility: C++ (general), Microsoft Visual C++
Views: 68964
author picture
(About the author)
 
     + Creating ActiveX DLL's in VB (and compatability issues),
+ Creating DLL's in C++,
+ Using ActiveX DLL's in VB apps,
+ Using Non-ActiveX DLL's in VB app and C++ apps,
+ Loading DLL's DYNAMICALLY in both VB and C++ (Good for plug-ins),
+ Calling a function by a string (Good for scripting programs).

This article was written for alot of reasons. For one, I needed to learn how to load and use DLL's and get a better understanding of doing so. Another was because there are so many shxtty examples out there today it's not even funny. Plus I found ZERO information on most of the stuff I go over. Every tutorial I've seen on how to make a DLL covers making one, then using the 'Declare' statement to utilize them. Not any good for plug-ins. Plus, the other half of the tutorials didnt even work! Anyways, I wrote this to try and sum up all of the half-ass tutorials and the complex, cryptic information and fill in the non existant information. I try to explain as best as I can. I know there might be slight changes that have to be done in order for it to work with you, but so be it. Make them and write about it to teach others.

I hope that this gives everyone a better idea of how to use DLL's and add flexability to their applications/games.

 
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 including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article 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 article (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 article 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 article or article's description.


Other 2 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 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
9/3/2003 10:51:20 PMD Davis

Yes, I did post this in two categories since it deals with VB and C++.
(If this comment was disrespectful, please report it.)

 
9/4/2003 4:46:08 AMPsyc

very good article.. thanks! :) [5 glbs]
(If this comment was disrespectful, please report it.)

 
9/5/2003 2:14:42 PMmaniac

very nice, 5 from me
(If this comment was disrespectful, please report it.)

 
9/7/2003 8:00:23 AMAli Akbar

Excellent Article. Thanks.

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

 
9/7/2003 12:21:30 PM

very nice, i was wanting to learn some of that stuff
(If this comment was disrespectful, please report it.)

 
9/10/2003 4:38:01 AM

Very useful article, with useful information.
(If this comment was disrespectful, please report it.)

 
9/11/2003 5:27:36 AMJohn Bell

Umm, stdafx? ack....I prefer the hard way. If you want info on loading dll's into other processes(cross-task subclassing) I submitted an article on how to do this for aol. fallenhobit is the name its submitted under. Peace.

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

 
9/11/2003 10:13:38 AMD Davis

I agree! I only used the stdafx for easy setup purposes. that's it. I prefer the hard way as well!
(If this comment was disrespectful, please report it.)

 
9/12/2003 2:17:31 PM

Thanks for the article! I've been wanting to do some of this myself and this will at least get me started before I head off into the dark...
(If this comment was disrespectful, please report it.)

 
9/16/2003 4:08:12 PMJohn Mikhaiel

just wanted to let you know that in the Accessing you Dll Code, before the end of the process, its better to include the following line:

Set MyDll = Nothing

to free it up this way itll free up memory wit it, and its unloaded properly. the reason y i think it should be included is cause you also included the loading process (CreateObject) so if the code loads it, it should unload it. not tryin 2 be smart or anythin.
(If this comment was disrespectful, please report it.)

 
9/16/2003 4:20:36 PMJohn Mikhaiel

also for users of older vb versions:
vb5 supports ActiveX Dlls, and the 32bit version of VB4 (but ActiveX Dlls were called OLE Dlls). the 16bit version of VB4 supports only ActiveX EXEs which if written correctly are the same as the Dlls but can also be stand-alone programs (NOTE That vb4 16bit is only good for compatibilty with windows 3.1 and is pretty useless otherwise, vb4 32, and 16 are on the same installation CD).
(If this comment was disrespectful, please report it.)

 
10/9/2003 7:11:19 PMJonathan Ho

Just what I needed. Very concise. 5 from me!
(If this comment was disrespectful, please report it.)

 
12/3/2003 4:47:30 PMWolf McCloud

I don't really see the use of this for me but it looks nice for beginners that can't learn by themselves... I'll vote like other people. :)
(If this comment was disrespectful, please report it.)

 
12/3/2003 5:05:30 PMD Davis

This article was meant for people who have never been involved in complex projects, or have no experience with design docs. Hopefully it didnt come off as a guideline for experts as well as it was not intended to be. It's basically a place to start for those who have never worked in organized projects.
(If this comment was disrespectful, please report it.)

 
12/25/2003 6:51:49 AMmugman21

PART 1:

Great Job, this is a very nice piece of work.

First, when calling the dll, there is no need for all the fancy api calls (load library, getprocaddress,freelibrary yada yada yada). Just do this... In your dll, change:
int MyDLLFunc1() {
MessageBox(NULL,"My DLL Function 1","",MB_OK);
return true;}
to:
int _stdcall MyDLLFunc1() {
MessageBox(NULL,"My DLL Function 1","",MB_OK);
return true;
}
(If this comment was disrespectful, please report it.)

 
12/25/2003 6:52:20 AMmugman21

PART 2:
NOTE THE KEY PRASE "_stdcall" I ADDED!!!
After that is done, you can call the dll normally, like anyother.
For example, in vb just do a "Private|Public declare function MyDLLFunc1 lib "The_DLL_Name" () as long" This goes for C++ as well. This makes it a lot easier. As you can see, in vb after you declare the api to call it you only use it's name:
Command_Click()
MyDLLFunc1
End Sub
One thing you mentioned in your article is the use of .def files. I use that method myself, but if I recall correctly, you said you did not know of any other way to export functions. This function __declspec(dllexport) allows you to do it without creating a .def file. I never use it, although it supposedly creates a smaller dll.Thanks for your submission, and hope you release an updated article soon, thanks and take care.
(If this comment was disrespectful, please report it.)

 
12/25/2003 7:09:51 AMmugman21

One more thing, the dll needs to be in the app.path directory, the winnt|windows dir, or the system32 dir. D Davis, I hope you would release an update to this article soon, I think what you created was excellent.
(If this comment was disrespectful, please report it.)

 
1/19/2004 9:07:05 AM

Listen to this dude he knows what hes talking about, ok

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

 
1/27/2004 3:03:28 AMRAJENDRA

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

 
2/6/2004 6:11:47 AMAbdul Rafay Mansoor

Thanks for the simple explaning style, but man i got this DEBUG ERROR when i run client exe it says "ESP not saved properly across the function call...as a result of calling a function declared with one calling convention with a function pointer declared with different calling convention" if i comment out the "HelloWorld" (C++)message box the it works fine.. any suggestions.??
(If this comment was disrespectful, please report it.)

 
2/16/2004 4:43:40 PM

Has anyone noticed that the 2nd function: MyDLLFunc2(char *TEXT) takes a string pointer as an argument, but that the section on how to access the functions from VB only gives an example using MyDLLFunc1 which does not have an argument. Anyone know how to pass a string argument to callwindowproc in visual basic so Tha MyDLLFunc2 will work?
(If this comment was disrespectful, please report it.)

 
4/28/2004 4:18:56 AM

The article is nice. I 've already implemented the code but I got this DEBUG ERROR when i run program it says "ESP not saved properly across the function call...as a result of calling a function declared with one calling convention with a function pointer declared with different calling convention"

Can you please help me?

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

 
5/20/2004 1:06:07 AMHenry_Mendes

Very good article. I were looking for a tutorial that teaches how to use C++ DLLS in VB app and this does!!! Thanks a Lot! Sorry with my engish mistakes!
(If this comment was disrespectful, please report it.)

 
6/12/2004 10:11:35 AM

That Debug error is very annoying, I think I’ve solved it though. Instead of returning an 'int' return 'LONG WINAPI', e.g.

LONG WINAPI MyDLLFunc2(LPSTR Text)
{
MessageBox(GetFocus(),Text,
(If this comment was disrespectful, please report it.)

 
6/22/2004 10:50:42 AMAlexander Wuzinger

zu __declspec(dllexport):
'__declspec(dllexport)XXX' Exportiert eine Function zwar ohne ein .def Datei, diese hat dann jedoch einen namen wie '??0XXX@@AAE@PBQBD@Z'

zu: MyDLLFunc2(char *TEXT):
Dieses Problem ist mir auch aufgefallen, weis vieleicht irgendjemand, wie man parameter übergiebt ? vieleicht mit pointern ????

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

 
9/24/2004 3:40:30 AMSriniwas

Good Man. This is a nice article for fresher. Thanks for Great Work.
(If this comment was disrespectful, please report it.)

 
3/23/2005 9:51:15 PM

hi, can you make a DevC++ version of that DLL? I've tried everything, but when I try to find functions address (GetProcAddress), it returns 0 (zero).

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

 
3/28/2007 3:50:52 AMManas Ranjan Sahu

Hi,
A very good effort indeed. Just a minor aberration. The C++ DLL that you have written is a plain API DLL and not an ActiveX DLL as the VB DLL. I feel it would have been better to include an ActiveX DLL example in C++ contrasting it with the VB DLL.
Anyways, it does not anyway undermine the efforts put in by you.
Good work.
Bye

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

 
4/11/2007 4:18:16 PMSebastian

hi, can you make a DevC++ version of that DLL? I've tried everything, but when I try to find functions address (GetProcAddress), it returns 0 (zero).

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

 
3/31/2008 1:55:50 AMchamika deshan

Hi
As someone mentioned there debug error can eliminated by using LONG WINAPI instead of int in DLL File MyDLLFunc2 function.
But how do we do that in main C++ code instead of changing the dll?
(If this comment was disrespectful, please report it.)

 
3/31/2008 7:08:03 AMchamika deshan

I found the way to eliminate the debug error by changing the C++ exe project instead of changing C++ dll project.

Instead of CALLBACK use __cdecl in the function pointer.

It's like this

typedef UINT (__cdecl * LPFNDLLFUNC1)(VOID);
(If this comment was disrespectful, please report it.)

 
10/24/2010 8:57:02 AMDave Carter

Dustin, Thanks Man, Dave :)
(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 article, please click here instead.)
 

To post feedback, first please login.