我使用winexec(pchar(...),sw_hide)执行一个DOS命令,如何知道这个命令已经执行完成了?

解决方案 »

  1.   

    用这个函数好象有些难办,你试试用别的函数调用执行其它程序如:CreateProcess
      

  2.   

    WinExec是不能的,用下面的Code了function TExecForm.Exec(FileName: string; Visibility: integer): integer;
    var
      zappname:array [0..512] of char;
      zcurdir:array [0..255] of char;
      workdir:string;
      startupinfo:tstartupinfo;
      processinfo:tprocessinformation;
      exitcode:cardinal;
    begin
      strpcopy(zappname,filename);
      getdir(0,workdir);
      strpcopy(zcurdir,workdir);
      fillchar(startupinfo,sizeof(startupinfo),#0);
      startupinfo.cb:=sizeof(startupinfo);
      startupinfo.wShowWindow:=visibility;
      if not createprocess(nil,zappname,nil,nil,false,create_new_console or Normal_priority_class,
                           nil,nil,startupinfo,processinfo) then result:=-1
         else begin
                waitforsingleobject(processinfo.hProcess,infinite);
                getexitcodeprocess(processinfo.hProcess,exitcode);
                result:=exitcode;
              end;
    end;procedure TExecForm.ExecuteClick(Sender: TObject);
    var h:thandle;
    begin
      exec(exename.Text,0);
      showmessage('Ruturn');
    end;
      

  3.   

    char *buff = new char[256] ;
        StrCopy ( buff, "Project1.exe" ) ;
        SHELLEXECUTEINFO ExeInfo;
        // Animate1->Active=true;
        ZeroMemory(&ExeInfo,sizeof( SHELLEXECUTEINFO));
        ExeInfo.cbSize = sizeof( SHELLEXECUTEINFO);
        ExeInfo.lpFile = buff;
        ExeInfo.fMask = SEE_MASK_NOCLOSEPROCESS ;
        ExeInfo.nShow = SW_MAXIMIZE;//SW_HIDE;
        ShellExecuteEx(&ExeInfo);
        delete []buff ;
        this->Hide();
        WaitForSingleObject(ExeInfo.hProcess,INFINITE);
        this->Show();
      

  4.   

    执行完winexec后判断最上面的窗体的handle的名称是不是你想要得应用程序的handle的名称
      

  5.   

    function WinExecAndWait(strFileName: string; uCmdShow: UINT): DWORD;
    var
      cAppName: array [0..512] of char;
      cCurDir: array [0..255] of char;
      strWorkDir: string;
      StartupInfo: TStartupInfo;
      ProcessInfo: TProcessInformation;
    begin
      StrPCopy(cAppName, strFileName);
      GetDir(0, strWorkDir);
      StrPCopy(cCurDir, strWorkDir);
      FillChar(StartupInfo, Sizeof(StartupInfo), #0);
      StartupInfo.cb := SizeOf(StartupInfo);
      StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
      StartupInfo.wShowWindow := uCmdShow;
      if not CreateProcess(nil, cAppName, nil, nil, true, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,
        nil, nil, StartupInfo, ProcessInfo) then
        Result := INFINITE
      else
      begin
        WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
        GetExitCodeProcess(ProcessInfo.hProcess, Result);
        CloseHandle(ProcessInfo.hProcess);
        CloseHandle(ProcessInfo.hThread);
      end;
    end;
      

  6.   

    我试了ly_liuyang函数,不能满足需要,主要是我执行的是NETSH命令,无法如何该函数运行时都没有办法隐藏cmd对话框。
    我想直接判断netsh.exe是否在进程中是否可以,如何判断,请教。
    如果进程中存在,如何等待直到进程执行完,再执行下一条命令?
      

  7.   

    我倒是觉得楼主的问题可以解决:
    //对于 WinExec
    If the function succeeds, the return value is greater than 31.
    If the function fails, the return value is one of the following error values: Value Meaning
    0 The system is out of memory or resources.
    ERROR_BAD_FORMAT The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).
    ERROR_FILE_NOT_FOUND The specified file was not found.procedure TForm1.Button1Click(Sender: TObject);
    var ReTurn:Integer;
    begin
     ReTurn:=winexec(pchar('command.com /C '+'net view '+' >'+'c:\xinxi.txt'),sw_hide);
     if ReTurn>31 then
       begin
        if not fileexists('c:\xinxi.txt') then
          sleep(100)
        else
          memo1.Lines.LoadFromFile('c:\xinxi.txt');
       end;
     end;//既然你都能把结果给得到了,还你能判断妈 /?
      

  8.   

    xiangwangz兄的方法不可行,因为RETURN返回的值在程序开始时,而我是要知道程序什么时候结束,当然不行。