Click here to Skip to main content
5,788,212 members and growing! (18,435 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » General     Intermediate License: The Code Project Open License (CPOL)

How to Unload the Input Method DLL on Pocket PC

By Aleh

Shows how to unload the Input Method (IM) DLL for the Software Input Panel (SIP) on Pocket PC, which is important to Input Method uninstallers
C++, eVC 3.0, eVC 4.0, eVC, Windows, Win Mobile, CE .NET 4.2, Visual Studio, Dev

Posted: 26 Apr 2007
Updated: 5 Jan 2008
Views: 13,006
Bookmarked: 11 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 2.99 Rating: 3.84 out of 5
1 vote, 16.7%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
2 votes, 33.3%
4
3 votes, 50.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Many developers who wrote their own Input Methods (IM) for the Software Input Panel (SIP) on Pocket PC, or those who developed uninstallers for custom Input Methods faced with the problem of unloading their DLL's in order to update them or remove from the device. However it seems a bit hard to find any information on how to achieve this without rebooting the system. This short article aims to solve this problem.

Background

According to the official documentation the SIP should release the Input Method COM object just after calling of IInputMethod->Deselect() method, i.e. just after another Input Method is selected. And I can confirm the SIP does release it. So it seems that the easiest method would be to simply select another Input Method. This does not work however.

As you know, the COM subsystem does not unload unused DLL's immediately. Instead, it waits for the user code to call CoFreeUnusedLibraries() or CoFreeUnusedLibrariesEx(). The SIP does call the first function, but, unfortunately, this is not enough to unload all unused input methods immediately. Inside this function the COM subsystem calls DllCanUnloadNow() routine for all DLL's that do not seem to be used anymore and puts all those DLL's that returned S_OK (DLL can be unloaded) into a list of candidates for unloading. When a DLL from this list is needed to the COM again (for example, the application requests to create a new COM object that sits in this DLL) the DLL is removed from this candidate list. If some DLL is still sitting in the candidate list when either CoFreeUnusedLibraries(), or CoFreeUnusedLibrariesEx() is called again, this DLL finally gets unloaded if and only if enough time has passed since putting the DLL into the list.

When using CoFreeUnusedLibrariesEx() this time interval can be specified as the first parameter, but for CoFreeUnusedLibraries() used by the SIP this timeout is the system default value, which is 10 minutes! Quite a long time to ask the user to wait for...

Solution

One of the possible ways to workaround this problem would be to call CoFreeUnusedLibrariesEx() from the same process where the SIP lives with zero or near to zero timeout parameter. Fortunately, this is easily to achieve, because the SIP is implemented as a device driver and resides where all device drivers reside--in the device.exe process. So we can create a simple driver, which calls needed function(s), then load it and enjoy the possibility to remove our DLL. Sure, the input method should be deselected before this. There is also another trick.

The driver would be simpler, if we could call CoFreeUnusedLibrariesEx() from the Init() entry point. This does not work, because the COM subsystem needs the owner process ID to be equal to the current process ID (device.exe), for which we need DLL's to be unloaded. However the owner process ID inside the Init() call is equal to the ID of process which loaded our driver (i.e. our example program), and therefore COM tries to unload COM DLL's for this example application.

To overcome this, we just need to create another thread and do our call from the context of this thread. Owner process ID for this thread is equal to the ID of device.exe, where the SIP and Input Methods live.

Using the code

The example can be compiled with eVC++ 4.0. There are two projects in the archieve: a simple device driver, which when loaded calls CoFreeUnusedLibrariesEx() as described above; and a simple executable that loads this driver.

The driver exposes a minimal set of required entry points (Init/Deinit, Open/Close, IOControl), because we just need it to be loaded into the device.exe, nothing more.

Notes

When it is needed to unload the Input Method DLL while an application is being uninstalled from the device via the Remove Programs applet, it is possible to use a Custom Setup DLL as a driver itself by providing needed entry points, i.e. there is no need in a separate DLL.

I hope this article will be helpful to Input Method writers. Do not hesitate to comment or ask any questions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Aleh


I am an independent professional based in Minsk, Belarus, specializing in embedded and in particular Windows Mobile related software development. I have extensive experience in projects ranging from Windows CE drivers and firmware for AVR and PIC microcontrollers to end-user Windows Mobile and J2ME applications.

If you have any questions related (or unrelated) to my article then feel free to contact me.
Occupation: Software Developer
Location: Belarus Belarus

Other popular Mobile Development articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
GeneralGreat!memberBitBlaster23:17 9 Sep '08  
QuestionCan CoFreeUnusedLibrariesEx() help with DLL load errors?memberUdara_G6:23 21 Aug '07  
AnswerRe: Can CoFreeUnusedLibrariesEx() help with DLL load errors?memberAleh10:35 22 Aug '07  
GeneralRe: Can CoFreeUnusedLibrariesEx() help with DLL load errors?memberUdara_G20:32 22 Aug '07  
GeneralThanks a lotmemberQuentin Pouplard8:49 4 Aug '07  
GeneralRe: Thanks a lotmemberAleh9:03 4 Aug '07  
GeneralI could not download the source filesmemberschwarz.works11:25 4 May '07  
GeneralRe: I could not download the source filesmemberAleh13:42 4 May '07  
QuestionCan you help with "the IM Select" Problem?memberschwarz.works11:21 4 May '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Jan 2008
Editor:
Copyright 2007 by Aleh
Everything else Copyright © CodeProject, 1999-2009
Web09 | Advertise on the Code Project