We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Unload DLL module loaded by Device manager (device.exe)

pcssecure
pcssecure asked
on
Medium Priority
321 Views
Last Modified: 2013-12-03
I have written a DLL in eVC 3.0 under pocketpc 2002. It is loaded with LoadLibrary(..) by program A.
This DLL module is attached to Device.exe.
Is there a way to unload this DLL module withOUT soft-resetting the PDA?
Comment
Watch Question

Hi pcssecure
I am not aware of the pocket PC much. But I know that there is a FreeLibrary function which unmaps the library module from
the address space or in effect unload the DLL.
Hope it helps.
Cheers,
Anup
Top Expert 2004

Commented:
Hi pcssecure,
Yes, FreeLibrary is also available for CE:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcesdkr/html/wcesdkrfreelibrary.asp

Try also

BOOL AFXAPI AfxFreeLibrary(
HINSTANCE hInstLib );

As usual, this returns TRUE on success.
Cheers,
Stefan
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
You will need to terminate 'program A' (if you are not 'inside' it and can call 'FreeLibrary()'). See http://www.microsoft.com/technet/prodtechnol/wce/maintain/threads.mspx ("Working With Processes and Threads in MS Windows CE") and http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcesdkr/html/wcesdkrTerminateProcess.asp ("TerminateProcess")

Author

Commented:
I forgot to mention unloading of the DLL is to be done by another program.

There is no problem calling FreeLibrary within program A.

Although program A loads the DLL via LoadLibrary(..) , the DLL is loaded by Device manager (aka device.exe).
I don't think terminating Device.exe is a good idea.

I have used CreateToolhelp32Snapshot(..) to retrieve handles to terminate both program A and the DLL module in question.
Using the handle to the module, I called FreeLibrary() but it failed.
Did you use the handle returned by the CreateToolhelp32Snapshot() or did you actually find the specific handle
of the dll process inside the snapshot ? I mean did you use a Process32First and Process32Next call to actually
get a handle to the DLL process structure?

If you used the handle returned by CreateToolhelp32Snapshot then FreeLibrary would fail for obvious reasons.

Let us know pcssecure.

Cheers,
Anup

Author

Commented:
Anup,

I used Process32First and Process32Next as well as Module32First & Module32Next.

First I killed program A which loaded the DLL in question, which was achieved.

However, the DLL module is under device.exe. I used the following code snippet to find device.exe and the DLL handle.
I am able to find both handles of the process and the DLL in question.
...
if (Process32First(hProcessSnap, &pe32)){
  do {
          wcscpy(szFile,pe32.szExeFile);
          if(wcscmp(szFile,szDevice)==0) {
      MessageBox(NULL,TEXT("Found device.exe"),NULL,MB_OK);
      dwPid = pe32.th32ProcessID;                        
      MODULEENTRY32 me32 = {0};
      me32.dwSize = sizeof(MODULEENTRY32);                        
      HANDLE hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,dwPid) ;
               if(hModuleSnap) {
         if (Module32First(hModuleSnap , &me32))
         do {                                             
                      if (wcscmp(me32.szModule,szPWR)==0) { //szPWR holds the module filename
                MessageBox(NULL,me32.szModule,NULL,MB_OK); //this msgbox will appear                                                
                if(FreeLibrary(me32.hModule))
            MessageBox(NULL,L"killed DLL",NULL,MB_OK);
                else { //then this fires.
            wsprintf(szFile,TEXT("last errorcode %u"),GetLastError());
            MessageBox(NULL,szFile,NULL,MB_OK);
                           }

                 }
                                    
           } while (Module32Next(hModuleSnap , &me32));
             CloseToolhelp32Snapshot(hModuleSnap);
      }
              }

       }while (Process32Next(hProcessSnap, &pe32));
}
Have you tried CloseHandle(me32.hModule) instead of FreeLibrary(me32.hModule) ?
CloseHandle also decrements the reference count as FreeLibrary does and if it is the last reference will remove the
object from memory.
Worth trying........

All the best
Anup
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
>>I have used CreateToolhelp32Snapshot(..) to retrieve handles to terminate both program A and the DLL
>>module in question.

Once 'program A' is terminated, the DLL will be freed automatically (unless it is loaded by another app)

>>Using the handle to the module, I called FreeLibrary() but it failed.

The handle is not valid outside 'program A'

Author

Commented:
jkr,

I have terminated program A and program A called LoadLibrary() the DLL, the DLL is still resident. It is because the DLL is a stream interface driver and it gets loaded by winCE Device manager (device.exe). I have verified it is so by using remote Process viewer provided by eVC.

Anup,

Thanks will give it a try.

Best Rgds

Author

Commented:
Anup,

Tried CloseHandle, returned a error code 6 which translate to "This handle is invalid", btw using FreeLibrary call, the error code translates to "This function is not supported on this system. ".

After some searching on newsgroup i found the following:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=439c01c375f3%2469c49720%24a501280a%40phx.gbl&rnum=6&prev=/groups%3Fq%3Ddevice%2Bfreelibrary%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8

I am beginning to believe that the only way is to soft-reset.

Best Rgds.
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.