Solved

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

Posted on 2004-04-06
12
274 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
ID: 10764738
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
ID: 10765453
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
ID: 10765490
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pcssecure
ID: 10771358
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
ID: 10773144
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
 

Author Comment

by:pcssecure
ID: 10773310
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
ID: 10773625
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
ID: 10774603
>>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
ID: 10781200
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
ID: 10781273
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
ID: 11464563
PAQed, with points refunded (250)

DarthMod
Community Support Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-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.

735 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