Windows Dynamic Link library programming

Source: Internet
Author: User

Windows Dynamic Link library programming

1. Introduction

The Windows operating system is the most application-critical operating system, so the dynamic link library is also familiar to programmers, even for ordinary users, a lot of times will encounter the file end of the. dll, which is the dynamic link library file. dynamic-link libraries under Windows can be compiled with reference header files and. lib library files, making the dynamic link library implicitly available, or you can use functions such as LoadLibrary, GetProcAddress, and so on to explicitly invoke the dynamic-link library.

2. Syntax, import and Export

In Windows programming, for functions or variables to be used or used, the __declspec keyword needs to be declared to tell the compiler that the variable or function is not an ordinary variable or function, but rather an interface property of a dynamic-link library.

If you define a function to be used by other code, you can write:
__declspec (dllexport) int Add (int a, int b);

If, in this code, you intend to use a variable in another program, you can write:
__declspec (dllimport) char *name;

A dynamic-link library typically contains a series of functions for other programs, so the declspec (dllexport) syntax is most commonly used. If a dynamic library requires a defined global variable in another program, you need to export the variable in another program, and in the dynamic-link library you need to declare the variable as an external variable for use using extern declspec (dllexport).

3. How to link

You can link to (or load) a DLL in one of the following two ways:

Implicit links
Explicit linking

Implicit chaining is sometimes referred to as static loading or dynamic linking at load time. Explicit linking is sometimes referred to as dynamic loading or runtime dynamic linking. Under implicit chaining, the executable file that uses the DLL is linked to the import library (. lib file) provided by the creator of the DLL. The operating system loads the DLL when the executable file is loaded with the DLL. The client executable calls the DLL's exported function as if the functions were contained within the executable file.

Under an explicit link, an executable file that uses a DLL must make a function call to explicitly load and unload the DLL and access the exported function of the DLL. The client executable must call the export function through a function pointer. The executable file can use the same DLL for both methods of linking. In addition, because one executable file can be implicitly linked to a DLL, and the other can be explicitly attached to this DLL, these mechanisms are not mutually exclusive.

4. Implicit link

The implicit link dynamic link library is relatively simple and not detailed.

5. Explicit Link API functions

Explicit linking mainly involves 3 API functions (LoadLibrary, GetProcAddress, and FreeLibrary), which you can use to include windows.h header files.

(1) hinstance LoadLibrary (LPCSTR lplibfilename);

This function is used to load the specified dynamic library file and returns a handle.

The parameter lplibfilename is the name of the dynamic-link library. Windows first searches for "known DLLs," such as Kernel32.dll and User32.dll. The dlls are then searched in the following order:

1. The directory where the executable module of the current process resides.
2, the current directory.
3. Windows system directory. The GetSystemDirectory function retrieves the path to this directory.
4. Windows directory. The GetWindowsDirectory function retrieves the path to this directory.
5. The directory listed in the PATH environment variable.

(2) Farproc GetProcAddress (hmodule hmodule, LPCSTR lpprocname);

The function GetProcAddress is used to get the address of the DLL export function. Returns the variable or function pointer specified by Lpprocname.

The parameter hmodule is a handle to a dynamic library file that has already been loaded.

The parameter lpprocname is the variable or function name to invoke.

(3) BOOL FreeLibrary (hmodule hmodule);
Frees the dynamic-link library represented by hmodule from memory.

(4) If an error occurs, you can call the GetLastError () function or go to the error code.

6. Examples of display links

(1) Dynamic library file code: DLL_DEMO.C

#include <stdio.h>

__declspec (dllexport) int Add (int a, int b)
{
printf ("Calling add\n");
return a + B;
}

The Add () function in the file calculates the sum of two integers and prints a hint string before returning. The function uses the __declspec (dllexport) syntax to illustrate that the function add (int a, int b) is to be exported.

(2) Client case code: MAIN.C

#include <stdio.h>
#include <windows.h>

int main (int argc, char *argv[])
{
int a = ten, B = 20;
int c = 0;
HInstance hinstlibrary = NULL;
Int (*add) ();

printf ("Load DLL file\n");
if (hinstlibrary = LoadLibrary (L "dll_demo.dll") = = = NULL)
{
printf ("***loadlibrary ERROR:%d.\n", GetLastError ());
return 1;
}
if ((add = (int (*)) ()) GetProcAddress (Hinstlibrary, "add") = = = NULL) {
printf ("***getprocaddress ERROR:%d.\n", GetLastError ());
return 1;
}

c = Add (A, b);
printf ("%d +%d =%d\n", A, B, c);

FreeLibrary (hinstlibrary);
return 0;
}

The program uses the LoadLibrary function to load the dynamic link Dll_demo.dll, uses the FreeLibrary to close the handle, obtains the error code with GetLastError (), and uses GetProcAddress to locate the Add function in the shared library. It then calls the function to perform the addition calculation and prints the result.

(3) Compile and run

To compile a shared library:

Create a dynamic link library project in vs.net, name Dll_demo, add File dll_demo.c, build Dll_demo.dll file after compiling.

To compile the case program:

Create a dynamic link library project in vs.net, name Dll_main, add File main.c, compile Dll_main.exe to execute the file.

Run:

Place Dll_demo.dll and Dll_main.exe in the same directory, and then double-click Run Dll_main.exe.

Output:

Load DLL File
Calling add
10 + 20 = 30

7. Calling variables in the dynamic link library

You can also use variables in a dynamic-link library. For example, define in the dynamic link library:

__declspec (dllexport) int num = 100;

Then you can call this in the case program:

int *d;
D = (int *) GetProcAddress (hinstlibrary, "num");
printf ("num =%d\n", *d);

Windows Dynamic Link library programming

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.