Solved

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

Posted on 2004-04-06
12
269 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SCANF - LIMIT THE NUMBER OF CHARARACTERS 1 60
USPS intelligent mail barcode font 11 133
font missing in acrobat 3 70
what font behind pdf 32 128
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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

895 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

15 Experts available now in Live!

Get 1:1 Help Now