I've programmed in a few languages - JavaScript, PHP, and a proprietary language called GML used for the Game Maker program - but am new to C++ and have been teaching myself. I'm interested in making a DLL. I understand the fundamentals of Structured programming in C++ and how to make a very simple class. I'm working my way through a textbook a friend let me borrow, but I don't believe that the book covers DLLs.
What I would like to do is show you the code Dev-C++ gives me when I make a new DLL project and ask that someone explain the various parts I'm fuzzy on.
The generic header file dll.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
01- #ifndef _DLL_H_
02- #define _DLL_H_
03-
04- #if BUILDING_DLL
05- # define DLLIMPORT __declspec (dllexport)
06- #else /* Not BUILDING_DLL */
07- # define DLLIMPORT __declspec (dllimport)
08- #endif /* Not BUILDING_DLL */
09-
10-
11- class DLLIMPORT DllClass
12- {
13- public:
14- DllClass();
15- virtual ~DllClass(void);
16-
17- private:
18-
19- };
20-
21-
22- #endif /* _DLL_H_ */
|
I understand that lines 1, 2, & 22 state that the header is only to be included if another file has not already included it. I also know lines 11-19 define the class that will hold the DLL's functions and data members.
However, what do lines 4-8 do? What does "DLLIMPORT" between the class statement and the class name on line 11 do?
Now in the source file dllmain.cpp it gives me
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
01- /* Replace "dll.h" with the name of your header */
02- #include "dll.h"
03- #include <windows.h>
04-
05- DllClass::DllClass()
06- {
07-
08- }
09-
10-
11- DllClass::~DllClass ()
12- {
13-
14- }
15-
16-
17- BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
18- DWORD reason /* Reason this function is being called. */ ,
19- LPVOID reserved /* Not used. */ )
20- {
21- switch (reason)
22- {
23- case DLL_PROCESS_ATTACH:
24- break;
25-
26- case DLL_PROCESS_DETACH:
27- break;
28-
29- case DLL_THREAD_ATTACH:
30- break;
31-
32- case DLL_THREAD_DETACH:
33- break;
34- }
35-
36- /* Returns TRUE on success, FALSE on failure */
37- return TRUE;
38- }
|
I understand that lines 2-3 include my dll's header file as well as the windows.h header file. I know that 5-14 are the constructor and destructor functions for my class. I know that any other functions would be put before line 17.
However, what do lines 17-38 accomplish? Is there anything specific that needs to be in the (de)constructor functions of a DLL? or are these optional like other classes? Is it necessary to include the windows.h header? What would I possibly put inside the cases of the switch statement (lines 21-34)?
I'm trying to teach myself C++. So if someone can break these things down for me and explain not just what they are but why they are there - what they accomplish, if there are alternatives, etc. - so that I don't just repeat someone else' code, but can actually grasp what I'm doing it would be much appreciated.
Thank you in advance.