通过命令行控制窗口置顶和结束指定进程工具c代码

使用示例:

程序名.exe set_top on|off 窗口标题

程序名.exe kill_exe 要结束的程序文件名.exe 正常结束等待时间(可选,毫秒) f(正常关闭不了,强制关闭,可选)

 

 

 

 

代码如下:

 

 

#include
#include

int is_tip(void);
int set_top(int argc, LPWSTR *argv);
int kill_exe(int argc, LPWSTR *argv);
DWORD get_process_id(const wchar_t* name_exe);
int kill_process_id( DWORD dwPID, DWORD dwTimeout ,BOOL close_exe);
BOOL CALLBACK TerminateAppEnum( HWND hwnd, LPARAM lParam );
 
int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    is_tip();
    return 0;
}

int is_tip(void)
{
  int argc;
  int is_err = 0;
  LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(),&argc);
  argc = argc - 1;//返回的是个数
  
  if (NULL == argv)
  {
       char int_c[100];
       itoa(GetLastError(),int_c,10);
       return MessageBox(NULL, int_c,"获取命令行出错[出错代码]" ,MB_OK);
  }
 
  if ( (argc < 1) ||
      ( (wcscmp(argv[1],L"set_top") != 0) && (wcscmp(argv[1],L"kill_exe") != 0) ) )
  {
            is_err = 1;
            MessageBox(NULL,"缺少操作类型/n格式如下/n程序路径//文件名.exe set_top|kill_exe","出错啦" ,MB_OK);
  }else if (wcscmp(argv[1],L"set_top") == 0)
   {
       is_err = set_top(argc, argv);
   }else if (wcscmp(argv[1],L"kill_exe") == 0)
   {
       is_err = kill_exe(argc, argv);
   }else
    {
        is_err = 1;
        MessageBoxW(NULL,argv[1],L"no this event" ,MB_OK);
     }
 
  if (is_err)
  {
       int for_i = 0;
       wchar_t for_i_c[100];
       MessageBoxW(NULL,GetCommandLineW(),L"the command_line is under",MB_OK);
          
       for (;for_i <= argc; for_i++)
       {
           _itow(for_i,for_i_c,10);
           MessageBoxW(NULL,argv[for_i],for_i_c ,MB_OK);
       }   
  }else
   {
     //MessageBoxW(NULL,argv[1],L"doned!",MB_OK);
   }

   LocalFree(argv);
}

int kill_exe(int argc, LPWSTR *argv)
{   
        if (argc < 2)
        {
            MessageBox(NULL,"缺少中止程序名/n格式如下/n程序路径//文件名.exe kill_exe xx.exe [数字] [f]             /n[数字]是可选,等待判断是否被关闭时长,过短会出现提示不能关闭后程序却被关闭了;/n[f]可选,等待[数字]毫秒后未正常关闭将强制关闭","出错啦" ,MB_OK);
            return TRUE;
        }       
       
        int time_out = 0;
        
        if ( (argc > 2) && ( (time_out = _wtoi(argv[3])) == 0) )
        {
            MessageBox(NULL,"错误强制中止程序超时参数/n格式如下/n程序路径//文件名.exe kill_exe xx.exe 数字","出错啦" ,MB_OK);
            return TRUE;
        } 
       
        BOOL close_exe = TRUE;
       
        if ( (argc > 3) && ( (close_exe = wcscmp(argv[4],L"f")) != 0) )
        {
            MessageBox(NULL,"错误强制中止程序参数/n格式如下/n程序路径//文件名.exe kill_exe xx.exe 数字 f","出错啦" ,MB_OK);
            return TRUE;
        }
       
        DWORD process_id = get_process_id(argv[2]);
       
       if((int)process_id < 0) 
       {
           MessageBoxW(NULL,argv[2],L"can't found under name_exe!",MB_OK);
           return TRUE;
        }
               
       return kill_process_id(process_id, time_out, close_exe);
}

 

