• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • Last Modified:

Start App And Wait for It Is Ready For Input - SendKeys

I have build macro, and number 3 in the line i execute an application
and in line 4 i want to sendkeys to the application e.g {ALT}fo
my macro may not go immedialy after the 3 line, he have to wait for the application is ready for input

How is this possible?
0
lulli
Asked:
lulli
  • 10
  • 9
  • 4
  • +2
1 Solution
 
MadshiCommented:
Call "WaitForInputIdle". This is all I say for 9 points...   :-)
0
 
intheCommented:
get the classname of the window using sinfo or winsight then can do below:

procedure TForm1.Button1Click(Sender: TObject);
var
h,he:thandle;
begin
h := 0;
he := 0;
winexec('notepad.exe',sw_normal);
repeat
h:=FindWindow('notepad',nil)
until h <> 0;
repeat
he:=FindWindowex(h,0,'Edit',nil);
until he <> 0;
SetForegroundwindow(he);
keybd_Event(ord('H'),0,0,0);
keybd_Event(ord('H'),0,KEYEVENTF_KEYUP, 0);
keybd_Event(ord('E'),0,0,0);
keybd_Event(ord('E'),0,KEYEVENTF_KEYUP, 0);
keybd_Event(ord('L'),0,0,0);
keybd_Event(ord('L'),0,KEYEVENTF_KEYUP, 0);
keybd_Event(ord('L'),0,0,0);
keybd_Event(ord('L'),0,KEYEVENTF_KEYUP, 0);
keybd_Event(ord('O'),0,0,0);
keybd_Event(ord('O'),0,KEYEVENTF_KEYUP, 0);

end;

9 points is pretty low isnt it?
0
 
lulliAuthor Commented:
Yes, i know , but i don't have more points i have to buy more,

Madshi:

Can you give me example to use WaitForInputIdle i send a new question for you

Thanks Barry
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
MadshiCommented:
Not tested, but should work:

function StartAndWaitForInputIdle(cmdLine: string; timeoutInMs: dword) : boolean;
var si : TStartupInfo;
    pi : TProcessInformation;
begin
  if CreateProcess(nil, 'c:\windows\notepad.exe', nil, nil, false, 0, nil, nil, si, pi) then begin
    result := WaitForInputIdle(pi.hProcess, timeoutInMs) = 0;
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread );
  end else result := false;
end;

Regards, Madshi.

P.S: Lulli, for the future, you'll have no other choice than to BUY points, sorry...   :-(
0
 
lulliAuthor Commented:
Yes i know that, thanks for telling me that, but this example you posted here in does not working

Thanks
0
 
MadshiCommented:
What do you mean with it does not work? Does it not compile? Or do you get an exception? Or ...?
0
 
palli011000Commented:
no errors, it compile rights, here is the code

function StartAndWaitForInputIdle(cmdLine: string; timeoutInMs: dword) : boolean;
var si : TStartupInfo;
    pi : TProcessInformation;
begin
  if CreateProcess(nil, cmdline, nil, nil, false, 0, nil, nil, si, pi) then begin
    result := WaitForInputIdle(pi.hProcess, timeoutInMs) = 0;
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread );
  end else result := false;
end;

thanks
0
 
MadshiCommented:
Ooops, yes, I left the "c:\windows\notepad.exe" there. It should be like this:

  if CreateProcess(nil, pchar(cmdLine), nil, nil, false, 0, nil, nil, si, pi) then begin
0
 
palli011000Commented:
No it does not work yet, no answer from the function
0
 
palli011000Commented:
No it does not work yet, no answer from the function
0
 
MadshiCommented:
What means no answer? Do you get "false" as result? Or "true"? Does the program start or not?
0
 
palli011000Commented:
Sorry!!!

No it does not start and the function returns false
0
 
MadshiCommented:
Please show me what you've given in as "cmdLine". It must not be a document, it must be an executable with full path.
0
 
palli011000Commented:
Function StartAndWaitForInputIdle(CmdLine: string; TimeOutInMs: dword) : boolean;
var si : TStartupInfo;
    pi : TProcessInformation;
Begin
  If CreateProcess(nil, PChar(CmdLine), nil, nil, false, 0, nil, nil, si, pi) then begin
    result := WaitForInputIdle(pi.hProcess, TimeOutInMs) = 0;
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread );
  End Else result := false;
End;


procedure TForm1.Button1Click(Sender: TObject);
Var B : Boolean;
begin
B := StartAndWaitForInputIdle('c:\winnt\notepad.exe',1000);
If B = True Then
ShowMessage('B = True')
Else
ShowMessage('B = False');
end;
0
 
MadshiCommented:
It works perfectly on my win98 pc. Make sure notepad is really there. Make sure you have the rights to start it. And perhaps use 5000 instead of 1000.
0
 
palli011000Commented:
Okey, it work's very fine in Win9x but not NT operating system
0
 
MadshiCommented:
It works fine with *MY* NT, too. It must be either that there's no notepad in your winNT directory or that you don't have the nessecary rights.

Function StartAndWaitForInputIdle(CmdLine: string; TimeOutInMs: dword) : boolean;
var si : TStartupInfo;
    pi : TProcessInformation;
Begin
  If CreateProcess(nil, PChar(CmdLine), nil, nil, false, 0, nil, nil, si, pi) then begin
    result := WaitForInputIdle(pi.hProcess, TimeOutInMs) = 0;
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread );
  End Else begin
    result := false;
    MessageBox(0, pchar(IntToStr(GetLastError)), 'CreateProcess failed', 0);
  end;
End;

Try this code and tell me the error number.
0
 
palli011000Commented:
Create Process Failed = 0

i have notepad on my c:\winnt\notepad.exe
0
 
palli011000Commented:
It works fine if i use this

OpenDialog1.Execute;
StartAndWaitForInputIdle(OpenDialog1.Filename,5000);

What is wrong with other?
0
 
MadshiCommented:
Uuuh... I'm sorry, it was my fault. Add this before the CreateProcess call:

  zeroMemory(@si, sizeOf(TStartupInfo));
  si.cb := sizeOf(TStartupInfo);

Now it should work alright.
0
 
palli011000Commented:
Alright!!! it works , thanks very much
0
 
lulliAuthor Commented:
But how can i start the program my way,
e.g SW_HIDE,SW_NORMAL with these function?

Thank you mery much?
Lulli
0
 
MadshiCommented:
Use this:

  zeroMemory(@si, sizeOf(TStartupInfo));
  si.cb := sizeOf(TStartupInfo);
  si.dwFlags := STARTF_USESHOWWINDOW;
  si.wShowWindow := SW_HIDE;

Regards, Madshi.
0
 
karen021897Commented:
thank you very much Madshi, you are the man
0
 
karen021897Commented:
i was nearly finish ask some question about waitforinputidle, thanks and thanks to Palli for asking this

Karen
0
 
lulliAuthor Commented:
this works very fine

thanx
Lulli
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 10
  • 9
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now