您的位置:首页 > 编程语言 > C语言/C++

深入C/C++系列:后台运行dos命令

2008-03-29 13:30 260 查看
方法一:widows API之CreateProcess , 源码如下:
#include "windows.h"
void main()
{
    STARTUPINFO si;
    ZeroMemory(&si,sizeof(si));
    si.cb=sizeof(si);
    si.dwFlag=USERSHOWWINDOW;
    si.nShowWindow=SW_HIDE;
   
    PROCESS_INFORMATION pi;
    ZeroMemory(&pi,sizeof(pi));
   
    CreateProcess(NULL,"cmd /c mkdir c://temp",NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
}

方法二:widows API之WinExec,源码如下:

#include "windows.h"
void main()
{
    WinExec("cmd /c c://test.bmp",SW_HIDE);
}

方法三:widows API之ShellExecute,源码如下:

#include "windows.h"
void main()
{
    ShellExecute(NULL,NULL,"cmd","/c c://test.bmp",NULL,SW_SHOW);

}

参考:
BOOL CreateProcess(


LPCTSTR lpApplicationName,


LPTSTR lpCommandLine,


LPSECURITY_ATTRIBUTES lpProcessAttributes,


LPSECURITY_ATTRIBUTES lpThreadAttributes,


BOOL bInheritHandles,


DWORD dwCreationFlags,


LPVOID lpEnvironment,


LPCTSTR lpCurrentDirectory,


LPSTARTUPINFO lpStartupInfo,


LPPROCESS_INFORMATION lpProcessInformation


);

UINT WinExec(

 

LPCSTR lpCmdLine,
 
UINT uCmdShow

);

HINSTANCE ShellExecute(
HWND hwnd,
LPCTSTR lpOperation,
LPCTSTR lpFile,
LPCTSTR lpParameters,
LPCTSTR lpDirectory,
INT nShowCmd
);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dos attributes null cmd api c