DLL entry series 2

Source: Internet
Author: User

For ease of learning, this series of articles is reproduced in workshop.

The above section briefly introduces how to create a simple DLL. Next I will briefly introduce how to use a DLL. After a DLL is generated, it creates a. dll file and A. Lib file. Both of them are required. To use DLL, You need to load this DLL.

Implicit Link

There are two methods to load a DLL. One method is a shortcut and the other is more complex than the other. The shortcut is to link only to your. Lib file and put the. dll file in the path of your new project. Therefore, create a new blank Win32 console project and add a source file. Put your DLL in the same directory of your new project.

 

# Include "stdafx. H"
# Include "dllsample. H"

# Pragma comment (Lib, "dllsample. lib") // you can set the Library link in the project properties.

Int main ()
{
Testdll (1, 123 );
Return (1 );
}

 

This is a simple way to load a DLL.

Explicit link

The method for loading DLL is a little complicated. You will need function pointers and some Windows functions. However, with this method of loading DLLs, you do not need the. lib or header file of the DLL, but only need the DLL.

 

# Include <iostream>
# Include <windows. h>
Typedef void (* dllfunc) (INT );
Int main ()
{
Dllfunc;
Hinstance hinstlibrary = loadlibrary ("dllsample. dll ");

If (hinstlibrary = NULL)
{
Freelibrary (hinstlibrary );
}
Dllfunc = (dllfunc) getprocaddress (hinstlibrary, "testdll ");
If (dllfunc = NULL)
{
Freelibrary (hinstlibrary );
}
Dllfunc (123 );
STD: cin. Get ();
Freelibrary (hinstlibrary );
Return (1 );
}

 


First, you will notice that the "windows. H" file is included and "dllsample. H" is removed ". The reason is simple: because windows. h contains some Windows functions, of course you only need a few of them now. It also contains some specific windows variables that will be used. You can remove the DLL header file (dllsample. h) because-as I mentioned earlier-you don't need it when using this method to load the DLL.

The following code is displayed:

Typedef void (* dllfunc) (INT );

This is a definition of the function pointer type. Point to a function that is an int type parameter and the return value is void type.

An hinstance is a Windows Data Type: it is the handle of an instance. In this case, this instance will be the DLL. You can obtain the DLL instance by using the loadlibrary () function. It obtains a name as a parameter. After calling the loadlibrary function, you must check whether the function returns success. You can check whether hinstance is equal to null (defined as 0 in windows. h or a header file in windows. h) to check whether hinstance is successful. If it is null, the handle is invalid and you must release the database. In other words, you must release the memory obtained by the DLL. If the function returns success, your hinstance contains a handle pointing to the DLL.

Once you obtain the handle pointing to the DLL, you can now obtain the function from the DLL again. To do this, you must use the getprocaddress () function, which takes the DLL handle (you can use hinstance) and function name as the parameter. You can let the function pointer get the value returned by getprocaddress (), and you must convert getprocaddress () to the function-defined function pointer. For example, for the add () function, you must convert getprocaddress () to addfunc. That is why it knows the parameters and return values. Now, it is best to first determine whether the function pointer is equal to null and whether they have DLL functions. This is just a simple if statement. If one of them is null, you must release the database as described earlier.

Once function pointers have DLL Functions, you can use them now, but here is a note: you cannot use the actual name of the function; you must use function pointers to call them. After that, all you need to do is release the database.

Module handle

Each DLL module in a process is identified by a globally unique 32-byte hinstance handle. The process itself has an hinstance handle. All these module handles are valid only within a specific process. They represent the starting address of the DLL or EXE module in the process virtual space. In Win32, the values of hinstance and hmodule are the same. You can replace these two types. The process module handle is almost always equal to 0x400000, while the default DLL module load address handle is 0x10000000. If the program uses several DLL modules at the same time, each has a different hinstance value. This is because different base addresses are specified when the DLL file is created, or the DLL code is relocated by the loader.
The module handle is particularly important for loading resources. The findresource function of Win32 contains an hinstance parameter. Both EXE and DLL have their own resources. If the application needs resources from the DLL, specify this parameter as the module handle of the DLL. If you need resources in the EXE file, specify the module handle of the exe.
But before using these handles, how do you get them? To obtain the EXE module handle, call the Win32 function getmodulehandle with the null parameter. If the DLL module handle is required, call the Win32 function getmodulehandle with the DLL file name as the parameter.

How does the application find the DLL file?

If the application uses the loadlibrary explicit link, you can specify the complete path of the DLL file in this function parameter. If you do not specify a path or perform an implicit link, Windows will follow the search order below to locate the DLL:
1. directory containing the EXE file,
2. current working directory of the process,
3. Windows System directory,
4. Windows Directory,
5. A series of directories listed in the PATH environment variable.
There is a trap that is prone to errors. If you use VC ++ for project development and create a project for the DLL module, copy the generated DLL file to the system directory, and call the DLL module from the application. So far, everything is normal. After some modifications are made to the DLL module, a new DLL file is generated again, but you forgot to copy the new DLL file to the system directory. The next time you run the application, it still loads the DLL file of the old version. Be careful!

Debug the DLL Program

Microsoft Vc ++ is an effective tool for developing and testing DLL. You only need to run the debugging program from the DLL project. When you perform this operation for the first time, the debug program will ask you about the path of the EXE file. After that, each time a DLL is run in the debugging program, the debugging program automatically loads the EXE file. Then the EXE file uses the above search sequence to find the DLL file, which means you must set the PATH environment variable to make it include the disk path of the DLL file, alternatively, you can copy the DLL file to the directory path in the search sequence.
Or, when you debug the EXE program, set the category in the debug tab to additional DLLs in project setting. You can debug the EXE and the DLL it calls at the same time (of course, you need the source code of the DLL.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.