Loading and using dynamic-link libraries in Windows programs

Source: Internet
Author: User

1 GetProcAddress ()1.1 Function prototypes

The GetProcAddress function retrieves the address of the output library function in the specified dynamic-link library (DLL).

Functional prototypes: Farproc GetProcAddress (hmodule hmodule,//DLL module handle LPCSTR lpprocname//function name); hmodule [in] a handle to the DLL module that contains this function. LoadLibrary, AfxLoadLibrary  Or the GetModuleHandle function can return this handle. Lpprocname [in] contains a null-terminated string for the function name, or specifies the ordinal value of the function. If this parameter is an ordinal value, it must be at the base byte of a word, and the high byte must be 0. Return value: If the function call succeeds, the return value is the address of the output function in the DLL. If the function call fails, the return value is null. Get further error message, call function GetLastError.1.2 Note the GetProcAddress function is used to retrieve the address of the output function in the DLL. The Lpprocname pointer points to the function name, the spelling and the capitalization must and the DLL source code in the module definition file (. DEF) is the same as specified in the output segment (exports). The output name of the Win32 API function may be different from the name of these functions that you call in the code, which is implicitly in the relevant SDK header file. For more information, refer to the Win32 function prototype (Win32 functions prototypes). The Lpprocname parameter recognizes a function in a DLL by specifying an ordinal value associated with the function (exports segment in. def). The GetProcAddress function verifies that the specified ordinal value is between the ordinal 1 of the output and the highest ordinal value (in. def). The function uses this ordinal value as an index to read the function address from the function table, if. DEF files do not sequentially define the ordinal value of a function, such as from 1 to N (n is the function ordinal value of the output), the error will occur, and GetProcAddress will return an incorrect, non-empty address, although the specified ordinal does not have a corresponding function. To prevent a function from being absent, the function should be specified by name rather than by ordinal value. Requirement: Windows nt/2000: Requires Windows NT 3.1 or later versions. Windows 95/98: Windows 95 or later versions are required. Header file: Declared in Winbase.h, include Windows.h. Library file: Use Kernel32.lib.1.3 Reference Code dynamic Link Library Overview (Dynamic-link Libraries Overview), dynamic link library functions (Dynamic-link libraries Functions), FreeLibrary, GetModuleHandle, LoadLibrary sample code: Call registerserviceprocess in KERNEL32.DLL (applicable only in Windows98) hmodule hmodule=getmodulehandle ("kernel32.dll"); if (hmodule)
{
typedef DWORD (CALLBACK *lpfnregister) (Dword,dword);
Lpfnregister Lpfnregister;
Lpfnregister= (Lpfnregister) GetProcAddress (hmodule, "registerserviceprocess");
if (Lpfnregister)
{
(*lpfnregister) (null,1l);
}
}2 LoadLibrary ()hmodule WINAPI LoadLibrary (_in_ lpctstr lpfilename); Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal Lplibfilename as String) as Long loads the specified dynamic-link library and maps it The address space used by the current process. Once loaded, the resource saved in the library can be accessed Long, and a handle to the library module is returned successfully, and 0 indicates failure. Sets the GetLastError parameter type and description Lplibfilename String, specifying the name of the dynamic-link library to load. Use the same search order annotations specified with the lpCommandLine parameter of the CreateProcess function to release the DLL with the FreeLibrary function once it is not required3 FreeLibrary ()

frees the loaded Dynamic-link library (DLL) module and, if necessary, decrements its reference count. When the reference count reaches zero, the module was unloaded from the address space of the calling process and the handle is no longer valid.

3.1 function Prototypes

BOOL WINAPI FreeLibrary (
_in_ hmodule hmodule
);

Parameters hmodule  [In]

A handle to the loaded library module. the LoadLibrary, loadlibraryex, getmodulehandle, or Getmodulehandleex function returns this handle.

Return value

If The function succeeds, the return value is nonzero.

If The function fails, the return value is zero. To get extended error information, call the GetLastError function.

3.2 Notes

The system maintains a per-process reference count for each loaded module. A module that is loaded at process initialization due to load-time the dynamic linking has a reference count of one. The reference count for a module was incremented each time, the module is loaded by a call to LoadLibrary. The reference count is also incremented by a call to LoadLibraryEx unless the module is being Loaded for the first time and is being loaded as a data or image file.

The reference count is decremented each time the< span>  freelibrary   or   Freelibraryandexitthread   function is called for the module. When a module's reference count reaches zero or the process terminates, the system unloads the module from the address spa Ce of the process. Before unloading a library module, the system enables the module to detach from the process by calling the module ' s & nbsp; DllMain   function, if it has one, with the Dll_process_detach value. Doing so gives the library module a opportunity to clean up resources allocated on behalf of the current process. After the Entry-point function returns, the library module was removed from the address space of the current process.

It is the not safe -to-call freelibrary from DllMain. For more information, see the Remarks sections in DllMain.

Calling freelibrary does not affect and other processes that is using the same module.

Use caution when calling   FreeLibrary   with a handle returned by   GetModuleHandle . The   getmodulehandle   function does not increment a module ' s Reference count, so passing this handle to   freelibrary   can cause A module to being unloaded prematurely.

A thread that must unload the DLL in which it's executing and then terminate itself should call Freelibrarya Ndexitthread instead of calling freelibrary and ExitThread separately. Otherwise, a race condition can occur. For details, see the Remarks section of Freelibraryandexitthread.

Loading and using dynamic-link libraries in Windows programs

Related Article

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.