Solved

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

Posted on 2004-04-06
12
268 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?
0
Comment
Question by:pcssecure
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 2

Expert Comment

by:anupvijay
Comment Utility
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
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 

Author Comment

by:pcssecure
Comment Utility
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
 
LVL 2

Expert Comment

by:anupvijay
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:pcssecure
Comment Utility
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
 
LVL 2

Expert Comment

by:anupvijay
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>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
 

Author Comment

by:pcssecure
Comment Utility
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
 

Author Comment

by:pcssecure
Comment Utility
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
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
Comment Utility
PAQed, with points refunded (250)

DarthMod
Community Support Moderator
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now