Link to home
Start Free TrialLog in
Avatar of lulli
lulli

asked on

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?
Avatar of Madshi
Madshi

Call "WaitForInputIdle". This is all I say for 9 points...   :-)
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lulli

ASKER

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
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...   :-(
Avatar of lulli

ASKER

Yes i know that, thanks for telling me that, but this example you posted here in does not working

Thanks
What do you mean with it does not work? Does it not compile? Or do you get an exception? Or ...?
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
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
No it does not work yet, no answer from the function
No it does not work yet, no answer from the function
What means no answer? Do you get "false" as result? Or "true"? Does the program start or not?
Sorry!!!

No it does not start and the function returns false
Please show me what you've given in as "cmdLine". It must not be a document, it must be an executable with full path.
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;
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.
Okey, it work's very fine in Win9x but not NT operating system
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.
Create Process Failed = 0

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

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

What is wrong with other?
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.
Alright!!! it works , thanks very much
Avatar of lulli

ASKER

But how can i start the program my way,
e.g SW_HIDE,SW_NORMAL with these function?

Thank you mery much?
Lulli
Use this:

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

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

Karen
Avatar of lulli

ASKER

this works very fine

thanx
Lulli