Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1027949
  • 博文数量: 288
  • 博客积分: 10306
  • 博客等级: 上将
  • 技术积分: 3182
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-12 17:00
文章分类

全部博文(288)

文章存档

2011年(19)

2010年(38)

2009年(135)

2008年(96)

我的朋友

分类: WINDOWS

2009-09-17 15:01:52

一.执行DOS命令

要在程式執行時啟動其他的應用程式有幾個做法,這裡先來看看最簡單使用的兩個 API 。   
  1:   UINT   WinExec   (   LPCSTR   lpCmdLine   , //   address   of   command   line     
         UINT   uCmdShow     //   window   style   for   new   application     
        );     
  Header   File   :   winbase.h     
  詳細說明請參考   Win32   SDK   Reference   
      
    
    
  第一個參數是程式名及參數;第二個參數用來指定目的程式被執行起來後如何顯示。     
    
  EX1:   WinExec("Notepad.exe   c:\\autoexec.bat",SW_SHOW); 執行   notepad.exe   並正常顯示其程式視窗     
    
    
  EX2:   WinExec("Notepad.exe",SW_SHOWMINIMIZED); 執行   notepad.exe   但最小化其程式視窗     
    
    
  EX3:   WinExec("Command.com   /c   dir   c:\\",SW_SHOW); 執行   dir   C:\   ,完成後關閉   MSDOS   視窗     
    
    
  EX4:   WinExec("Command.com   /k   dir   c:\\",SW_SHOW); 執行   dir   C:\   ,完成後不關閉   MSDOS   視窗     
    
    
   注意:第一個參數雖然可用長檔名,但長檔名中如果有空格的話,有時候目的程式執行起來會發生錯誤,發生錯誤的原因是目的程式本身判斷輸入參數時疏忽了長檔名的關係,如果遇到這個情況時,要記得將程式名改為短檔名喔。     
    
        
    
  2:   ShellExecute(HWND   hwnd, //   handle   to   parent   window     
       LPCTSTR   lpOperation, //   pointer   to   string   that   specifies   operation   to   perform     
       LPCTSTR   lpFile, //   pointer   to   filename   or   folder   name   string     
       LPCTSTR   lpParameters, //   pointer   to   string   that   specifies   executable-file   parameters       
       LPCTSTR   lpDirectory, //   pointer   to   string   that   specifies   default   directory     
       INT   nShowCmd //   whether   file   is   shown   when   opened     
       );     
  Head   File   :   ShellApi.h     
  詳細說明請參考   Win32   SDK   Reference   

二. 结束另一个进程
HWND hWnd = ::FindWindow( NULL, "pp" );
CWnd* pWnd = FromHandle( hWnd );
DWORD nProcessID = 0;
::GetWindowThreadProcessId( hWnd, &nProcessID );
HANDLE hProcessHandle = ::OpenProcess( PROCESS_TERMINATE, FALSE, nProcessID );
BOOL bTerminate = ::TerminateProcess( hProcessHandle, 4 );

三.结束本身
exit( 0 );

阅读(2626) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~