原文地址http://blog.csdn.net/tonylk/archive/2005/04/21/357701.aspx
后台完美调用外部程序 
-----------------------------------------------------
主代码unit Unit2;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm2 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
   FDesktop:HDESK; { Private declarations }
  public
    { Public declarations }
  end;var
  Form2: TForm2;
 const
  DesktopName = 'MYDESK';implementationuses Unit3;{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
var
  StartInfo:TStartupInfo;
  FProceInfo :TProcessInformation;
begin
  FillChar(StartInfo, sizeof(StartInfo), 0);
  StartInfo.cb:=sizeof(StartInfo);
  StartInfo.lpDesktop:=PChar(DesktopName); //指定Desktop的名称即可
  StartInfo.wShowWindow:=SW_SHOW;
  StartInfo.dwFlags:=STARTF_USESHOWWINDOW;
  StartInfo.hStdError:=0;
  StartInfo.hStdInput:=0;
  StartInfo.hStdOutput:=0;
  if not CreateProcess('D:\Downloads\quickpar\QuickPar\QuickPar.exe', nil,nil,nil,true,0,nil,'D:\Downloads\quickpar\QuickPar',StartInfo,FProceInfo) then begin
  MessageBox(Application.Handle,PChar(inttostr(GetLastError)),PChar(Application.Title),MB_ICONWARNING);
  exit;
  end;
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
 FDesktop:=CreateDesktop(DesktopName,nil,nil,0,GENERIC_ALL,nil);//创建新的虚拟桌面
  if FDesktop=0 then
  ShowMessage( IntToStr(GetLastError));
end;procedure TForm2.Button3Click(Sender: TObject);
begin
FindWindowThread:=TFindWindowThread.Create(false,FDesktop);
try
  FindWindowThread.WaitFor;
  FMainWindowHandle:=FindWindowThread.WindowHandle;
finally
  FindWindowThread.Free;
end;
if FMainWindowHandle=0 then begin
  MessageBox(Application.Handle,'Error when init voice (6).',PChar(Application.Title),MB_ICONWARNING);
  exit;
end;
end;
新线程代码
unit Unit3;interfaceuses
  Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF};type  TFindWindowThread = class(TThread)
  private
  FDesktop:THandle;
  FWindowHandle:THandle;
  protected
  procedure Execute; override;
  public
  constructor Create(ACreateSuspended:Boolean;const ADesktop:THandle);reintroduce;
  property WindowHandle:THandle read FWindowHandle;
end;implementationuses Unit2;
procedure TFindWindowThread.Execute;
var
  I:Integer;begin
  NameThreadForDebugging('TThread');
  //make the current thread find window on the new desktop!
  if not SetThreadDesktop(FDesktop) then begin
    exit;
  end;
  for I:=0 to 60 do begin //wait 30 seconds for open the main window
    FWindowHandle:=FindWindow(nil,PChar('WindowCaption'));
    if FWindowHandle<>0 then begin
      break;
    end;
    Sleep(500);
  end;end;
 constructor TFindWindowThread.Create(ACreateSuspended:Boolean;const ADesktop:THandle);
begin
  inherited Create(ACreateSuspended);
  FDesktop:=ADesktop;
end;
end.现在
procedure TForm2.Button3Click(Sender: TObject);
begin
FindWindowThread:=TFindWindowThread.Create(false,FDesktop);
try
  FindWindowThread.WaitFor;
  FMainWindowHandle:=FindWindowThread.WindowHandle;
finally
  FindWindowThread.Free;
end;
if FMainWindowHandle=0 then begin
  MessageBox(Application.Handle,'Error when init voice (6).',PChar(Application.Title),MB_ICONWARNING);
  exit;
end;
button 那里编译不过,谁帮我看看代码
我现在就是想在虚拟桌面控制 'D:\Downloads\quickpar\QuickPar\QuickPar.exe'
这个软件 我共享了
http://u.115.com/file/t741585673#目的 在QuickPar.exe,添加一个文件,然后按下创建。

