Overview Microsoft Windows and Windows applications use dynamic link library (DLL) execution features such as DllRegisterServer, DllunregISTERRERVER, or load and uninstall, DllgetClassObject, DllcanunloadNow objects, processes, and data.
This article will discuss dynamic links, entry points, dependence, dynamic libraries, DLL problems, and troubleshooting. This article also includes comparisons for Win32 and .NET assembly.
Back to top
More information
What is DLL? Dynamic Link Library (DLL) is a collection of modules that contain functions and data. Program files (such as .exe files or .dll files) load these modules (that is, the required modules map to the address space of the calling process). The following two types of functions define a DLL:
Export function: These functions are called by other modules. Internal functions: These functions are only called from the DLL defined. DLL also exports data. However, this data is used by the corresponding function.
Dynamic links and static link dynamic links include information required for the code to find the code of the exported DLL function when the system is loaded or run.
In static links, the linker copies the code of the library function to each module that calls the DLL.
The function in the DLL can be called in the following manner:
Dynamic link when loading: The call square module performs explicit call to export the DLL function. Create a wrapper for the DLL, then link the DLL to your application. When loading an application, the import library provides information on loading DLL and finding the exported DLL function. Running Dynamic Link: When loading the DLL at runtime, the calling square module uses the loadLibrary function or the LoadLibraryEx function. The call square module calls the getProcAddress function to get the address of the exported DLL function. In the link, Windows searches for a pre-installed set of DLLs, such as performance libraries (kernel32.dll) and security libraries (user32.dll). Then, Windows searches for the DLL in the following order:
The directory where the current process is executable. Current directory. Windows system catalog. (The getSystemDirectory function gets the path to the Windows system directory.) Windows directory. (The getWindowsDirectory function gets the path to the Windows directory.) The directory listed in the PATH environment variable. Note: libpath environment variables are not available for search.
Back to top
DLL and static library
DLL saves memory and reduces exchange. By sharing a single copy of the DLL in the memory, multiple processes can be used simultaneously with a DLL. In contrast, every application built by using a static link library, Windows must load a copy of the library code in memory. DLL saves disk space. Multiple applications can share a DLL copy on the disk. In contrast, each application built using a static link library requires a library code that links to the program file image as a separate private copy. DLL saves time. When changing the function in the DLL, as long as the function's parameters and return values are unchanged, they do not have to recompile or re-link applications using these functions. However, if you use a static link object code, you must re-link the application after changing the function. DLL can share a function. In WIN32, DLL can share a function. By default, data is independent for each process. However, the static library contains separate data copies and functions for each process.
Back to top
DLL entry point DLL has a special entry point (
Dllmain functions are running in addition and separation processes and threads. This behavior allows you to create and destroy the data structure as needed. File extension is .ocx,
.cpl and
The file type of .drv is also a DLL, although the file extension has changed. In Windows 2.
X and windows 3.
In X, each DLL has only one data segment instance, regardless of how many applications are available. In Windows 32, the DLL can be marked as sharing to result in the same behavior. However, the default setting for each process is a dedicated copy with DLL data.
You can implement the following objects by creating a DLL:
The procedure is divided into separate modules that can be loaded on demand. Store resources specific to language or specific to zones. Make your own app to use the core code library. Generate a COM object or ActiveX control (OCX) in the process. Use the OLE object as the process within the process. This use can improve the performance of the OLE link. Use the control panel to expand or use some types of drivers. To generate a DLL, please use
DLLMAIN function (not a program file) replace
Winmain function.
To guide the function in Win16, please
FAR EXPORT is added to all exported DLL functions (do not require this in Win32). Many 32-bit compilers provide a function declaration, such as
__Declspec (dllexport) and
__Declspec (dllimport). These function declares appear before the function declaration is replaced. However, the definition must only be specified
DllexPort properties.
Back to top
DLL problem and troubleshooting tool
Initialization Problem In the DLL initialization, you may encounter one or more questions:
If the service account password of the program service is different from the password of the domain user manager account, you may receive the following initialization error message when you plan service:
Initialization of Dynamic Link Library C: /Winnt/System32/kernel32.dll failed.Process is Terminating AbnorMally
For additional information, click the article number below to see the corresponding article in the Microsoft Knowledge Base: 250265 When using cmd.exe to run the AT program command, a kernel32.dll dynamic link library initialization error If you continue to load and uninstall Crypt32.dll, The transport layer security (TLS) is allocated while loading CRYPT32.DLL, but does not release it when uninstalling CRYPT32.DLL. This behavior can lead to general protection errors (GPF) errors, and you may receive the following error message:
Initialization of the Dynamic Link Library C: /Winnt40/system32/crypt32.dll failed.the process is terminating abnormal.
For additional information, click the article number below to see the article in the Microsoft Knowledge Base: 212825 SMS: General Protection Error: Initialization of crypt32.dll failed (Crypt32.dll initialization) If you upgrade your computer to McAfee VirusScan 6.0, you may receive the following error message within three minutes after starting Outlook 2000:
Outlook.exe. DLL INITIALIZATION FAILD.INITIALIZATION OF C: /WINNT/System32/compstui.dll. Process is Terminating Abnormal. For additional information, click the article number below to see the article in Microsoft Knowledge Base: 310413 OL2000 : Upgrade to McAfee VirusScan 6.0 After Outlook 2000 DLL Initialization Errors If you set Windows Nt Mail or MSmail32.exe to start by planning, the plan service may be recorded as other content other than the system, and you may receive the following error message :
Initialization of the Dynamic Link Library OleCli32.dll failed abnormally
For additional information, click the article number below to see the article in the Microsoft Knowledge Base: 133476 Windows NT Mail Error Message: Initialization of the Dynamic Link ... (Dynamic Link Initialization ...) If remote access service (RAS) file or modem file loss or damage, and you attempt to open the control panel, you may receive the following error message:
Explorer.exe - DLL Initialization Failed Initialization of the Dynamic Link Library E: /WINNT/System32/Rasscrpt.dll Failed.The Process IS Terminating Abnormal.
For additional information, click the article number below to see the article in the Microsoft Knowledge Base: 184443 Error message: Explorer.exe - DLL Initialization Failed ... (Explorer.exe - DLL initialization failed ...) If the system Insufficient memory, you can't create a new desktop stack for the start-up service, you may receive the following error message:
Servicename - DLL Initialization Failure Initialization of the Dynamic Link Library C: /Windows/system32/User32.dll failed.the process is terminating abnormally.
For additional information, click the article number below to see the article in the Microsoft Knowledge Base: 142676 Solve the failure of the USER32.DLL initialization
Back to top
Dependency If you don't wait until all system services start running, start your service, it may encounter a dependency problem. For example, service depends on other services or drivers. These services or drivers turn over to other services or drivers. More specifically, Rasapi32.dll implicitly rely on RAS service. Use Rasapi32.dll
A Windows NT service of LoadLibrary makes the service depend on RAS service.
When you change the project, you regularly scan all dependencies through the Projects menu to ensure the accuracy of the dependencies list. If you do not save changes to files in the project before performing a dependency scan, the new dependency will not appear in the list.
Back to top
Performance issues If you load an implicit link to the DLL that you want to link to the application while loading an application, a performance issue occurs. For example, a delay may occur when a computer running a Windows 2000 loads a WinMM.dll file in the memory. This delay may occur at different times, depending on how the Winmm.dll file is loaded: If the program uses the import library to load the Winmm.dll file, Winmm.dll is loaded when the program starts. In this case, it may be delayed when the program is started. Therefore, if the batch file is used multiple times, the performance will decrease significantly. If the program loads a Winmm.dll file using LoadLibrary, WinMM.dll is only loaded when the program needs. For example, if you play the sound when you open the menu, load the Winmm.dll file in the first playback sound. In this case, a delay may occur when the program is loaded with the WinMM.dll file. To improve performance, use optimization skills when loading. In addition, DLL can be divided into two DLLs:
Put the calling application to all functions required immediately after loading, and then link the calling application to this DLL. Put the function that calls the application does not immediately put in another DLL and explicitly link the application to this DLL.
Back to top
Troubleshooting Tools support troubleshooting using the following tools:
Dependency Walker Dependency Walker is more than just a troubleshooting utility. Dependency Walker provides a lot of valuable information about module layouts about a particular application. This utility also provides detailed information about each module. For more information, see the "DLL Dependence" section of this article. DLL UNIVERSAL PROBLVER Tools The DLL file version conflict (also known as "DLL Hell" problem), you can use the DLL UNIVERSAL SOLVER (DUPS) tool to find these issues and fix. The DUPS package is a utility set that uses it to track and compare multiple DLL versions on Windows-based computers. For more information, please visit the Microsoft Web site below: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsetup/html/dllDanger1.asp regsvr32 Using Regsvr32 Tools ( Regsvr32.exe) can be registered or logged out of the OLE control, such as a DLL control file or ActiveX control (OCX) file that can be registered. To troubleshoot in Windows, Microsoft Internet Explorer, or other programs, you may have to register or log out of these files. For additional information, click the article number below to see the article in the Microsoft Knowledge Base: 249873 Regsvr32 Usage and Error Message OLE / COM Object Viewer Using OLE / COM Object Viewer, you can pass the type library To view the interfaces of the control (DLL or OCX file). For more information, visit the following Microsoft Web site: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/vcrefUsingOLECOMObjectViewerToViewControlsInterfaces.asp .NET Framework Tools Microsoft The .NET Framework SDK Tool is designed to make you create, deploy, and manage applications and components that use .NET Framework more easily. .NET Framework provides four sets of tools: configuration and deployment tools, debugging tools, security tools, and regular tools. For more information, please visit the Microsoft Web site below: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptools/html/cpconnetFraMeworkTools.asp Back to top
DLL dependencies You can view the application's dependence on the application by using Dependency Walker. For example, Depends.exe is included in the Visual C .NET. Depends.exe installed in the / Microsoft Visual Studio .NET 2003 / Common7 / Tools / Bin folder. You can also install the Depends.exe executable when you select the Win32 Platform SDK tool when you select the Win32 Platform SDK tool in the Visual C tool category of Visual C .NET Custom installation.
Use the Depends.exe or Dumpbin utility and use
/ Debendents option, you can view static links to the DLL list of your application and a list of delayed DLLs. To see which DLLs (such as ActiveX controls) have been dynamically loaded, use the information summary feature of Depends.exe. For more information, please visit the Microsoft Web site below: http://msdn.microsoft.com/library/default.asp? Url = / library / en-us / vccore / html / vccondeeterminingwhichdllstoreDistribute.asp
Back to top
The comparison of Win32 and .NET assembly is in .NET Framework, the assembly is a logical EXE or DLL. Below is the difference between the assembly compared with Win32 DLL:
The code in these EXE or DLL files is generated by the language compiler. This code is not an X86 computer code or a computer code dedicated to any particular CPU operating system. This code is generated with Microsoft Intermediate Language (MSIL). MSIL can be written in assembly language. Microsoft provides an MSIL assembler (ILASM.EXE) and MSIL disassembler (ILDASM.exe). This DLL or EXE file contains metadata generated by the compiler. The public language runtime uses this metadata to find and load class types in the file, determine the layout of the object instance, parsing method calls, and field references, converts MSIL to native code, perform security settings. The component you generated is not just an exe file or a DLL file. They are the basic units of reuse and deployment, which is an assembly in .NET. You can generate single file assemblies or multi-file assemblies. In single file assembly, the file extension of the assembly is .exe or .dll. The multi-file assembly is such a thought: divide the code into multiple files by separating a logical concept and physical concept of a reusable component, to meet your needs. However, you can still reserve a collection as a unit of version control and deployment. For example, if you use multi-file assemblies, you can use increment and on-demand downloads. By using assemblies, developers and administrators can represent strict version dependence between components of the program. Since each component is described, you can create no impact installation. When installing components of the new application, they may override the components of the early applications. If this overrides happen, early applications may run abnormal or stop running. The .NET Framework architecture allows the applications to remain independent, so that the application is always able to load the correct components. Therefore, the application that can run in the expected operation after installation will continue to run. (This feature solves the "DLL Hell" issue.) The assembly is part of the .NET security system. The assembly is the unit of application and grant permission. In order to correctly implement version control, deployment, and security functions in the .NET Framework, the assembly plays a role in which the analysis range is used to resolve references to the type.
The various components of the assembly are described in the list. The list is a block, which lists the files of the assembly and controls which types and resources that are open to the assembly. For example, some types may remain in use only within the program. Other types may be able to export users from the assembly. .NET Framework parsing the type and resource parsing into a program set to the slave subsequence assembly in accordance with rules expressed in the assembly list.
Although .NET Framework is large and wide, there is still some features that can be performed using Win32 API, while using .NET Framework unable to execute. For example, you can use Win32 API
MessageBeep and
BEEP contains the functionality of generating noise in the application.
Back to top
The .NET connection client application uses Win32 DLL Export (PINVOKE) to use the Win32 DLL routine through the .NET connection application, use the Platform Call Service mechanism (also called platform calls or Pinvoke). This service is passed
System.Runtime.InteropServices a custom property defined in the namespace (
DllimportAttribute is running. This property allows the name of the DLL (and other required information) to declare with a process or function declaration. With this association, you can call the DLL routine. Component Object Model (COM) Interop Supports to automatically use the Interope-encapsulated spliter to automatically block the unpacking parameters and return values between the managed .NET environment and the non-hosting WIN32 environment.
To use the exported DLL function, follow these steps:
Identifies the name of the function name and export the DLL of the function. For example, by specifying the MessageBox function in user32.dll, you can identify the position of the function (MSSAGEBOX) and the function (user32.dll, user32 or user32). Create a class to pack the DLL function. Define a static method for each DLL function to be called. For example, you can call the MessageBox function in user32.dll, as shown in the following example: use system.Runtime.InterOpServices;
[DLLIMPORT ("User32.dll")]]]]]
Public Static Extern Int MessageBox (int hWnd, String text,
String Caption, Uint Type; use the DLLIMPORTATTRIBUTE property to identify the DLL and functions. Use an external "C" tag package method or function. The method in the hosted class is called by passing the members of the class defined in the hosted code. For example, pass the member of the MySystemTime class (these members are defined by order) to the getSystemTime method in the user32.dll file, as shown in the following example: void getSystemTime (SystemTime * SystemTime);
Class Win32API {
[DLLIMPORT ("kernel32.dll")]]]]
Public Static Extern void getSystemTime (MySystemTime ST);
} For more information, please visit the Microsoft Web site below:
Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconconsumingunmanageddllfunctions.asp
Back to top
Win32 client application uses the .NET assembly export (reverse pinvoke) To use the .NET assembly in the Win32 application, use the "Reverse Platform Call Service" mechanism. This service is used in the mechanism
Pinvoke uses mechanism reverse.
By using this mechanism, the public language runtime in .NET can disclose any .NET method to the outside (Win32) world. This mechanism performs the following:
Configure the .NET assembly as a corresponding intermediate language (IL) source code and metadata. Reissue the IL code, metadata, and resources in the .NET program set.
details
To disassemble the .NET assembly, you can use the .NET Framework IL disassembler (ILDASM.exe) included in the Framework SDK. For example, Myassembly.dll contains the following standalone routines, ie helloworld and hellomyworld: public static void helloworld ()
{
Console.writeline ("Hello World!");
}
Public static void hellomyworld ()
{
Console.writeline ("Hello My World!");
} To disconnect the assembly, use the following command line:
Ildasm myassembly.dll / linenum /out:.myassembly.il This command will generate a named IL file. It also stores any non-managed resources in myassembly.res files, and stores any managed resources in a file specified in the program column data. If the debug information is included in the PDB file,
The / linenum option enables the IL file to include references to the original source code row.
Back to top
From the disclosure method of the assembly, the assembly list must be changed for some changes to the assembly list. Before the clearance of the class declaration at the top of the IL file:
.Module myassembly.dll
// mvid: {140b1958-fc28-4802-80e3-74c0f2015cdc}
.imagebase 0x11000000
.subsystem 0x00000003
Alignment 512
.corflags 0x00000001 To make changes, do the following:
To define a V-Table fix and make it more than the slot that contains the way to export (two methods in the previous example), change the list as follows: Module Myassembly.dll
.Module myassembly.dll
// mvid: {140b1958-fc28-4802-80e3-74c0f2015cdc}
.imagebase 0x11000000
.subsystem 0x00000003
Alignment 512
.corflags 0x00000001
.DATA VT_01 = Int32 [2]
.vtfixup [2] int32 fromunmanaged at vt_01 .corflags command Set the run library header file flag. By default, the .corflags directive specifies the value 1. This value is equal to the comiMage_flags_ilonly flag (defined in the Corhdr.h in the .NET Framework SDK). Once this flag is set, the Windows XP loader ignores the main part of the assembly file, and does not correct. This will cause fatal errors when you try to export using the assembly. So, if you want the assembly to run on Windows XP, you must specify the comiMage_flags_32bitrequired flag (this value is 2): module myassembly.dll
.Module myassembly.dll
// mvid: {140b1958-fc28-4802-80e3-74c0f2015cdc}
.imagebase 0x11000000
.subsystem 0x00000003
Alignment 512
.corflags 0x00000002.Data vt_01 = int32 [2]
.vtfixup [2] INT32 fromUnmanaged AT VT_01 IL representation of these methods is currently displayed as follows: .method public hidebysig static void
HelloWorld () CIL Managed
{
// Code Size 11 (0xB)
.MAXSTACK 1
.line 18: 4
IL_0000: LDSTR "Hello World!"
IL_0005: Call void [mscorlib] system.console :: writeline (String)
.LINE 19: 3
IL_000A: RET
} // end of method class1 :: helloworld
Public Hidebysig static void
HellomyWorld () CIL Managed
{
// Code Size 11 (0xB)
.MAXSTACK 1
.line 23: 4
IL_0000: ldstr "Hello My World!"
IL_0005: Call void [mscorlib] system.console :: writeline (String)
.line 24: 3
IL_000A: RET
} // end of method class1 :: hellomyworld To make these methods as unmanaged, please change them as follows:
Public Hidebysig static void
HelloWorld () CIL Managed
{
// Code Size 11 (0xB)
.MAXSTACK 1
.line 18: 4
.vtenTry 1: 1
.export [1] as HelloWorld
IL_0000: LDSTR "Hello World!"
IL_0005: Call void [mscorlib] system.console :: writeline (String)
.LINE 19: 3
IL_000A: RET
} // end of method class1 :: helloworld
Public Hidebysig static void
HellomyWorld () CIL Managed
{
// Code Size 11 (0xB)
.MAXSTACK 1
.line 23: 4
.VTENTRY 1: 2
. EXPORT [2] as HellomyWorld
IL_0000: ldstr "Hello My World!"
IL_0005: Call void [mscorlib] system.console :: writeline (String)
.line 24: 3
IL_000A: RET
} // end of method class1 :: HellomyWorld Each method requires a .vntsRY instruction to link the method to the V-Table correction (bold display specified slot number), you need a .export instruction to specify the name of the exported name. .
To re-compose the code as a .NET assembly, use the IL assembler included in the .NET Framework (ILASM.exe). Use the following command line:
C: / temp / myassembly / bin / debug> iSM / DLL Myassembly.il /out:myassembly_new.dll / r
ES: myassembly.res
Microsoft (R) .NET Framework il assembler. Version 1.0.3705.0copyright (c) Microsoft Corporation 1998-2001. All Rights Reserved.
Assembling 'myassembly.il', no listing file, to dll -> 'myassembly_new.dll'
Source File Is Ansi
Assembled Method Class1 :: Main
AskEMBED METHOD CLASS1 :: HelloWorld
AskEMBED METHOD CLASS1 :: Hellomyworld
Assembled Method Class1 ::. Ctor
Creating pe file
Emitting MEMBERS:
Global
Class 1 Methods: 4;
Resolving Member Refs: 1 -> 1 Defs, 0 Refs
Writing pe file
Operation Completed SuccessFully
The main difficulties that may encounter when using reverse pinvoke are maintenance issues. If you want to modify the assembly generated by a creative round-trip, re-update the assembly to export routines.
Back to top
Refer to the Microsoft DLL Help Web Site to determine the specific version of the DLL installed by the application. This site provides information to developers, system administrators, and other IT professionals who may encounter file versions between Microsoft applications and other applications. For more information, please visit the Microsoft Web site below:
Http://support.microsoft.com/servicedesks/fileversion/dllinfo.asp