Solved

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

Posted on 1997-12-13
2
183 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 100 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

708 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

15 Experts available now in Live!

Get 1:1 Help Now