locked
CreateToolhelp32Snapshot sometimes fails with errorcode 87 (invalid parameter) RRS feed

  • Question

  • Hello,

    I use CreateToolhelp32Snapshot to detect a speciffic process and get his handle. However, when this process is waiting for something, CreateToolhelp32Snapshot returns with Invalid_Handle and error 87 and when the process is idle, everything works fine.

    Are there conditions where this function shouldn't be used?

    This is my code:

    BOOL StopRuntime(void)
    {
    	BOOL bRunning = FALSE;
    	HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    	TCHAR* szTelnetSrv = _T("Runtime.exe");
    	HANDLE hProcess = INVALID_HANDLE_VALUE;
    	DWORD dwWaitReturn;
    
    	if(hSnapShot == INVALID_HANDLE_VALUE )
    	{
    		RETAILMSG(TRUE, (TEXT("SystemUpdate: StopRuntime, get Processlist failed; Error %d \r\n"),GetLastError()));
                    //Workaround
    		HWND hWnd = ::FindWindow(NULL,_T("Runtime Window Name"));
    		if(hWnd != NULL)
    		{
    			::PostMessage(hWnd,WM_JVER_CLOSE,0,0);
    			Sleep(2000);
    		}
    		else
    		{
    			RETAILMSG(TRUE, (TEXT("SystemUpdate: StopRuntime, failed to get window handle \r\n")));
    		}
    		return bRunning;
    	}
    
    	PROCESSENTRY32* pProcess = new PROCESSENTRY32;
    	memset(pProcess,0,sizeof(PROCESSENTRY32));
    
    	pProcess->dwSize = sizeof(PROCESSENTRY32);
    	if(Process32First(hSnapShot, pProcess))
    	{
    		while(TRUE)
    		{
    			
    			if (_wcsicmp(pProcess->szExeFile, szTelnetSrv) == 0)
    			{
    				hProcess = OpenProcess(0, FALSE, pProcess->th32ProcessID);
    				break;
    			}
    
    			if(!Process32Next(hSnapShot, pProcess))
    			{
    				break; 
    			}
    		}
    	}
    
    	if(hProcess != INVALID_HANDLE_VALUE)
    	{
    		HWND hWnd = ::FindWindow(NULL,_T("Runtime Window Name"));
    		if(hWnd != NULL)
    		{
    			::PostMessage(hWnd,WM_JVER_CLOSE,0,0);
    			//RETAILMSG(TRUE, (TEXT("USB: Embedded Runtime detected and closed.\r\n")));
    			dwWaitReturn = WaitForSingleObject(hProcess,5000); // Wait 5s, runtime needs ca 2s to shut down, higher Values cause exception
    			CloseHandle(hWnd);
    			if (dwWaitReturn == WAIT_OBJECT_0)
    			{	// Process closed
    				CloseHandle(hProcess);
    				bRunning = TRUE;
    			} 
    			else if (dwWaitReturn == WAIT_TIMEOUT)
    			{	// Terminate 
    				if (hProcess)
    				{
    					if (TerminateProcess(hProcess, 0))
    					{	// Terminate success
    						RETAILMSG(TRUE, (TEXT("SystemUpdate: StopRuntime, Runtime terminated\r\n")));
    						bRunning = TRUE;
    					} 
    					else
    					{	// Terminate failed 
    						
    						RETAILMSG(TRUE, (TEXT("SystemUpdate: StopRuntime, Runtime Terminate failed\r\n")));	
    						bRunning = FALSE;
    					}
    					CloseHandle(hProcess);
    				}
    				
    			} 
    			else
    			{	// unknown Error
    				CloseHandle(hProcess);
    				RETAILMSG(TRUE, (TEXT("SystemUpdate: StopRuntime, WaitForSingleObject unknown Error %x\r\n"),dwWaitReturn));
    				bRunning = FALSE;
    			}
    		}
    		else
    		{	// ERROR WINDOW HANDLE not found
    			RETAILMSG(TRUE, (TEXT("SystemUpdate: StopRuntime, Error Window Handle not found\r\n")));
    			bRunning = FALSE;
    		}
    	}
    	else
    	{	// ERROR Process not found 
    		RETAILMSG(TRUE, (TEXT("SystemUpdate: StopRuntime, Error Process not found\r\n")));
    		bRunning = FALSE;
    	}
    
    	CloseToolhelp32Snapshot(hSnapShot);
    	delete pProcess;
    	return bRunning;
    }

    Tuesday, March 13, 2012 12:49 PM

Answers

  • CreateToolhelp32Snapshot was the Problem.

    The heap inforamtion from the processes were included in the Snapshot and so it exceeded 1 MB and failed.

    Solution:

    #ifndef TH32CS_SNAPNOHEAPS
    // define missing in Tlhelp32.h, but needed 
    #define TH32CS_SNAPNOHEAPS 0x40000000
    #endif
    
    ...
    
    BOOL StopRuntime(void)
    {
    	...
    	HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0);
    	...
    }
    

    • Marked as answer by John Bober Wednesday, March 14, 2012 2:13 PM
    Wednesday, March 14, 2012 2:12 PM