我想要在D6中调用外部vfp程序,传几个参数进去,然后vfp程序处理好数据库之后,并传出一个文件的完整路径名或者空值(程序处理异常),我在D6中对该文件进行处理。我用ShellExecute调用vfp程序,参数传进去没有问题,但是不能得到返回值。而且vfp程序运行不受控制,d6的程序不管结果怎么样,继续往下走了。高手请指点一二,十分感谢!

解决方案 »

  1.   

    function MyExecute(const Command: string; bWaitExecute: Boolean;
           bShowWindow: Boolean; PI: PProcessInformation): Boolean;
        //在自己的进程中运行别的程序,调用举例:MyExecute('C:\WINNT\system32\net.exe send huo aa',true,true,nil);
    function TMyClass.MyExecute(const Command: string; bWaitExecute,
      bShowWindow: Boolean; PI: PProcessInformation): Boolean;
    var
      StartupInfo       : TStartupInfo;
      ProcessInformation: TProcessInformation;
    begin
      FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
      with StartupInfo do
      begin
        cb := SizeOf(TStartupInfo);
        dwFlags := STARTF_USESHOWWINDOW;
        if bShowWindow then
          wShowWindow := SW_NORMAL
        else
          wShowWindow := SW_HIDE;
      end;
      Result := CreateProcess(nil, PChar(Command),
        nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil,
        StartupInfo, ProcessInformation);
      if not Result then
        Exit;
      if bWaitExecute then
        WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
      if Assigned(PI) then
        Move(ProcessInformation, PI^, SizeOf(ProcessInformation));
    end;
      

  2.   

    给你一个得到 DOS输出的例子,该例子执行DOS命令后将结果输出到一个MEMO中,当然你也可以使用DOS重定向功能将输出结果保存为文件然后调用
    procedure TForm1.btnRunClick(Sender: TObject);
    var
     hReadPipe,hWritePipe:THandle;
     si:STARTUPINFO;
     lsa:SECURITY_ATTRIBUTES;
     pi:PROCESS_INFORMATION;
     mDosScreen:String;
     cchReadBuffer:DWORD;
     ph:PChar;
     fname:PChar;
     i,j:integer;
    begin
     fname:=allocmem(255);
     ph:=AllocMem(5000);
     lsa.nLength :=sizeof(SECURITY_ATTRIBUTES);
     lsa.lpSecurityDescriptor :=nil;
     lsa.bInheritHandle :=True; if CreatePipe(hReadPipe,hWritePipe,@lsa,0)=false then
     begin
       ShowMessage('Can not create pipe!');
       exit;
     end;
     fillchar(si,sizeof(STARTUPINFO),0);
     si.cb :=sizeof(STARTUPINFO);
     si.dwFlags :=(STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);
     si.wShowWindow :=SW_HIDE;
     si.hStdOutput :=hWritePipe;
     StrPCopy(fname,'CMD.EXE /c dir/w'); if CreateProcess( nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False  then
     begin
       ShowMessage('can not create process');
       FreeMem(ph);
       FreeMem(fname);
       Exit;
     end; while(true) do
     begin
       if not PeekNamedPipe(hReadPipe,ph,1,@cchReadBuffer,nil,nil) then break;
       if cchReadBuffer<>0 then
       begin
         if ReadFile(hReadPipe,ph^,4096,cchReadBuffer,nil)=false then break;
         ph[cchReadbuffer]:=chr(0);
                                   Memo1.Lines.Add(ph);
       end
       else if(WaitForSingleObject(pi.hProcess ,0)=WAIT_OBJECT_0) then break;
       Sleep(100);
     end; ph[cchReadBuffer]:=chr(0);
              Memo1.Lines.Add(ph);
     CloseHandle(hReadPipe);
     CloseHandle(pi.hThread);
     CloseHandle(pi.hProcess);
     CloseHandle(hWritePipe);
     FreeMem(ph);
     FreeMem(fname);
    end;
      

  3.   

    那返回值放在哪里?是在PI里面吗?我去看了一下 PProcessInformation 结构,如下:
    PProcessInformation = ^TProcessInformation;
      _PROCESS_INFORMATION = record
        hProcess: THandle;
        hThread: THandle;
        dwProcessId: DWORD;
        dwThreadId: DWORD;
      end;里面没有放置参数得地方呀?!