Home > Back-end >  (turn) DLL written tutorial
(turn) DLL written tutorial

Time:10-02

Half an year can't get to the Internet, network finally through recently, finally can update the blog, write something? Decided recently wrote a series of programming technology, its content is some common programming techniques, such as a DLL, COM, Socket, multithreading, and so on, the characteristics of these techniques is widely used, but a lot of misunderstanding; Many online tutorial, but there are few good quality product, I nearly a few months of programming experience, found that it is necessary to thoroughly summarize these programming techniques, result is summarized to improve on their own, and can be convenient to visit my blog friend,

Ok, cut the crap, get to the point, the first is the "DLL written tutorial", why so soil name? Why not call "easily write DLL" or "DLL 1 tong"? Or more nb "DLL" in-depth Jane out of? Ha ha, often search for information online brothers naturally know,

In this paper, the general DLL technology made a summary, and provides the source code package download, download address is:

http://www.blogjava.net/Files/wxb_nudt/DLL_SRC.rar

The advantage of DLL
In short, the DLL has the following advantages:

1) save memory, one software module, and if it is in the form of source code reuse, will be compiled into different executable program, at the same time run the exe these modules binary code will be repeated loading into memory, if you are using a DLL, is loaded in memory only once, and all the process of using the DLL will be sharing this piece of memory (like global variables in a DLL, of course, this kind of thing will be a copy of each process),

2) does not need to compile software system upgrade, if a software system to use the DLL, the DLL is changed unchanged (function), the system upgrade you just need to replace the DLL, don't need to recompile the entire system, in fact, many software upgrade in this way, for example, we often play starcraft, warcraft game version upgrade it, such as

3) Dll library can be used for a variety of programming languages, such as Dll can call in vb written in c, this Dll is done is not enough, so on the basis of the Dll invented the COM technology, better to solve a series of questions,

The most simple DLL
Before start to write a DLL, you need a c/c + + compiler and linker, and close your IDE, yes, I know the your VC and c + + BUILDER is turned off, and open the notepad application you just to remember the telephone in the past, do not do so, you might not understand the true meaning of DLL, the rest of my life I used the VC's own cl compiler and link the linker, they are generally under the bin directory of VC, (if you do not have when install the VC to select register environment variables, then immediately to add their path to path) if you are still left the IDE and afraid to cry, because you can close this page and continue to see such as "VC + + technical insider" boring book,

The most simple DLL is not better than c helloworld difficult, as long as a DllMain function, contain objbase. H header file (support COM technology a header file), if you feel this header file name is hard to remember, then use Windows. H can also, source code is as follows: dll_nolib. CPP
 # include & lt; Objbase. H> 

#include

BOOL APIENTRY DllMain (HANDLE hModule, dwords dwReason, void * lpReserved)

{

HANDLE g_hModule;

The switch (dwReason)

{

Case DLL_PROCESS_ATTACH:

cout<& lt;" Dll is attached!"
G_hModule=(HINSTANCE hModule);

break;

Case DLL_PROCESS_DETACH:

cout<& lt;" Dll is detached!"
G_hModule=NULL;

break;

}

return true;

}

The DllMain is the entrance to each DLL function, as well as the main function of c, DllMain with three parameters, hModule said this DLL instance handle (not understand ignore it, wrote a Windows program of natural understand), said dwReason DLL's current state, such as DLL_PROCESS_ATTACH said DLL is loaded into a process just now, said DLL_PROCESS_DETACH DLL just uninstall from a process, of course, said loaded into the thread and unload from the thread state, here is omitted, the last parameter is a reserved parameters (current and some state related DLL, but rarely used),

Can be seen from the above procedures, when the DLL is loaded into a process, the DLL print "DLL is attached!" Statements; When the DLL from the process of unloading, print "DLL is detached!" Statements,

Compile DLL requires the following two commands:
 cl/c dll_nolib. CPP 

This command will compile the CPP for obj file, if no use/c parameters, cl obj links to exe, also will try to continue but this is a DLL, the main function, therefore complains, it doesn't matter, continue to use the link command,
The Link/DLL dll_nolib. Obj
This command will generate dll_nolib. DLL,

Note that because the compiler command is simpler, so this paper does not discuss nmake, interested can use nmake, or write a bat batch to compile the link DLL,

Load the DLL (explicitly call)
Using DLL in general there are two ways to explicitly call and call, first introduced explicitly call here, write a client program: dll_nolib_client. CPP
 # include & lt; Windows. H> 

#include

Int main (void)

{

//load our DLL

HINSTANCE hinst=: : LoadLibrary (" dll_nolib. DLL ");

If (NULL!=hinst)

{

cout<& lt;" DLL the loaded!"
}

return 0;

}

Note that call DLL using LoadLibrary function and its parameters is the path and name of the DLL, the return value is a handle to the DLL, use the following commands to compile the client link:
 Cl dll_nolib_client. CPP 

And perform dll_nolib_client. Exe, get the following results:
 Dll is attached! 

DLL the loaded!

Dll is detached!

The above results show that the DLL has been client load, but it can only DLL is loaded into memory, can't find the function in the DLL,
Use dumpbin command view function in a DLL
Dumpbin command to view a DLL output function of the symbolic name, type the following command:
 Dumpbin - exports dll_nolib. DLL 

CodePudding user response:

Thank you, welcome to continue,
  • Related