Implicit link No .lib dynamic link library

zhaozj2021-02-17  55

Since we often call some dynamic link libraries written by third-party vendors or other compilers, they generally do not provide source files or .lib files, and as VC implicit links to DLL (IMPLICITLY LINK to THE DLL), these but is compulsory. This article will focus on reconstructing .lib files without source files and .lib input library files or want to call Windows unappromated functions. Before being built, we must first understand how many ways of DLL output functions. First, the mode of output function from DLL (CALLING CONFENTIONS) _CDECL is the default call mode of the C and C programs. Each function that calls it contains the code that clears the stack, so the resulting executable size is larger than the call _stdcall function. The function uses the stack of right to left. VC compiles the function and adds a downline prefix in front of the function name. _Stdcall is the default call mode of the Pascal program. It is usually used in the Win32 API. The function uses the stack of right to left, and the stack is cleared when you exit. The VC compiles the function before adding a downline prefix in front of the function name, plus the number of bytes of "@" and parameters after the function name. _FastCall mode functions use register pass parameters, and VC compiles the function after the "@" prefix in front of the function name, add the number of bytes of "@" and parameters after the function name. Established an empty dynamic link library with VC 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 fastproC (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

After compiling, generate noname.lib, output function_cdeclproc, _stdcallproc @ 0, @ fastcallproc @ 0; generated Noname.dll can only see the CDECLPROC and FASTCALLPROC functions in PE formats such as Exescope, because stdcallproc is specified Noname properties, No name output, similar to the Windows unappromested function.

Second, the way the program is called DLL

The executable can call a DLL in two ways of implicit linking or Explicit Linking.

When using the explicit link, the program using the DLL must load the DLL before use to get the handle of the DLL module, and then call the getProcAddress function to get the pointer to the output function. You must uninstall the DLL (Freelibrary) before exiting, because not This article focuses on the specific routines, please refer to the relevant documentation. Obviously, this method is very inconvenient when calling a large number of functions.

When using implicit links, executable program links to a input library file (.lib file) containing the DLL output function information. The operating system loads the DLL when loading the executable program. The executable is the same as the output function of the DLL through the function name, the calling method, and other functions inside the program, and the program.

Third, rebuild .LIB input library file

According to Microsoft's suggestions, to be implicitly connected to a DLL, the executable must get a header file (.h file) containing the output function from the DLL provider, a input library for link (.lib file) ). The wishes are very good, but in general, we can't get the input library files for the third-party dynamic link library, or we need to call the Windows unappromested function. If you are using the Delphi or Visual Basic development program, then you can justify the function and the output library. However, friends who use VC have to rebuild .lib files.

1. Delete the Noname.lib generated in the first step (assuming that we don't have this file).

2. Use Microsoft's Dumpbin.exe: Dumpbin / Exports Noname.dll> Noname.def, leaving the output segment of the noname.def file:

Ordinal Hint Rva Name

2 0 00001005 CDECLPROC

3 1 0000100F FastCallProc

1 0000100A [Noname]

change into:

Library "Noname"

Exports

CDECLPROC @ 2

FastCallProc @ 3

NonameProc @ 1 // Please pay attention to the difference between Noname.def in the first step: NonameProc can specify yourself as any name

Perform lib.exe /def:noname.def to generate a Noname.lib file (but if this dynamic link library is not only including _cdecl type functions, then this noname.lib is not ultimately available .lib file, please see The following).

3. Create a Win32 console program called Dllcaller to copy the Noname.dll and Noname.lib that have just been generated into the DllcallerDebug directory.

//Dllcaller.cpp

// Declare function prototype

Extern "C" void _stdcall noameproc (void);

EXTERN "C" void _cdecl cdeclproc (void);

Extern "c" void _fastcall fastpallproc (void);

/ / Link Enter Library File

#Pragma comment (Lib, "Debug / Noname.lib")

Int main (int Argc, char * argv [])

{

NonameProc ();

CDECLPROC ();

FastCallProc ();

Return 0;

}

The compiler produces the following error:

Dllcaller.obj: Error LNK2001: Unresolved External Symbol @ FastCallProc @ 0

Dllcaller.obj: Error LNK2001: Unresolved External Symbol _nonameproc @ 0 Changes Noname.def according to the error prompt information:

@ FastCallProc @ 0 @ 3

NonameProc @ 0 @ 1

Recover the noname.lib, you can recompile Dllcaller.exe.

Fourth, call the Windows Unemployment Function

According to the above analysis, a simple-calling a unapplicared function in the WINDOW98 system shell32.dll is given, and the restart of the dialog box will appear after execution.

//shell32.def, therefore generate shell32.lib

Library "shell32"

Exports

Shshutdowndialog @ 4 @ 60

// DLLCALLER.CPP: Call the console program for unappromant functions

// Function declaration

Extern "C" long _stdcall shshutdowndialog (long lshutdown);

/ / Link Enter Library File

#Pragma comment (Lib, "Debug / shell32.lib")

Int main (int Argc, char * argv [])

{

Shshutdowndialog (0);

Return 0;

}

All programs in this article are touched by VC6.0, Windows98se environment, and I hope that these programs will help with VC's friends.

转载请注明原文地址:https://www.9cbs.com/read-30726.html

New Post(0)