Sending F1 to DOS Program from Windows Program

My windows application starts an old DOS application (the kind that you can tab around in and use various keys - psuedo windows looking).  Anyway, I need to tab to each field, enter values, and exit the program.  I can do all this while the DOS Command Window is minimized using:

::PostMessage(hDOS, WM_CHAR, Ch, NULL);
where hDOS is the handle to the DOS Command Window, and Ch is a key (e.g. VK_TAB).

I can not get the VK_F1 key to work though (the program needs F1 to exit the app).  Any ideas how I can do this?  Is it being eaten and not sent to the DOS program?

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.

If the DOS application has the focus, then you can use keybd_event() to send the keys to the app.  This is much more reliable than using PostMessage(WM_CHAR)

Try this an let me know how it works.
You could do what nietod is suggesting, or you could try to use CreateProcess() to start your old app and TerminateProcess() to stop it...

But you should avoid using TerminateProcess() to terminate a process.  It shoudl be considered only a last resort.  The program will not be giving an opportunity to clean-up.  This is a very serious problem, because windows (especially non-NT) is not very good at cleanining up a 16-bit program.  (Not to mention that the clean-up it may do, will simply be clean-up that relates to windows itself, it won-t do any program-specific clean-up.)
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

DOS apps have some special requirements for 'special' keys (called extended).

You start it by sending a 0, then the exact key you want. for example the left-arrow would be 0, 75, while 75 would be a  alphanumeric character

I forgot the F1 key though :)

Also, I dont even know whether or not you have to do this manually or that windows handles this automatially.. but maybe you can do something with this info.

joeslowAuthor Commented:

The keybd_event seems to work pretty well.  I'm not quite sure what the Scan Code parameter does but the program has to work in other countries.  Does this look like the right thing to do?:

UINT SC = MapVirtualKey(VK_F1, 0);
::keybd_event(VK_F1, SC, 0, 0);

Also, I am using
to insure that the window has keyboard focus.  It seems to work even if the window is minimized.  Does this seem like a reliable way to do that?

Thank you,
>> I'm not quite sure what the Scan Code parameter
Usually nothing.  It indicates what key on the keyboard produced the keystroke--or at least what key you are "pretending" produced the keystroke.  This scan code changes for different types of keyboaard, (which means it often changes from language-to-language).  So the scan code is meaningless without a knowledge oft he keyboard layout, which in tern means it is pretty-much meaningless.  Virtually no software will ever use it.  (In a real keyboard event it is important to the keyboard driver, because it will convert the scan code into the keystroke it represents.  After that it is not important.)

Anyways the way you are obtaining the scan code is correct.  

make sure that every kebd_event() occurs in pairs.  That is, when you post a key down, you must later post a key up.  (You may have interveining key downs and key ups, though.)

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
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

From novice to tech pro — start learning today.