int set_top(int argc, LPWSTR *argv)
{   
        if ((argc < 2)||
            ( (wcscmp(argv[2],L"on") != 0) && (wcscmp(argv[2],L"off") != 0) ) )
        {
            MessageBox(NULL,"缺少置顶状态/n格式如下/n程序路径//文件名.exe set_top on|off","出错啦" ,MB_OK);
            return TRUE;
        }
       
        if (argc < 3)
        {
            MessageBox(NULL,"缺少置顶窗口标题/n格式如下/n程序路径//文件名.exe set_top on|off /"窗口标题字符串/"","出错啦" ,MB_OK);
            return TRUE;
        }
       
        HWND hta = FindWindowW(NULL,argv[3]);
       
       if(!hta) 
       {
           MessageBoxW(NULL,argv[3],L"can't found under title window!",MB_OK);
           return TRUE;
        }
   
       HWND is_top;
      
       if (0 == wcscmp(argv[2],L"on"))
       {
           is_top = HWND_TOPMOST;
           //SetWindowPos(hta, HWND_NOTOPMOST, 0, 0, 0, 0,  SWP_NOMOVE|| SWP_NOREDRAW|| SWP_NOSIZE || SWP_NOACTIVATE);
       }else
        {
            is_top = HWND_NOTOPMOST;
       }
      
       SetWindowPos(hta, is_top, 0, 0, 0, 0,  SWP_NOMOVE|| SWP_NOREDRAW|| SWP_NOSIZE || SWP_NOACTIVATE);
       return FALSE;     
}

DWORD get_process_id(const wchar_t* name_exe)
{
  DWORD process_id = -1;
  HANDLE Snapshot;
  Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
  //获得某一时刻系统的进程、堆(heap)、模块(module)或线程的快照信息
  PROCESSENTRY32W processListStr;
  processListStr.dwSize = sizeof(PROCESSENTRY32W);
  BOOL return_value;
  return_value = Process32FirstW(Snapshot,&processListStr);
  //获得系统进程链表中第一个进程的信息
  while(return_value)
  {
   //MessageBoxW(NULL,processListStr.szExeFile,L"exefile!",MB_OK);
   if( wcsicmp(name_exe, processListStr.szExeFile) == 0 )
   { //比较进程名,如果此进程与传入的进程名相同,那么就找到了需要的进程信息
    process_id = processListStr.th32ProcessID;
    break;
   }
   return_value = Process32NextW(Snapshot, &processListStr);
   //获得系统进程链表中下一个进程的信息
  }
 
  CloseHandle( Snapshot );
  return process_id;
}

int kill_process_id( DWORD dwPID, DWORD dwTimeout ,BOOL close_exe)
{            
      if (dwTimeout < 0)dwTimeout = 0;
     
      wchar_t int_str[100];
      _itow(dwPID,int_str,10);          
      HANDLE   hProc ;

      // 如果无法用 PROCESS_TERMINATE 权限打开进程,那么立即放弃。
      hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE,dwPID);

      if(hProc == NULL)
      {
           MessageBoxW(NULL,int_str,L"can't OpenProcess Process_id!",MB_OK);
           return 0;
      }

      // TerminateAppEnum() 将 WM_CLOSE 消息发到所有其进程ID 与你所提供的进程ID 匹配的窗口.
      EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM) dwPID) ;

      // 等待处理,如果成功,OK。如果超时,则干掉它.
      if(WaitForSingleObject(hProc, dwTimeout)!=WAIT_OBJECT_0)
      {
        if (!close_exe)
        {
          if (TerminateProcess(hProc,0))
          {           
            //MessageBoxW(NULL,int_str,L"SUCCESS_KILL Process_id!",MB_OK);
          }else
           {
               MessageBoxW(NULL,int_str,L"can't KILL Process_id!",MB_OK);
           }
        }else
         {
             MessageBoxW(NULL,int_str,L"can't close Process_id!",MB_OK);
         }
      }else
         //MessageBoxW(NULL,int_str,L"SUCCESS_close Process_id!",MB_OK);

      CloseHandle(hProc) ;

      return 0 ;
}


BOOL CALLBACK TerminateAppEnum( HWND hwnd, LPARAM lParam )
{
      DWORD dwID ;

      GetWindowThreadProcessId(hwnd, &dwID) ;

      if(dwID == (DWORD)lParam)
      {
         PostMessage(hwnd, WM_CLOSE, 0, 0) ;
      }

      return TRUE ;
}
  

你可能感兴趣的:(通过命令行控制窗口置顶和结束指定进程工具c代码)