[Qt-interest] How to close a process running on windows !

Constantin Makshin cmakshin at gmail.com
Tue Sep 14 20:13:51 CEST 2010


Please check the documentation on the TerminateProcess() function:

The handle must have the PROCESS_TERMINATE access right.

Also, you don't check the handle returned by the OpenProcess() function. That's not a good idea. :)

On Tuesday 14 September 2010 15:20:42 Santhosh Y wrote:
> Hi,
> 
> This is in context of closing nxdatcom.exe process.
> 
> I am able to get the handle to the process in which I am interested in 
> closing, but when I call TerminateProcess() it is returning me false.
> That means it is not able to terminate the process. Any idea on 
> alternative !
> 
> ////////////////////////////////////////////////////////////////////////////////////////////////////
> 
>      bool isRunning()
>      {
>          unsigned long aProcesses[1024], cbNeeded, cProcesses;
>          if(!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
>              return false;
> 
>          cProcesses = cbNeeded / sizeof(unsigned long);
> 
>          for(unsigned int i = 0; i < cProcesses; i++) {
>              if(aProcesses[i] == 0)
>                  continue;
> 
>              HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | 
> PROCESS_VM_READ, 0, aProcesses[i]);
> 
>              wchar_t buffer[50];
> 
>              GetModuleBaseName(hProcess, 0, buffer, 50);
> 
>              std::string str;
>              for (int i=0; i < 50; i++) {
>                  str.push_back (buffer[i]);
>              }
> 
> *// std::cout << str << std::endl;
>              // Able to print  all the exe's running over my machine*
> 
>              if(strcmp(str.c_str(), "nxdatcom.exe") == 0) {
> 
>                  //CloseHandle(hProcess);
> 
> * BOOL b = TerminateProcess( hProcess, 0); // This is failing; Any idea 
> on alternative !*
> 
>                  if (b == false) {
>                     std::cout <<  GetLastError() << std::endl;
>                  }
>                  return b;
>              }
>          }
>          return false;
>      }



More information about the Qt-interest-old mailing list