解决方案 »

  1.   

    procedure TForm2.Button3Click(Sender: TObject);
    begin
    FindWindowThread:=TFindWindowThread.Create(false,FDesktop);
    try
      FindWindowThread.WaitFor;
      FMainWindowHandle:=FindWindowThread.WindowHandle;
    finally
      FindWindowThread.Free;
    end;
    if FMainWindowHandle=0 then begin
      MessageBox(Application.Handle,'Error when init voice (6).',PChar(Application.Title),MB_ICONWARNING);
      exit;
    end;
    就这里编译不过
      

  2.   

    这么久了,还没有弄完。你变量都没有声明不报错就怪了
    FindWindowThread,
    FMainWindowHandle
    看来你不会delphi吧
      

  3.   

    zhaodog
    我是搞硬件,这个是我第一次用delphi
      

  4.   

    var
    FindWindowThread:TThread;
    FMainWindowHandle:THandle;
    begin
     FindWindowThread:=TFindWindowThread.Create(false,FDesktop);
      try
        FindWindowThread.WaitFor;
        FMainWindowHandle:= FindWindowThread.WindowHandle;
      finally
        FindWindowThread.Free;
      end;
      if FMainWindowHandle=0 then begin
        MessageBox(Application.Handle,'Error when init voice (6).',PChar(Application.Title),MB_ICONWARNING);
        exit;
      end;错误
    Undeclared identifier: 'WindowHandle'
    这个怎么解决
      

  5.   

    你调用下 新线程的那个单元啊 unit3吧!不加入uses里面,怎么用这个类来声明 变量啊
      

  6.   

    var
    FindWindowThread:TThread;
    FMainWindowHandle:THandle;
    begin
     FindWindowThread:=TFindWindowThread.Create(false,FDesktop);
      try
      FindWindowThread.WaitFor;
      FMainWindowHandle:= FindWindowThread.Handle;//原来(FindWindowThread.WindowHandle;)  finally
      FindWindowThread.Free;
      end;
      if FMainWindowHandle=0 then begin
      MessageBox(Application.Handle,'Error when init voice (6).',PChar(Application.Title),MB_ICONWARNING);
      exit;
      end;
    这样会编译能通过。
    调试FMainWindowHandle有反馈值但是怎么确定这个是我调用的程序的窗口Handle呢
      

  7.   

    unit3 新线程
    unit Unit3;interfaceuses
      Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF};type  TFindWindowThread = class(TThread)
      private
      FDesktop:THandle;
      FWindowHandle:THandle;
      protected
      procedure Execute; override;
      public
      constructor Create(ACreateSuspended:Boolean;const ADesktop:THandle);reintroduce;
      property WindowHandle:THandle read FWindowHandle;
    end;implementationuses Unit2;
    procedure TFindWindowThread.Execute;
    var
      I:Integer;begin
      NameThreadForDebugging('TThread');
      //make the current thread find window on the new desktop!
      if not SetThreadDesktop(FDesktop) then begin
        exit;
      end;
      for I:=0 to 60 do begin //wait 30 seconds for open the main window
        FWindowHandle:=FindWindow(nil,PChar('WindowCaption'));
        if FWindowHandle<>0 then begin
          break;
        end;
        Sleep(500);
      end;end;
     constructor TFindWindowThread.Create(ACreateSuspended:Boolean;const ADesktop:THandle);
    begin
      inherited Create(ACreateSuspended);
      FDesktop:=ADesktop;
    end;
    end.
    主程序:unit Unit2;
    interface
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm2 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
      private
       FDesktop:HDESK; { Private declarations }
      public
        { Public declarations }
      end;var
      Form2: TForm2;
     const
      DesktopName = 'MYDESK';implementationuses Unit3;{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
    var
      StartInfo:TStartupInfo;
      FProceInfo :TProcessInformation;
    begin
      FillChar(StartInfo, sizeof(StartInfo), 0);
      StartInfo.cb:=sizeof(StartInfo);
      StartInfo.lpDesktop:=PChar(DesktopName); //指定Desktop的名称即可
      StartInfo.wShowWindow:=SW_SHOW;
      StartInfo.dwFlags:=STARTF_USESHOWWINDOW;
      StartInfo.hStdError:=0;
      StartInfo.hStdInput:=0;
      StartInfo.hStdOutput:=0;
      if not CreateProcess('D:\Downloads\quickpar\QuickPar\QuickPar.exe', nil,nil,nil,true,0,nil,'D:\Downloads\quickpar\QuickPar',StartInfo,FProceInfo) then begin
      MessageBox(Application.Handle,PChar(inttostr(GetLastError)),PChar(Application.Title),MB_ICONWARNING);
      exit;
      end;
    end;
    procedure TForm2.Button2Click(Sender: TObject);
    begin
     FDesktop:=CreateDesktop(DesktopName,nil,nil,0,GENERIC_ALL,nil);//创建新的虚拟桌面
      if FDesktop=0 then
      ShowMessage( IntToStr(GetLastError));
    end;procedure TForm2.Button3Click(Sender: TObject);
    var
    FindWindowThread:TThread;
    FMainWindowHandle:THandle;
    begin
    FindWindowThread:=TFindWindowThread.Create(false,FDesktop);
    try
      FindWindowThread.WaitFor;
      FMainWindowHandle:=FindWindowThread.Handle;
    finally
      FindWindowThread.Free;
    end;
    if FMainWindowHandle=0 then begin
      MessageBox(Application.Handle,'Error when init voice (6).',PChar(Application.Title),MB_ICONWARNING);
      exit;
    end;
    end;
    这样调试是过了的
    #8 zkroy36我在unit2里 uses Unit3 了的;
      

  8.   

    你原来根本就不会DELPHI,没有问题也来说事儿