【Win32API】CreateProcess启动脚本

脚本

ping -w 1000 -n %1 1.0.0.1
exit

c代码

#include "stdafx.h"
#include <string>
#include <Windows.h>

static void runScript(const char * cmdline);

int _tmain(int argc, _TCHAR* argv[])
{
    runScript(" /k c:\\temp\\sleep.bat 5");
    return 0;
}

void runScript(const char * cmdline)
{
    STARTUPINFOA startInf;
    memset( &startInf, 0, sizeof startInf );
    startInf.cb = sizeof(startInf);
    // If you want to redirect result of command, set startInf.hStdOutput to a file
    // or pipe handle that you can read it, otherwise we are done!
    PROCESS_INFORMATION procInf;
    memset( &procInf, 0, sizeof procInf );

    char cmd[ MAX_PATH ];
    size_t nSize = _countof(cmd);
    getenv_s( &nSize, cmd, "COMSPEC" );
    BOOL b = CreateProcessA( cmd, (char *)cmdline, NULL, NULL, FALSE,
        NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, NULL, NULL, &startInf, &procInf );

    DWORD dwErr;
    if( b ) {
        // Wait till cmd do its job
        WaitForSingleObject( procInf.hProcess, INFINITE );
        // Check whether our command succeeded?
        GetExitCodeProcess( procInf.hProcess, &dwErr );
        // Avoid memory leak by closing process handle
        CloseHandle( procInf.hProcess );
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值