Implicit link without. Lib Dynamic Link Library

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.

1. Calling conventions)

_ Cdecl is C and C ++Program. Every function that calls it containsCodeTherefore, 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
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.

1. Delete noname. Lib generated in step 1 (assuming we do not have this file ).
2.use the Microsoft dumpbin.exe: dumpbin/exports noname. dll> noname. Def to 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 from noname. Def in Step 1: nonameproc can be specified as any name.

Run 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 import 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 [])
{
Nonameproc ();
Cdeclproc ();
Fastcallproc ();
Return 0;
}

The compiler produces the following errors:
Dllcaller. OBJ: Error lnk2001: unresolved external symbol @ fastcallproc @ 0
Dllcaller. OBJ: Error lnk2001: unresolved external symbol _ nonameproc @ 0

Change noname. Def as follows based on the error message:
@ Fastcallproc @ 0 @ 3
Nonameproc @ 0 @ 1

Create noname.libto recompile dllcaller.exe.

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;
}

 

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.