Go to: secure exit of the thread in freelibraryandexitthread DLL

Source: Internet
Author: User

First, let's take a look at the freelibraryandexitthread API.

Void freelibraryandexitthread (
Hmodule,
DWORD dwexitcode
);

Msdn: The freelibraryandexitthread function decrements the reference count of a loaded dynamic-link library (DLL) by one, then CILS exitthread to terminate the calling thread. The function does not return.

 

This API reduces the reference count of the dynamic library and then calls the exitthread function to end the calling thread. This API does not return a value. (Inserting a little bit of knowledge, loadlibrary and LoadLibraryEx will increase the reference count of a dynamic library, and if the load operation is performed on a completely identical DLL in the same process, the reference count of the DLL module is increased, and the entry point function is not called. When the reference count of a dynamic library is reduced to 0, the module is detached from the process .)

 

One of the most troublesome problems encountered previously is that if a thread is started in the DLL and when the DLL is going out of freelibary, if the thread in the DLL has not exited, the program will crash immediately because the code space of the entire DLL module is no longer valid after freelibary. If the DLL thread continues to run, it will naturally crash. In this case, loading a DLL in a function is the most obvious. The time to release the DLL is a waste of time. Some methods used previously have to query the DLL to check whether the thread has exited, or use methods such as killing the threads in the DLL.

Today, we found that the freelibraryandexitthread API handles this problem very well. This API was born to handle this problem. The method is as follows: the thread process for calling the DLL remains unchanged, and the loadlibrary remains unchanged, freelibary before the function exits, and add an uninstallation mechanism to the DLL. The Code is as follows:

 

[CPP]
View plaincopyprint?
  1. # Include "stdafx. H"
  2. # Include <stdio. h>
  3. # Include <windows. h>
  4. HmoduleG_hdll = NULL;
  5. DWORDWinapi freeselfproc (PvoidParam)
  6. {
  7. Printf ("unloadproc! /N ");
  8. : MessageBox (null, text ("Press OK to unload me ."),
  9. Text ("msgbox in DLL"), mb_ OK );
  10. // Freelibrary (g_hdll );
  11. // Exitthread (0 );
  12. : Freelibraryandexitthread (g_hdll, 0 );
  13. Return0;
  14. }
  15. BoolApientry dllmain (HandleHmodule,
  16. DWORDUl_reason_for_call,
  17. LpvoidLpreserved
  18. )
  19. {
  20. If(Dll_process_attach = ul_reason_for_call)
  21. {
  22. CharSzpath [max_path + 1] = {0 };
  23. : Getmodulefilename ((Hmodule) Hmodule, szpath, max_path );
  24. : Loadlibrary (szpath );
  25. Printf ("dll_process_attach! /N ");
  26. G_hdll = (Hmodule) Hmodule;
  27. HandleHthread =: createthread (null, 0, freeselfproc, null, 0, null );
  28. : Closehandle (hthread );
  29. }
  30. ReturnTrue;
  31. }

# Include "stdafx. H "<br/> # include <stdio. h> <br/> # include <windows. h> </P> <p> hmodule g_hdll = NULL; </P> <p> DWORD winapi freeselfproc (pvoid PARAM) <br/>{< br/> printf ("unloadproc! /N "); <br/>: MessageBox (null, text (" Press OK to unload me. "), <br/> text (" msgbox in DLL "), mb_ OK); <br/> // freelibrary (g_hdll ); <br/> // exitthread (0); <br/>: freelibraryandexitthread (g_hdll, 0); <br/> return 0; <br/>}</P> <p> bool apientry dllmain (handle hmodule, <br/> DWORD ul_reason_for_call, <br/> lpvoid lpreserved <br/>) <br/>{< br/> If (dll_process_attach = ul_reason_for_call) <br/>{< br/> Cha R szpath [max_path + 1] = {0}; <br/>: getmodulefilename (hmodule) hmodule, szpath, max_path); <br/> :: loadlibrary (szpath); <br/> printf ("dll_process_attach! /N "); <br/> g_hdll = (hmodule) hmodule; <br/> handle hthread =: createthread (null, 0, freeselfproc, null, 0, null ); <br/>:: closehandle (hthread); <br/>}< br/> return true; <br/>}

 

Principle: Load yourself at the DLL entry point, increase the reference count by 1, and call freelibraryandexitthread at the end of the thread function to uninstall itself and exit the thread, if the thread that calls the DLL calls freelibary to release the DLL, the DLL will not be uninstalled because the reference count is still greater than 0, the thread functions in the DLL can safely run and know that they are uninstalling themselves. If the thread functions in the DLL call freelibraryandexitthread first, the DLL will not be uninstalled because the DLL reference technology is greater than 0, the thread exits Until freelibary is called to uninstall it.

Note that freelibary cannot be called in the DLL thread function to uninstall it, because the returned result of freelibary is still the DLL code space. If the DLL is uninstalled, the returned space is an invalid code space and the program will crash immediately.

Related Article

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.