Solved

Keyhooks in DOS-Prompt??

Posted on 1997-12-14
13
183 Views
Last Modified: 2013-12-03
How can I receive keystrokes from a DOS-Prompt or a DOS-program with my Visual Basic 5.0 program.
Is there an API-Call or something like this?? I used the API Message Functions to recieve the keystrokes, but it doesen't work. How can I find out, witch key was pressed in the DOS prompt or the DOS program??? Is it possible??
0
Comment
Question by:BenLee
  • 8
  • 4
13 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 400 total points
Comment Utility
Answer comming.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
You should be able to set a hook on the DOS proccess using SetWidowsHookEx().  You would use

SetWindowsHookEx(WH_KEYBOARD,HookProc,hInstance,ThreadID);

Thread ID should be the thread for the DOS process you are hooking.  I'm not sure how you get that thread ID, because I'm not sure how you know the DOS process you are interested.  Hopefully you already have an answer to this, if not, explain how you know which DOS prompt you want to hook.

the HookProc should be declared as

LRESULT CALLBACK HookProc(int code,WPARAM wParam,LPARAM lParam)
{
   if (code < 0)
      return CallNextHookEx(code,wParam,lParam);
   if  (code = HC_ACTION)
      // look at virtual code in wParam and repeat count in lParam.
   return 1;
}
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Woops! You asked for visual basic and I gave you C.  Well, the idea should be the same, you still need to use the SetWindowsHookEx() procedure.  You just have to use it in basic Visual basic syntax.
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
nietod, have you actually tested your proposed solution?

There was a similar question by somebody not long ago (sorry, forgot the reference) and the combined wizdom of the experts reached the conclusion that WH_KEYBOARD hooks do not work for command-line windows under NT.

I'd be happy to be proven wrong.

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
No.  I guess I didn't make it clear enough with the word "should", but I've never tried this.  From what I've read about in Microsoft's documentation about input is handled, it should work.  But their documentation is often simplified or just plain wrong.


0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
Found the reference (qid=8630028164).

tcola asked:

--- begin quote ---

Right now I have a program in Visual C++ that will execute certain commands after X minutes a system idle. The system is considered "idle" by my program when no keyboard or mouse activity is occuring.

The program works fine in pure windows, but when users sit in an MS-DOS Box for these X amount of minutes my windows program will not detect any mouse/keyboard activity (even if someone is pounding the keys).

Right now, I set a hook to the keyboard and mouse through a dll.
                   
I want my program to detect any mouse/keyboard activity regardless of if the user is typing into the MS-DOS Box or not.

--- end quote ---

I suggested to use a VxD for Win95.

Frankly, I'm curious myself.

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
The hook in that case is a system wide hook (not a hook for a specific process).  Perhaps a hook for a specific DOS process will work.  Yeah, its a long shot, but its easier to test than to write a VxD.  
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
Why should be there be a difference?  The underlying mechanism for system-wide hooks and per-thread hooks seems to be the same.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Why?  Mostly, wishful thinking.  It is possible that for a system-wide hook, the operating system checks the process to see if it is a DOS process and, if so, does not pass it to the hook procedure assuming that windows applications aren't interested in  DOS applications.  However, given a hook for a specific process, it might guess that the hooking program obviously wants to hook the specified program regardless of whethor or not it is a DOS program so it might call the hook procedure.

Sounds logical?  Yeah, I know.  If I could reject my own answer, I would have already.
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
But then we would lose the forum for exchanging ideas :-)

BTW, strange that we haven't heard from BenLee.

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I noticed that too.  For 400 points you would think he is desperate for an answer.  For a 50 question I check for answers every hour, but I'm cheap.
0
 

Author Comment

by:BenLee
Comment Utility
I think it should work!! Thanks nietod!!
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
You should try it BEFORE accepting my answer.  It might not work and you are out 400 points!  If it doesn't work I would be happy to try to help you, but you would do better with help from more experts.  

If it doesn't work, and if the experts exchange customer service people can arrange it, I would be willing to give the points back to have the question reopened.  (Not that I'm eager to loose the points, but I'm sure you want a working solution.)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now