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

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?
pcssecureAsked:
Who is Participating?
 
DarthModConnect With a Mentor Commented:
PAQed, with points refunded (250)

DarthMod
Community Support Moderator
0
 
anupvijayCommented:
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
0
 
stefan73Commented:
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
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
jkrCommented:
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")
0
 
pcssecureAuthor 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.
0
 
anupvijayCommented:
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
0
 
pcssecureAuthor 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));
}
0
 
anupvijayCommented:
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
0
 
jkrCommented:
>>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'
0
 
pcssecureAuthor 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
0
 
pcssecureAuthor 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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.