CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2003
    Posts
    5

    Lightbulb How do I send commands to dos exe?

    Does anyone know how to send commands to a exe file that is an old dos exe file?

    I have a program that I use for drainage calculations ktdid.exe. It is an old dos run exe file that is time consuming!!! I would like to use VB to open the file and then send commands to it to run it with the input I have already created. I know that I can use a shell command and I have no problem opening the program, but I can't get the program to recognize sendkeys, sendinput, keybd_event or anything. I am using Microsoft Windows 2000. Does anyone know of any limitations to sending commands to dos exe's? I am desparately in search of anyone that can figure out how to automate this program for me.

    Below is a link to a Kentucky state highway download for ktdid. Ktdid.exe is the file that opens the whole program. If someone can get the program to open and then send a command to get the program to select menu 7 or something I would love to see the code. If anyone is up to the challenge or knows some great advice I would appreciate it. Also attached is the code I have been trying to use to get the file to open and run.

    http://www.kytc.state.ky.us/design/s...tdid/ktdid.htm
    Attached Files Attached Files

  2. #2
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    With a little bit of help from some of Thread1 & others posts, I managed to get it

    Replace your Command2_Click() code with this:

    Code:
    Private Sub Command2_Click()
    Dim cmd As String
    Dim hWndParent&
    Dim hWndChild&
    Dim sText$
    Dim nSC As Byte
    Dim nVK As Byte
    
    cmd = "c:\ktdid\ktdid.exe"
    retval = Shell(cmd, vbNormalFocus)
    For i = 0 To 32000
    ' pause for a bit to let ktdid start up
        DoEvents
    Next i
      'get parent window handle
      hWndParent = FindWindow(vbNullString, cmd)
    
    SetForegroundWindow hWndParent
      
    nVK = vbKey6
    nSC = MapVirtualKey(nVK, 0&)
        
    keybd_event nVK, nSC, 0&, 0
    keybd_event nVK, nSC, KEYEVENTF_KEYUP, 0
    
    SaveSetting App.Path, "Stuff", "Path", Text1
    End Sub
    Add the following declarations into your module1
    Code:
    Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Public Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
    Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Public Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
    Its not perfect, but it should get you going.

    Merry Xmas:wave
    Be nice to Harley riders...

  3. #3
    Join Date
    Apr 2002
    Location
    Sri Lanka
    Posts
    71

    Smile It should be corrected like this...

    Hi,

    The above posting shold be corrected like this. I have downloaded "ktdid.exe". It is working fine...

    Code:
    Option Explicit
    
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    Private Const KEYEVENTF_KEYUP = &H2
    
    Private Sub Command1_Click()
    Dim sFile       As String
    Dim hWndParent  As Long
    Dim i           As Integer
    Dim nSC         As Byte
    Dim nVK         As Byte
    Dim sWorkDir    As String
    
        sFile = "c:\ktdid\ktdid.exe"
        sWorkDir = "c:\ktdid"
        
        ShellExecute hwnd, "open", sFile, vbNullString, sWorkDir, 1
        Sleep 1000
    '    Get parent window handle
        hWndParent = FindWindow(vbNullString, sFile)
        SetForegroundWindow hWndParent
    
        nVK = vbKey7
        nSC = MapVirtualKey(nVK, 0&)
    
        keybd_event nVK, nSC, 0&, 0
        keybd_event nVK, nSC, KEYEVENTF_KEYUP, 0
    
    End Sub
    - Rgds
    - Buddhi -

  4. #4
    Join Date
    Oct 2003
    Location
    Philadelphia, PA
    Posts
    167
    One of the things to remember here is that in VB., the shell command is very, very useful. Many other alngauges require specific packages for specific calls, but with Shell it's pretty much all taken care of for you very easily.
    Mike Dershowitz
    [email protected]
    www.lexientcorp.com

  5. #5
    Join Date
    Dec 2003
    Location
    India
    Posts
    1

    hi

    Hi
    How are you?? Do u know batch file progrmming. then u can do it this things very easily.

    Bye

  6. #6
    Join Date
    Dec 2003
    Posts
    5

    Lightbulb best way to convert text to keybd_events?

    Thanks everybody for their help!!! I worked on this for a long time and couldn't figure this one out without any help, but now I have another question. What is the best way to convert text strings to keyboard events? Here is my best guess so far since I haven't been able to find any shortcut method. I just loop through the string and convert each character to a keyboard event. If the character is either upper case or needs a shift to produce the character I use the shift key in combination with the key. Is this a long way to go about this, or is this they only way???

    Private Sub Text_To_Keys(Text As String)
    Dim tstring As String

    Code

    For num = 1 To Len(Text)
    tstring = LCase(Mid(Text, num, 1))
    If tstring = tstring Then
    TextCase = False
    Else
    TextCase = True
    End If
    If tstring = "a" Then
    Call Send_Keys (65)
    ElseIf tstring = "b" Then
    Call Send_Keys (66)
    ElseIf tstring = "c" Then
    Call Send_Keys (67)

    continuing.........

    ElseIf tstring = ">" Then
    ShiftCase = True
    Call Send_Keys (190)
    ElseIf tstring = "?" Then
    ShiftCase = True
    Call Send_Keys (191)
    ElseIf tstring = "*" Then
    Call Send_Keys (106)
    ElseIf tstring = "-" Then
    Call Send_Keys (109)
    ElseIf tstring = "+" Then
    Call Send_Keys (107)
    ElseIf tstring = " " Then
    Call Send_Keys (32)
    End If

    End Sub

    Private Sub Send_Keys(nVK As Byte)
    Dim nSC As Byte, nvk2 As Byte, nSC2 As Byte

    If nVK > 64 And nVK < 90 Or ShiftCase = True Then
    If TextCase = True Or ShiftCase = True Then
    nvk2 = vbKeyShift
    nSC2 = MapVirtualKey(nvk2, 0&)
    keybd_event nvk2, nSC2, 0&, 0
    End If
    End If

    nSC = MapVirtualKey(nVK, 0&)
    keybd_event nVK, nSC, 0&, 0
    keybd_event nVK, nSC, KEYEVENTF_KEYUP, 0
    keybd_event nvk2, nSC2, KEYEVENTF_KEYUP, 0

    ShiftCase = False

    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured