Windows C++ 调用带参数exe

1.使用system函数

system("xxx.exe参数1参数2 参数3...)

chars[300];

char*filename = "*****";

sprintf_s(s,"%s %s %s %s","....**.exe", "aa", "bb",filename);

system(s);

system 的一般格式为:system("teqc.exe meta");

缺陷:会出现命令行窗口。

 

2.使用execl或execv函数

 

3.使用WinExec函数

winexec('C:\WINNT\system32\notepad.exe',0)参数1:.exe文件全路径名参数2:控制参数

 

4.使用CreateProcess函数

BOOLCreateProcess(

LPCTSTRlpApplicationName,

LPTSTRlpCommandLine,

LPSECURITY_ATTRIBUTESlpProcessAttributes,

LPSECURITY_ATTRIBUTESlpThreadAttributes,

BOOLbInheritHandles,

DWORDdwCreationFlags,

LPVOIDlpEnvironment,

LPCTSTRlpCurrentDirectory,

LPSTARTUPINFOlpStartupInfo,(welcomebbs)

LPPROCESS_INFORMATIONlpProcessInformation

);

 

5.使用ShellExecute\ShellExecuteEx函数

HINSTANCEShellExecute(HWND hwnd,

LPCTSTRlpOperation,

LPCTSTRlpFile,

LPCTSTRlpParameters,

LPCTSTRlpDirectory,

INTnShowCmd

);

::ShellExecute(NULL,"open",pszPName, "参数1参数2 参数3", NULL, SW_SHOW);

SHELLEXECUTEINFOShExecInfo= {0};

ShExecInfo.cbSize=sizeof(SHELLEXECUTEINFO);

ShExecInfo.fMask=SEE_MASK_NOCLOSEPROCESS;

ShExecInfo.hwnd=NULL;

ShExecInfo.lpVerb=_T("open");

ShExecInfo.lpFile =_T("c4.5.exe");

ShExecInfo.lpParameters= _T("-ftrain");

ShExecInfo.lpDirectory= NULL;ShExecInfo.nShow = SW_HIDE;

ShExecInfo.hInstApp=NULL;

ShellExecuteEx(&ShExecInfo);

WaitCursorBegin();

WaitForSingleObject(ShExecInfo.hProcess,INFINITE);

WaitCursorEnd();

 

6.System.Diagnostics.Processproc

System.Diagnostics.Processproc = newSystem.Diagnostics.Process(); //程序名称

proc.StartInfo.FileName="mt3000.exe"; //参数

proc.StartInfo.Arguments= "/p1 /b38400/fstock /mr"; //隐藏

proc.StartInfo.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden; //执行

proc.Start();

 

system函数和execl、execv函数不能控制程序窗口是否显示,我要的效果是不显示该exe程序的窗口,所以这两种方法不予考虑。WinExec函数不好控制主程序来等该exe程序的结束,所以放弃。CreateProcess函数是新建一个进程,所以,你必须人为地控制该进程的生死,不如ShellExecuteEx方便。ShellExecute可以指定工作目录,并且还可以寻找文件的关联直接打开不用加载与文件关联的应用程序,ShellExecute还可以打开网页,启动相应的邮件关联发送邮件等等。

WinExec,ShellExecute,CreateProcess(welcomebbs)的注意

1、定义头文件在头文件stdafx.h中必须定义以下两个头文件:

#include<shlobj.h> // 可替换为 windows.h

#include<shellapi.h> 如果定义了头文件 #include<windows.h>的话就不必定义#include<shlobj.h>了,"windows.h"不光是包含了"shellapi.h",它还定义了许多数据类型,如果没有这些数据类型,shellapi.h本身会出错。

2、定义路径C++中所表示的路径要用" \\ "而不是平常所用的"\ ",所以以上三个函数表示路径都为:Disk:\\Directory\\...\\FilenameWinExec("D:\\ProgramFiles\\Test\\Test.exe",SW_SHOWMAXIMIZED);ShellExecute(NULL,"open","C:\\Test.txt",NULL,NULL,SW_SHOWNORMAL);

boolfRet=CreateProcess("D:\\putty.exe",NULL,NULL,FALSE,NULL,NULL,NULL,NULL,&si,π);

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值