C++使用CreateProcess执行cmd命令(实例演示)

使用CreateProcess可以执行cmd命令,也可以执行一些可执行程序。

//摘自msdn的示例代码
#include 
#include 
#include 
void _tmain( int argc, TCHAR *argv[] )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    if( argc != 2 )
    {
        printf("Usage: %s [cmdline]\n", argv[0]);
        return;
    }

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        argv[1],        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

在上面的代码基础上改造之后,执行打开windows下的记事本的命令。

//使用CreateProcess执行notepad的测试的代码
//调用示例 CString strcmd=_T("notepad");cmdProcess(strcmd);
void cmdProcess( CString strCmd)
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    //隐藏掉可能出现的cmd命令窗口
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        (LPSTR)(LPCTSTR)strCmd,        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

以下是我的个人公众号,主要作为C/C++语言技术分享使用,该公众号里干货满满,如果您有对此博文的疑问或者java方面的问题也可以添加公众号交流讨论。最后,再次希望您能添加关注,互相交流互相学习共同进步:
C++使用CreateProcess执行cmd命令(实例演示)_第1张图片

MSDN CreateProcess函数介绍:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
MSDN CreateProcess实例地址:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682512(v=vs.85).aspx

你可能感兴趣的:(c语言,C++,cmd,命令行,c++)