Implicit link to a dynamic link library without lib [ZT]

Source: Internet
Author: User
Because we often call Dynamic Link Libraries written by third-party vendors or other compilers, the source files or. lib file, which is required for calling as VC implicit link to DLL (implicitly link to the DLL. This article will mainly discuss how to reconstruct the. Lib file without a source file or a. Lib input library file or calling a function that is not publicly available in windows. Before creating a function, we should first understand several methods of DLL output functions.
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ///
For example, when developing DirectX, if we use older sdks, We will link them to d3dx9. lib, you will find the old SDK d3dx9. lib is much larger than the new SDK because the old d3dx9. lib is a complete static library, and the new d3dx9. lib is the import and export of d3dx9_xx.dll. If you want to use d3dx9_xx.dll directly, you can use the method described in this article to export static libraries from DLL. Of course, the new dxsdk provides d3dx9_xx.dll for d3dx9. Lib. Here we only want to explain how to export lib from DLL.
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ///
1. Calling conventions)
_ Cdecl is C and C ++ Program . Every function that calls it contains Code Therefore, the size of the executable file is larger than that of the _ stdcall function. The function uses the stack pressure mode from right to left. After compiling a function, VC adds an underline prefix to the function name.
_ Stdcall is the default calling method of the PASCAL program. It is usually used in Win32 API. The function uses the stack pressure method from right to left and clears the stack when it exits. After compiling a function, VC adds an underline prefix to the function name, and adds "@" and the number of bytes of the parameter to the function name.
_ Fastcall functions use registers to pass parameters. After compiling a function, VC adds the "@" prefix to the function name, and adds "@" and the number of parameters after the function name.

Use VC to create an empty dynamic link library and add the following three files:
// Noname. h dynamic link library header file
Extern "C" Void _ stdcall stdcallproc (void );
Extern "C" Void _ cdecl cdeclproc (void );
Extern "C" Void _ fastcall fastcallproc (void );
// Noname. cpp dynamic link library implementation file
# Include <windows. h>
Extern "C" Void _ stdcall stdcallproc (void)
{
MessageBox (0, "stdcall function", "DLL call", 0 );
}
Extern "C" Void _ cdecl cdeclproc (void)
{
MessageBox (0, "cdecl function", "DLL call", 0 );
}
Extern "C" Void _ fastcall fastcallproc (void)
{
MessageBox (0, "fastcall function", "DLL call", 0 );
}
// Noname. Def Dynamic Link Library output Function Definition
Library "noname"
Exports
Stdcallproc @ 1 noname
Cdeclproc @ 2
Fastcallproc @ 3

Noname is generated after compilation. lib, output function _ cdeclproc, _ stdcallproc @ 0, @ fastcallproc @ 0; generated noname. dll can only see the cdeclproc and fastcallproc functions in tools such as PE format, because stdcallproc is specified with the noname attribute and has no name output, similar to functions not made public in windows.

Ii. Method for calling DLL by executable programs
An executable program can call a DLL by using an implicit link (implicit linking) or an explicit link (explicit linking.
When explicit link is used, the program using DLL must load (loadlibrary) before use to obtain the handle of a DLL module, and then call the getprocaddress function to obtain the pointer of the output function, before exiting, you must uninstall the DLL (freelibrary) because it is not the focus of this Article. For specific routines, see the relevant documentation. Obviously, this method is inconvenient to call a large number of functions.
When implicit links are used, the executable program is linked to an input library file (. Lib file) that contains DLL output function information ). The operating system loads the DLL when using an executable program. The executable program calls the DLL output function directly through the function name. The Calling method is the same as other functions in the program.

3. Rebuilding the. Lib input Library File
According to Microsoft's suggestion, to implicitly link to a DLL, the executable program must obtain a header file containing the output function from the DLL provider (. an input Library (. lib file ). The desire is good, but in general, we cannot get the input library file of the third-party dynamic link library, or we need to call windows undisclosed functions. If you are developing a program using Delphi or Visual Basic, you only need to declare the function and output library. However, friends who use VC had to recreate the. Lib file. For the above example:
1. Delete noname. Lib generated in step 1 (assuming we do not have this file ).
2.use Microsoft dumpbin.exe (in the installation directory of Vc ):
Dumpbin/exports noname. dll> noname. Def
Leave the output segment of the noname. Def file:

Ordinal hint RVA name
------------------------------------------------------
2 0 00001005 cdeclproc
3 1 Listen 100f fastcallproc
1 10000100a [noname]

To:
Library "noname"
Exports
Cdeclproc @ 2
Fastcallproc @ 3
Nonameproc @ 1 // note the difference with noname. Def in Step 1: nonameproc can be specified as any name

execute lib.exe/DEF: noname. def to generate noname. lib file (but if this dynamic link library does not only contain the _ cdecl type function, then this noname. lib is not finally available. lib file, see the following for details ).
3. Create a Win32 console program named dllcaller and load the generated noname. dll and noname. Lib into the dllcallerdebug directory.
// dllcaller. CPP
// declare the function prototype
extern "C" Void _ stdcall nonameproc (void);
extern "C" Void _ cdecl cdeclproc (void );
extern "C" Void _ fastcall fastcallproc (void);
// link the input library file
# pragma comment (Lib, "Debug \ noname. lib ")
int main (INT argc, char * argv [])
{< br> nonameproc ();
cdeclproc ();
fastcallproc ();
return 0;
}< br> the compiler produces the following error:
dllcaller. OBJ: Error lnk20 01: unresolved external symbol @ fastcallproc @ 0
dllcaller. OBJ: Error lnk2001: unresolved external symbol _ nonameproc @ 0
according to the error message, set noname. def changes are as follows:
@ fastcallproc @ 0 @ 3
nonameproc @ 0 @ 1
the new generation is noname.lib, and dllcaller.exe can be re-compiled.

Iv. Call functions not publicly available in Windows
Based on the above analysis, the following is a simple call to the undisclosed function 60 in Windows 98 System shell32.dll. After the execution, a restart dialog box is displayed.
// Generate shell32.lib Based on shell32.def
Library "shell32"
Exports
Shshutdowndialog @ 4 @ 60

// Dllcaller. cpp: Console program that calls undisclosed Functions
// Function declaration
Extern "C" long _ stdcall shshutdowndialog (long lshutdown );
// Link the input Library File
# Pragma comment (Lib, "Debug \ shell32.lib ")
Int main (INT argc, char * argv [])
{
Shshutdowndialog (0 );
Return 0;
}
All the programs in this article are debugged in the vc6.0 and windows98se environments, and I hope these programs will be helpful to friends who use VC.

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /////////////////////////////////////////
Summary:
Library to generate the DLL input library file (*. Lib ).
In general, if you pay attention to the correct DLL export rules, there will be no problems in the use process. In addition, it is easier to export serial numbers.

example:
dumpbin/exports d3dx9_24.dll> d3dx9_24.def
lib/DEF: d3dx9_24.def/machine: x86/out: d3dx9_24.lib

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.