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?
lulliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.