Friday, May 20, 2005

Installing a MAPI filter

I recently came across the challenge of writing an application that processed SMS messages. The MAPIRULE sample of the Pocket PC 2003 was duly studied, successfully modified and used. The real challenge came when I needed to cleanly install it and uninstall it. Installing is simple: you just copy the DLL to the \Windows folder, register it (the setup can do this for you) and then you have to soft-reset the device (a setup DLL is required for this). This is quite simple and a sample of soft-reset code can be found here.

Uninstalling calls for a different approach. First, you have to make sure that the DLL is not being used by the SMS-handling process. My answer to this is to shut down the tmail.exe process. Here is some code:

BOOL KillProcess(LPCTSTR pszProcName)
{
HANDLE hSnapshot;
BOOL bRet = FALSE;

hSnapshot = CreateToolhelp32Snapshot(
TH32CS_SNAPPROCESS, 0);
if((int)hSnapshot != -1)
{
BOOL bOk;
PROCESSENTRY32 pe;
DWORD dwID = 0;
HANDLE hProcess;

pe.dwSize = sizeof(PROCESSENTRY32);

for(bOk = Process32First(hSnapshot, &pe);
bOk;
bOk = Process32Next(hSnapshot, &pe))
{
if(!wcsicmp(pe.szExeFile, pszProcName))
dwID = pe.th32ProcessID;
}
CloseToolhelp32Snapshot(hSnapshot);

if(dwID != 0)
{
// Kill the process
hProcess = OpenProcess(0, FALSE, dwID);
if(hProcess != INVALID_HANDLE_VALUE)
{
bRet = TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
}
}
}
return bRet;
}


After killing the process, you have to unregister the DLL (sample code here) and finally soft-reset the device.

A sample setup dll project is available on demand.

2 comments:

Theeyattu said...

Is this post relevant to WM 5.0 and later ?

This is the thing. I have an app that uses SMS's for some its business logic. The SMS handling code is taken from the sample provided by PPC 2003. Now the problem is when I uninstall the application. Even though the DllUnregisterServer() call is provided, it does not unregister this dll from the SMS service and hence I cannot delete the dll.

You suggested that tmail.exe process be killed. Is this documented anywhere or is this from your experience ?

Thanks

João Paulo Figueira said...

The easiest way to close the mail client is to send a WM_CLOSE message to the top-level window whose class name is "Inbox.MainWnd". This will close the mail client and you will be able to unregister your DLL (hopefully).