现在的位置: 首页 > 综合 > 正文

CreateProcess的用法

2018年01月16日 ⁄ 综合 ⁄ 共 2721字 ⁄ 字号 评论关闭

第一、第二个参数的用法:

 

例子:

使用ie打开指定的网页。

注意第二个参数是 可执行文件+命令行参数

 

  1. #include "stdafx.h"
      
  2. #include <windows.h> 
      
  3. #include <stdio.h> 
      
  4.   
  5. int main(int argc, char* argv[])   
  6. {   
  7.     STARTUPINFO si = { sizeof(si) };   
  8.     PROCESS_INFORMATION pi;   
  9.   
  10.     si.dwFlags = STARTF_USESHOWWINDOW;   
  11.     si.wShowWindow = TRUE; //TRUE表示显示创建的进程的窗口
      
  12.     TCHAR cmdline[] =TEXT("c://program files//internet explorer//iexplore.exe http://community.csdn.net/");  
  13.     BOOL bRet = ::CreateProcess (   
  14.         NULL,  
  15.         cmdline, //在Unicode版本中此参数不能为常量字符串,因为此参数会被修改  
      
  16.         NULL,   
  17.         NULL,   
  18.         FALSE,   
  19.         CREATE_NEW_CONSOLE,   
  20.         NULL,   
  21.         NULL,   
  22.         &si,   
  23.         &pi);   
  24.   
  25.     int error = GetLastError();  
  26.     if(bRet)   
  27.     {   
  28.         ::CloseHandle (pi.hThread);   
  29.         ::CloseHandle (pi.hProcess);   
  30.   
  31.         printf(" 新进程的进程ID号:%d /n", pi.dwProcessId);   
  32.         printf(" 新进程的主线程ID号:%d /n", pi.dwThreadId);   
  33.     }   
  34.     else  
  35.     {  
  36.         printf("error code:%d/n",error );  
  37.     }  
  38.     return 0;   
  39. }   

 

也可如此:

第一个参数是 可执行文件;第二个参数是 命令行参数

 

 

  1. #include "stdafx.h"
      
  2. #include <windows.h> 
      
  3. #include <stdio.h> 
      
  4.   
  5. int main(int argc, char* argv[])   
  6. {   
  7.     STARTUPINFO si = { sizeof(si) };   
  8.     PROCESS_INFORMATION pi;   
  9.   
  10.     si.dwFlags = STARTF_USESHOWWINDOW;   
  11.     si.wShowWindow = TRUE; //TRUE表示显示创建的进程的窗口
      
  12.     TCHAR cmdline[] =TEXT(" http://community.csdn.net/"); //注意前面有空格,否则打开的是主页。  
  13.     BOOL bRet = ::CreateProcess (   
  14.         TEXT("c://program files//internet explorer//iexplore.exe"),  
  15.         cmdline, //在Unicode版本中此参数不能为常量字符串,因为此参数会被修改  
      
  16.         NULL,   
  17.         NULL,   
  18.         FALSE,   
  19.         CREATE_NEW_CONSOLE,   
  20.         NULL,   
  21.         NULL,   
  22.         &si,   
  23.         &pi);   
  24.   
  25.     int error = GetLastError();  
  26.     if(bRet)   
  27.     {   
  28.         ::CloseHandle (pi.hThread);   
  29.         ::CloseHandle (pi.hProcess);   
  30.   
  31.         printf(" 新进程的进程ID号:%d /n", pi.dwProcessId);   
  32.         printf(" 新进程的主线程ID号:%d /n", pi.dwThreadId);   
  33.     }   
  34.     else  
  35.     {  
  36.         printf("error code:%d/n",error );  
  37.     }  
  38.   
  39.   
  40.     return 0;   
  41. }   

 

 

 

抱歉!评论已关闭.