Thursday, November 8, 2012

Find a windows program running and kill it when the condition is not meet


HWND hWnd; 
DWORD procId; 
HANDLE hProc; 

if ( (hWnd = FindWindow (NULL, "Calc")) &&  GetWindowThreadProcessId(hWnd, &procId)) 
{
if ( hProc = OpenProcess (PROCESS_ALL_ACCESS, FALSE, procId) ) 
{
// Get full address of the program by Program Id
  TCHAR filename[MAX_PATH];
GetModuleFileNameEx(hProc, NULL, filename, MAX_PATH);

// This is the condition to get modified date
struct tm* clock;
char   buf[100]; 
struct stat attrib;

stat(filename, &attrib);
clock = localtime(&(attrib.st_mtime));
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", clock);

   // Kill if the modified date is before year 2012 if (clock->tm_year < 112)
{
if ( TerminateProcess(hProc, 0) ) 
sprintf(pTemp, "Not Genuine Program found. Closed!\n");
else
sprintf(pTemp, "Not Genuine Program found. Failed to closed!\n");
}

CloseHandle (hProc); 
}
}

No comments: