如何在VC++种执行dos命令如copy(),打开IE、WORD等!谢谢用SYSTEM()为什么编译没有错,可是执行
   system("iexplore http://www.sohu.com.cn");
  打开IE后一闪而过,ie就好象close,不停留?

解决方案 »

  1.   

    /*****************感谢关注*******************/
    /////////////////Creamdog/////////////////////
    要是想打开WIN32程序,那么用WinExec好了,相当好用
      

  2.   

    ShellExecute(NULL,"open","http://www.sohu.com","",NULL,SW_SHOW);
    WinExec只能执行win16的程序,不能执行win32的程序。
      

  3.   

    记事本是16位的吗  winexec可以用他的
      

  4.   

    #include "stdafx.h"
    #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;
    }
      

  5.   

    执行DOS命令可以这样(以copy为例):
    system("command /C copy c:\1.txt c:\2.txt");
      

  6.   

    一般的命令可用system函数,拷贝文件可用copy函数,运行win32程序可用ShellExecute 函数
      

  7.   

    微软推荐使用CreateProcess().
    不过它的参数比较复杂.我用一个函数把它封装一下.
    说明:
    程序执行的结果(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;
    }