//1)首先,建立一个虚拟的Desktop
const DesktopName = 'MYDESK’;
FDesktop:=CreateDesktop(DesktopName,nil,nil,0,GENERIC_ALL,nil); //2)在CreateProcess的时候,指定程序在我新生成的Desktop上运行
var StartInfo:TStartupInfo; FillChar(StartInfo, sizeof(StartInfo), 0);
StartInfo.cb:=sizeof(StartInfo);
StartInfo.lpDesktop:=PChar(DesktopName); //指定Desktop的名称即可
StartInfo.wShowWindow:=SW_HIDE;
StartInfo.dwFlags:=STARTF_USESHOWWINDOW;
StartInfo.hStdError:=0;
StartInfo.hStdInput:=0;
StartInfo.hStdOutput:=0;
if not CreateProcess(PChar(FileName),nil,nil,nil,true,CREATE_NEW_CONSOLE+HIGH_PRIORITY_CLASS,nil,PChar(ExtractFilePath(FilePath)),StartInfo,FProceInfo) then begin
MessageBox(Application.Handle,’Error when init voice (5).’,PChar(Application.Title),MB_ICONWARNING);
exit;
end; //3)用FindWindow去找程序的主窗口
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;
{ TFindWindowThread } procedure TFindWindowThread.Execute();
var
I:Integer;
begin
//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;
//而主程序中的代码变成这样
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;
//4)最后,再用这个主窗口Handle,找出里面的EditBox的Handle,如这样
FEditWindow:=FindWindowEx(FMainWindowHandle,0,PChar(’Edit’),nil); if (FMainWindowHandle=0) or (FEditWindow=0) then begin
exit;
end;
SendMessage(FEditWindow,WM_SETTEXT,0,LongInt(@AText[1]));
SendMessage(FMainWindowHandle,WM_COMMAND,$8012,$0); if FProceInfo.hProcess<>0 then begin
TerminateProcess(FProceInfo.hProcess,0);
end;
if FDesktop<>0 then begin
CloseDesktop(FDesktop);
end;
const DesktopName = 'MYDESK’;
FDesktop:=CreateDesktop(DesktopName,nil,nil,0,GENERIC_ALL,nil); //2)在CreateProcess的时候,指定程序在我新生成的Desktop上运行
var StartInfo:TStartupInfo; FillChar(StartInfo, sizeof(StartInfo), 0);
StartInfo.cb:=sizeof(StartInfo);
StartInfo.lpDesktop:=PChar(DesktopName); //指定Desktop的名称即可
StartInfo.wShowWindow:=SW_HIDE;
StartInfo.dwFlags:=STARTF_USESHOWWINDOW;
StartInfo.hStdError:=0;
StartInfo.hStdInput:=0;
StartInfo.hStdOutput:=0;
if not CreateProcess(PChar(FileName),nil,nil,nil,true,CREATE_NEW_CONSOLE+HIGH_PRIORITY_CLASS,nil,PChar(ExtractFilePath(FilePath)),StartInfo,FProceInfo) then begin
MessageBox(Application.Handle,’Error when init voice (5).’,PChar(Application.Title),MB_ICONWARNING);
exit;
end; //3)用FindWindow去找程序的主窗口
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;
{ TFindWindowThread } procedure TFindWindowThread.Execute();
var
I:Integer;
begin
//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;
//而主程序中的代码变成这样
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;
//4)最后,再用这个主窗口Handle,找出里面的EditBox的Handle,如这样
FEditWindow:=FindWindowEx(FMainWindowHandle,0,PChar(’Edit’),nil); if (FMainWindowHandle=0) or (FEditWindow=0) then begin
exit;
end;
SendMessage(FEditWindow,WM_SETTEXT,0,LongInt(@AText[1]));
SendMessage(FMainWindowHandle,WM_COMMAND,$8012,$0); if FProceInfo.hProcess<>0 then begin
TerminateProcess(FProceInfo.hProcess,0);
end;
if FDesktop<>0 then begin
CloseDesktop(FDesktop);
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货