[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Simulate Key Strokes to a Dos program

Posted on 1997-07-04
7
Medium Priority
?
872 Views
Last Modified: 2006-11-17
How can a simulate key strokes to a Dos Program running under Windows95?

My app need to lunch an app and time a five minutes delay before it ask it to quit, usualy by sending key strokes in series like "alt-x, n" or "F10, Y". My user will tell the corect sequence to send. But I must know how to send them!

ps: I know it can be done, the ScriptTools and ScriptRunner of PcTools 2.0 for Windows 3.1 does it wonderfully under both 3.1 and 95.
0
Comment
Question by:lmarceau
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:sperling
ID: 1338244
Hummm... 16-bits I'm not sure of, 32-bits I might have a solution for. One question, though, do you need to be able to send just *some* keystrokes? I've already written a 32-bit class allowing you to redirect *all* input/output to/from a console/dos app, if you need to control only parts of the input it won't do.

Regards,

Erik.
0
 
LVL 1

Author Comment

by:lmarceau
ID: 1338245
Edited text of question
0
 
LVL 3

Accepted Solution

by:
sperling earned 300 total points
ID: 1338246
This code should get you started.

Just create a form with a single button and use this code for the OnClick handler. Remember to replace the hardcoded path in CreateProcess with e.g. path to command.com.

This code will simply send an ENTER every 2 seconds until the dos app quits.

--
procedure TForm1.Button1Click(Sender: TObject);
var
  si      : TStartupInfo;
  pi      : TProcessInformation;
  ir      : ARRAY [0..1] of TInputRecord;
  n       : INTEGER;
  sa      : TSecurityAttributes;
begin
  FillChar(si, SizeOf(si), 0);
  si.cb := SizeOf(si);
  AllocConsole;
  CreateProcess('C:\WINNT\SYSTEM32\CMD.EXE', nil, nil, nil, TRUE, 0, nil, nil, si, pi);
  FillChar(ir, SizeOf(ir), 0);
  ir[0].EventType := KEY_EVENT;
  with ir[0].KeyEvent do begin
    bKeyDown := TRUE;
    wRepeatCount := 1;
    wVirtualKeyCode := VK_RETURN;
    AsciiChar := #13;
  end;
  ir[1] := ir[0];
  ir[1].KeyEvent.bKeyDown := FALSE;
  Sleep(1000);
  repeat
    WriteConsoleInput(GetStdHandle(STD_INPUT_HANDLE), ir[0], 2, n);
    Sleep(1000);
  until WaitForSingleObject(pi.hProcess, 0) = WAIT_OBJECT_0;
  FreeConsole;
end;
--

The important parts here are:

AllocConsole - Creates a console owned by your app.

TRUE for bInheritHandles in CreateProcess - The dos app will use the already existing console, still owned by your app.

FreeConsole - Removes the console owned by your app.

The TInputRecord array is necessarry because you normally needs to send both key-down and key-up to the app. Look up WriteConsoleInput in API help, look at the INPUT_RECORD and then KEY_EVENT_RECORD topic. You'll probably need to use the dwControlKeyState flag to simulate ALT and so on. Some error-checking might be an idea too.


Regards,

Erik.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:lmarceau
ID: 1338247
I've copy/pasted your code in Delphi developper 2.0.
The console boot ok. Prety cool right.
But the key aren't sent. Can you find out why?
0
 
LVL 3

Expert Comment

by:sperling
ID: 1338248
Win 95 or NT ?? Version?

Are the path and filename in CreateProcess correct?

What return values do you get from CreateProcess and WriteConsoleInput? What are the values returned if calling  GetLastError after these calls?

Regards,

Erik.
0
 
LVL 1

Author Comment

by:lmarceau
ID: 1338249
Escuse me for the delay. I'm back now.

The answers to your questions are:
I'm under Win95, Delphi 2.0

While running your code:
Console created ok
Process created ok in the console ('c:\command.com') CreateProcess returns 1, GetLastError (just after the call to createprocess) returns 6  (what does that means? where can I get a list of error code?)

Key not sent (I've added a feed back on the form, 'Sent' is beaing textout on the form every two second but the keys aren't sent)
WriteConsoleInput returns 1, GetLastError (just after the call to WriteConsoleInput) returns 6 too.

Hope this help, my shipping deadline is now aproaching!
0
 
LVL 1

Author Comment

by:lmarceau
ID: 1338250
Well, my deadline had gone though and I had to ship without the feature/ But a still need a solution.
Please help me. You seem to have a more powerfull solution than anyone else.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question