如何在VC++种执行dos命令如copy(),打开IE、WORD等!谢谢用SYSTEM()为什么编译没有错,可是执行
system("iexplore http://www.sohu.com.cn");
打开IE后一闪而过,ie就好象close,不停留?
system("iexplore http://www.sohu.com.cn");
打开IE后一闪而过,ie就好象close,不停留?
解决方案 »
- MFC绘图与图例
- CreateRoundRectRgn创建圆角矩形
- 为什么从文件读取出来的字符 画出来是乱码,请真心的你耐心分析下
- 保存到sheet1,什么打开时显示的是sheet2?
- 通过http代理服务器访问外有哪些方式呢?
- 今天发工资,又发现CSIDL真是好东西,不敢独享,散分:)
- 如何将xml文件转换成access 数据库中的一个表(wplpwj)
- 在没有CWinApp的支持下我如何设置打印
- ADO有很多的函数是MSDN上没有提及过的,那位有他们的相关开发资料?
- 急!请问如果在DES算法的加密代码中存在"\0",那么该如何处理才能取到一行正常加密代码进行解码?
- 小波变换与数字水印
- 如何将已经做好的.dll 添加成控件使用(在线等)
/////////////////Creamdog/////////////////////
要是想打开WIN32程序,那么用WinExec好了,相当好用
WinExec只能执行win16的程序,不能执行win32的程序。
#include <process.h>
#include <windows.h>
#include <shellapi.h>
int main(int argc, char* argv[])
{ system("copy f:\\1.ppt f:\\2.ppt");
system("start \"E:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\" http://www.google.com" );
WinExec( "\"E:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\" http://www.google.com", SW_SHOW );
ShellExecute(NULL, "open", "http://www.google.com", NULL, NULL, SW_SHOWNORMAL); return 0;
}
system("command /C copy c:\1.txt c:\2.txt");
不过它的参数比较复杂.我用一个函数把它封装一下.
说明:
程序执行的结果(stdout)和错误(stderr),原来在屏幕上,
会保存到相应的文件中(文件名由第二个,第三个参数给定)
如果不必保存,给NULL即可。
/* ******************************************************************************
** [NAME]: ExecCmd .
** [FUNCTION]: To execute a command .
** [PARAMETER]:
** Input: lpCmdLine: a string contain the command.
** Output: lpStdOutFile: Filename of a file created automatically, to Record the StdOut (Standard Out) info.
** Output: lpStdErrFile: Filename of a file created automatically, to Record the StdErr (Standard Error) info.
** [RETURN]: If execute successfully ,return 0.(NOTE: 只要运行即认为成功,程序本身运行中的错误在StdErr文件中记录,如果程序有该功能的话。)
** else, the last error. (???)
**
** [USAGE EXAMPLE]:
sprintf(lpCmdLine, "SomeCommand %d %s other_solid_parameter ", para1 ,para2); //compose lpCommand
int r=ExecCmd(lpCmdLine ,"stdout.log","stderr.log"); //call the func. return r for farther anylyse.
** [NOTE]
Both or one of the 2 parameters (lpStdOutFile,lpStdErrFile) could set to NULL,
so the associated log file(s) will not be created. for example:
ExecCmd(lpCmdLine ,NULL,NULL);
** [AUTHOR] OlOl in CSDN. other ID in other BBS :)
** ****************************************************************************
*/
int ExecCmd(char * lpCmdLine , char * lpStdOutFile ,char *lpStdErrFile)
{ BOOL ret;
HANDLE OutHandle=NULL;
HANDLE ErrHandle=NULL; __try
{
if(lpStdOutFile!=NULL) // create stdout file and handle
{
SECURITY_ATTRIBUTES Sa;
Sa.nLength = sizeof(SECURITY_ATTRIBUTES);
Sa.lpSecurityDescriptor = NULL;
Sa.bInheritHandle = TRUE; //!****
OutHandle = CreateFile( lpStdOutFile,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,
&Sa,
CREATE_ALWAYS ,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH,
NULL ); if( OutHandle==INVALID_HANDLE_VALUE )
{ throw "Can't Open File - stdout file!";
}
} if(lpStdErrFile!=NULL) // create stdout file and handle
{
SECURITY_ATTRIBUTES Sa;
Sa.nLength = sizeof(SECURITY_ATTRIBUTES);
Sa.lpSecurityDescriptor = NULL;
Sa.bInheritHandle = TRUE; //!****
ErrHandle = CreateFile( lpStdErrFile,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,
&Sa,
CREATE_ALWAYS ,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH,
NULL ); if( ErrHandle==INVALID_HANDLE_VALUE )
{ throw "Can't Open File - stderr file!";
}
} STARTUPINFO infoStart;
memset(&infoStart,0,sizeof(infoStart));
infoStart.cb=sizeof(STARTUPINFO);
//infoStart.dwFlags= STARTF_USESTDHANDLES;
if((lpStdOutFile!=NULL)&&(lpStdErrFile!=NULL))
{
infoStart.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
}
else
{
infoStart.dwFlags=STARTF_USESHOWWINDOW;
}
infoStart.wShowWindow=SW_HIDE ; //SW_SHOWMAXIMIZED; infoStart.hStdOutput = OutHandle;
infoStart.hStdError = ErrHandle; PROCESS_INFORMATION infoProcess;
memset(&infoProcess,0,sizeof(infoProcess));
ret=CreateProcess(NULL,
lpCmdLine,
NULL,
NULL,
TRUE, //!****
NORMAL_PRIORITY_CLASS,
NULL,
NULL,
&infoStart,
&infoProcess
); if (ret==FALSE)
{ throw "Can't Run the Command!";
}
WaitForSingleObject(infoProcess.hProcess,INFINITE);
}
__finally
{
CloseHandle(OutHandle);
CloseHandle(ErrHandle);
}
return 0;
}