Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to send key strokes to a non-windowed dos box

Posted on 1997-12-13
2
Medium Priority
?
199 Views
Last Modified: 2010-05-18
I've read the MS article #Q77394 "How to send keystrokes from VB App to MS-DOS App" but this will only work if the dos box is windowed. Is there anyway to send keystrokes to a hidden dos box?
Thanks
0
Comment
Question by:Karma121397
  • 2
2 Comments
 
LVL 4

Accepted Solution

by:
rantanen earned 400 total points
ID: 1445050
I suppose you mean a window which is really hidden, not only minimized.

Then the only thing I could figure out was to make the DOS window appear in minimized state and do what is desctibed in that KB article. The only problem is, what is taking place in the DOS window happens asynchronously from VB's point of view and hiding the window again requires more thinking, if you need that feature.

Anyway, following code supplements the code from that KB article and lets you do SendKeys to an originally hidden DOS window, which then stays as minimized window.

Option Explicit
Dim progname As String
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClasszName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetClasszName Lib "user32" Alias "GetClasszNameA" (ByVal hwnd As Long, ByVal lpClasszName As String, ByVal nMaxCount As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Const SW_HIDE = 0
Const SW_NORMAL = 1
Const SW_MINIMIZE = 6

Const GW_CHILD = 5
Const GW_HWNDNEXT = 2

Private Sub btnSendKeys_Click()
    DOSWindowShow SW_MINIMIZE
    progname = "Project1 - Microsoft Visual Basic [run]"
    Clipboard.Clear
    Clipboard.SetText DosKeys.Text + Chr$(13)  ' Append a <CR>.
    AppActivate DoszClassName.Text
    SendKeys "% ep", 1
    AppActivate progname
    ' Following does or does not work dependingwhat is happening in DOS window
    DOSWindowShow SW_HIDE
End Sub


Private Sub DOSWindowShow(swMode As Integer)
    Dim szClassName As String
    Dim szName As String
    Dim hwnd As Long
    Dim lResult As Long
    Dim bSuccess As Boolean
   
    bSuccess = False
    ' Get first top-level window
    hwnd = GetWindow(GetDesktopWindow(), GW_CHILD)
    ' Iterate through remaining windows
    Do While hwnd <> 0
        szName = String$(80, 0)
        lResult = GetClasszName(hwnd, szName, 80)
        szClassName = Left$(szName, lResult)
        lResult = FindWindow(szClassName, "MS-DOS Prompt")
        If lResult <> 0 Then
            lResult = ShowWindow(hwnd, swMode)
            bSuccess = True
        End If
        hwnd = GetWindow(hwnd, GW_HWNDNEXT)
    Loop
End Sub

0
 
LVL 4

Expert Comment

by:rantanen
ID: 1445051
¤#%!,

theres also typos in this sample program. (Luckily I picked them up and not somebody else...have to be more careful with module wide replacements). The API function is not  GetClasszName but  GetClassName (GetClassNameA) and textbox used in original sample was not DoszClassName but DosTitle.

I'm sorry about this.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

782 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