C/C++使用WinAPI CreateProcess函数调用外部程序

CreateProcess函数原型:

BOOL WINAPI CreateProcess(
  _In_opt_     LPCTSTR lpApplicationName,
  _Inout_opt_  LPTSTR lpCommandLine,
  _In_opt_     LPSECURITY_ATTRIBUTES lpProcessAttributes,
  _In_opt_     LPSECURITY_ATTRIBUTES lpThreadAttributes,
  _In_         BOOL bInheritHandles,
  _In_         DWORD dwCreationFlags,
  _In_opt_     LPVOID lpEnvironment,
  _In_opt_     LPCTSTR lpCurrentDirectory,
  _In_         LPSTARTUPINFO lpStartupInfo,
  _Out_        LPPROCESS_INFORMATION lpProcessInformation
);

查看微软官方说明:https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx

示例代码:

# include 
# include 

/*
函数原型:
BOOL WINAPI CreateProcess(
  _In_opt_     LPCTSTR lpApplicationName,
  _Inout_opt_  LPTSTR lpCommandLine,
  _In_opt_     LPSECURITY_ATTRIBUTES lpProcessAttributes,
  _In_opt_     LPSECURITY_ATTRIBUTES lpThreadAttributes,
  _In_         BOOL bInheritHandles,
  _In_         DWORD dwCreationFlags,
  _In_opt_     LPVOID lpEnvironment,
  _In_opt_     LPCTSTR lpCurrentDirectory,
  _In_         LPSTARTUPINFO lpStartupInfo,
  _Out_        LPPROCESS_INFORMATION lpProcessInformation
);
*/

int main(int argc, char * argv[])
{
    TCHAR commandLine[] = TEXT("notepad");
    STARTUPINFO si = {sizeof(si)};
    PROCESS_INFORMATION pi;
    bool bRet = CreateProcess(
        NULL,
        commandLine,
        NULL,
        NULL,
        FALSE,
        CREATE_NO_WINDOW,
        NULL,
        NULL,
        &si,
        &pi);
    int error = GetLastError();
    if (bRet)
    {
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);

        printf("进程ID:%d\n", pi.dwProcessId);
        printf("线程ID:%d\n", pi.dwThreadId);
    }
    system("pause");
    return 0;
}

运行效果:
C/C++使用WinAPI CreateProcess函数调用外部程序_第1张图片

NOTE:

TCHAR * commandLine = TEXT("notepad");//这里用了指向字符串的指针
    STARTUPINFO si = {sizeof(si)};
    PROCESS_INFORMATION pi;
    bool bRet = CreateProcess(
        NULL,
        commandLine,
        NULL,
        NULL,
        FALSE,
        CREATE_NO_WINDOW,
        NULL,
        NULL,
        &si,
        &pi);

//TCHAR commandLine[] = TEXT("notepad");
    STARTUPINFO si = {sizeof(si)};
    PROCESS_INFORMATION pi;
    bool bRet = CreateProcess(
        NULL,
        TEXT("notepad"),
        NULL,
        NULL,
        FALSE,
        CREATE_NO_WINDOW,
        NULL,
        NULL,
        &si,
        &pi);

以上两种用法都不能编译通过。这是因为在内部,CreateProcess实际上会修改我们传给他的命令行字符串,不过在这个函数返回前,会把这个字符串还原,所以这样的代码是错误的。

如果使用了lpApplicationName参数传递了可执行程序,那么lpCommandLine参数前必须有个空格;

TCHAR * appPath = TEXT("\"c:\\program files (x86)\\internet explorer\\iexplore.exe\"");
    TCHAR cmdline[] = TEXT(" http://www.baidu.com");    //http前有空格
    STARTUPINFO si = {sizeof(si)};
    PROCESS_INFORMATION pi;

如果可执行程序路径中如果有空格,须用双引号括起。如果不用双引号括起会产生调用错误,如下代码:

TCHAR cmdline[] = TEXT("c:\\program files (x86)\\internet explorer\\iexplore.exe);
//这里会打开C盘下program.exe程序,如果存在,而不是iexplore.exe
CreateProcess(      
        NULL,       
        cmdline,
        //这里会打开C盘下program.exe程序,如果存在,而不是iexplore.exe
        NULL,
        NULL,
        FALSE,
        CREATE_NEW_CONSOLE,
        NULL,
        NULL,
        &si,
        &pi);

lpCommandLine参数不能直接传递字符串。像这样调用是错误的:

CreateProcess(      
        NULL,       
        TEXT("\"c:\\program files (x86)\\internet explorer\\iexplore.exe\" http://www.baidu.com"),
        NULL,
        NULL,
        FALSE,
        CREATE_NEW_CONSOLE,
        NULL,
        NULL,
        &si,
        &pi);

你可能感兴趣的:(C/C++,For,Windows)