Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create a duplicate finder Application 9 115
VBA open file from excel cell 4 36
SQL VB connection works in one PC and doesn't in another 15 58
using web browser with BING 40 110
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

895 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

17 Experts available now in Live!

Get 1:1 Help Now