Friday, 28 May 2010

Exception in Toolhelp.dll

Hi all,
recently i've written a simple taskmanger and used the toolhelp.dll to enumerate the processes running on my target. 
Here is my sample:
       // PROCESSENTRY32 saves the information of one running process
       PROCESSENTRY32 pe32 = {0};
       HANDLE hSS; 
       // Create a snapshot of your system 
       hSS = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS|
                                                         TH32CS_SNAPNOHEAPS, 0);

             return FALSE;
       //prepare the enumeration 
      pe32.dwSize = sizeof(PROCESSENTRY32);
       //now get the first process running
       BOOL bRet = Process32First(hSS, &pe32);
       //iterate through all proccesse in the snapshot
       while(FALSE != bRet)
             if( pe32.dwSize != sizeof(PROCESSENTRY32) )
            // get the next process running 
            bRet =Process32Next(hSS, &pe32);
       //close the snapshot.

My problem was that when i called  the Process32First function a data abort exception has been thrown in the toolhelp.dll. On my development machine CE5 and CE6 are installed in parallel which works well. But, for my application debugging, VisualStudio 2005 and/or one of my Remote Tools copied the toolhelp.dll located in "\Program Files\CE Remote Tools\5.01\target\wce500\armV4i"  to  my target. It replaced the CE6 toolhelp.dll with the CE5 one from the folder mentioned above. The problem is, that Microsoft has changed the PROCESSENTRY32 struct Two new members have been added.
So with the initialization of my PROCESSENTRY32 struct (pe32.dwSize = sizeof(PROCESSENTRY32);)  I  passed 
the CE5 toolhelp Process32First function a struct with the size of the CE6 PROCESSENTRY32 struct. This results in a DataAbort exception inside the Process32First call because the CE5 Process32First function can not handle a CE6 PROCESSENTRY32 struct.
So if you get an exception in one of the toolhelp functions, check the version of  your dll, first.

No comments: