Upgrade the dynamic link library and create a simple dynamic link library, use the dynamic link when loaded, use the runtime dyn

xiaoxiao2021-03-06  14

Upgrade dynamic link library

Sometimes you need to replace a DLL upgrade to a new version. Check that the confirmation is indeed a newer version before replacing the DLL. Sometimes you may have to replace a DLL that is using. Replacing the DLL method varies depending on the operating system, Windows XP and subsequent versions, applications should use independent programs and parallel systems.

Follow the steps below to upgrade, you do not have to restart your computer:

Rename the DLL to be replaced with TabINDEX = "0" keywords = "0" = "0" keywords = "_ win32_movefileex" error = "./ common / badjump.htm"> MoveFileEx function. Don't specify MoveFile_copy_allowed and determine that the redistributed file is still in the original volume. You can also rename the current directory by modifying the extension name. Copy the new DLL to the directory containing the rename DLL (the translator's note: should be the original directory before the DLL). Now all applications will use new DLLs. Call MoveFileEx delete the renamed DLL with MoveFileEx_until_reboot.

Before implementing the replacement, the application will always know the UNLOAD. Once the replacement is implemented, the application will use a new DLL. When writing a DLL, it is especially careful to confirm that this situation, especially the DLL maintains global status information or makes communication with other services, more need to be considered. If you have not prepared for a global state or communication protocol, upgrade DLL requires you to restart your computer to be confident that all applications are using the same version of the DLL.

Windows Me / 98/95: Due to tabindex = "0" keywords = "_ win32_movefileex" erroRURL = "../ common / badjump.htm"> MoveFileEx is not supported, upgrade dynamic link library need to restart the computer, more details, Detailed description of MoveFileex can be referred to.

Create a simple dynamic link library

The following example, myputs.c, contains the source code required to create a simple DLL (MyPuts.dll), and a simple character display function is included in Myputs.c -myputs. MyPuts DLL does not define an inlet function because it is connected to the C running library and does not implement itself initialization and clear functions.

// File: myputs.c.

// the myputs function Writes a null-terminated string to

// The Standard Output Device.

#include

#define EOF (-1)

INT Myputs (LPTSTR LPSZMSG)

{

DWORD cchwritten;

Handle hstdout;

Bool fret;

// Get a handle to the standard output device.

HSTDOUT = GetStdHandle (std_output_handle);

IF (Invalid_Handle_Value == HSTDOUT)

Return EOF;

// Write a null-terminated string to the standard output device.

While (* lpszmsg! = '/ 0')

{

Fret = Writefile (HSTDOUT, LPSZMSG, 1, & Cchwritten, NULL);

IF ((false == fret) || (1! = cchwritten) Return EOF;

LPSZMSG ;

}

Return 1;

}

Finally, you need to generate a DLL, you will need to do it in accordance with the instructions in the development tool documentation.

Dynamic link when using loading

Once you have created a DLL, you can use it in a program. The following file -LoadTime.c implements a simple console application using myPuts function in Myputs.dll.

// file: loadingtime.c.

// a Simple Program That Uses myputs from myputs.dll.

#include

Void myputs (lptstr); // a Function from A DLL

Int main (void)

{

Int rett = 1;

Ret = Myputs ("Message Printed Using The DLL Function / N");

Return Ret;

}

Since LOADTIME.C explicitly calls the DLL function, the application module must be connected to myPuts.lib. Create a DLL information, you can refer to the development tool-related documentation.

Use the running dynamic link

You can use the same DLL when loaded or during run. The following source code implements the same output as the example of the previously loaded load. The program utilizes LoadLibrary to get myPuts.dll's handle, LoadLibrary is successful, and the program calls the GetProcAddress function to get the MyPuts function address in the DLL via this handle. After calling the DLL function, the program calls the FREELIBRARY function release the DLL.

The next example demonstrates an important difference between dynamic links and dynamic links during operation. If the myputs.dll file is not available, the program that is dynamically connected when using the load directly terminates, and the program when the runtime can report an error generation.

// file: runtime.c

// a Simple Program That Uses LoadLibrary and

// GetProcaddress to access myputs from myputs.dll.

#include

#include

Typedef void (* myproc) (LPTSTR);

Void main (void)

{

Hinstance hinstlib;

MyProc Procadd;

Bool ffreeresult, fruntimelinksuccess = false;

// Get a Handle to the DLL Module.

Hinstlib = loadingLibrary ("Myputs");

// if The Handle Is Valid, Try to Get The Function Address.

IF (Hinstlib! = NULL)

{

PROCADD = (MyProc) GetProcaddress (Hinstlib, "Myputs");

// if the function address is valid, call the function.

IF (NULL! = Procadd)

{

Fruntimelinksuccess = true;

"" "Message Via DLL Function / N");

}

// free the dll module.

FFreeResult = freeElibrary (Hinstlib);

}

// if unable to call the dll function, use an alternative.if (! Fruntimelinksuccess)

Printf ("Message Via Alternative Method / N);

}

Since the dynamic link is used, do not connect to the library again.

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

New Post(0)