Solved

Shelling out to DOS

Posted on 1998-11-17
2
161 Views
Last Modified: 2010-08-05
I have a VB program that repeatdly shells out to DOS. I want to make it run faster by shelling out once, and keeping the same DOS window open for future "shell outs." Is this possible?
0
Comment
Question by:rsoft1
2 Comments
 
LVL 3

Expert Comment

by:Sendoh
ID: 1445361
Hi !

Is that you mean you need to shell app 1.
Then keep that shell for future use without terminate it ??
0
 
LVL 14

Accepted Solution

by:
waty earned 50 total points
ID: 1445362
You should write a batch file and execute it once. It will be the easiest (if possible).

Otherwise, you could use the following class to send keystroke to dos command box :

' #VBIDEUtils#************************************************************
' * Programmer Name  : Waty Thierry
' * Web Site         : www.geocities.com/ResearchTriangle/6311/
' * E-Mail           : waty.thierry@usa.net
' * Date             : 24/09/98
' * Time             : 13:36
' * Module Name      : class_SendKeys
' * Module Filename  :
' **********************************************************************
' * Comments         : Allows users to be able to send keystrokes to
' *                    dos programs running in a windows95 dos box
' *
' * This class has one property, Destination, which needs to be the handle
' *  returned from the shell function of the dos program or any program
' *  started with the shell function.
' * It also has one method called, SendKeys, this is the string to be sent
' *  to the destination.
' *
' **********************************************************************

Option Explicit

Private mvarDestination As Long 'local copy
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_SHIFT = &H10

Private Declare Function OemKeyScan Lib "user32" (ByVal wsOemchar As Integer) As Long
Private Declare Function CharToOem Lib "user32" Alias "CharToOemA" (ByVal lpszSrc As String, ByVal lpszDst As String) As Long
Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" (ByVal cChar As Byte) As Integer
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType 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 Sub SendAKey(ByVal sKeys As String)

   Dim vk            As Integer
   Dim nShiftScan    As Integer
   Dim nScan         As Integer
   Dim sOemchar      As String
   Dim nShiftKey     As Integer
   
   ' *** Get the virtual key code for this character
   vk = VkKeyScan(Asc(sKeys)) And &HFF
   ' *** See if shift key needs to be pressed
   nShiftKey = VkKeyScan(Asc(sKeys)) And 256
   sOemchar = " " ' 2 character buffer
   ' *** Get the OEM character - preinitialize the buffer
   CharToOem Left$(sKeys, 1), sOemchar
   ' *** Get the nScan code for this key
   nScan = OemKeyScan(Asc(sOemchar)) And &HFF
   ' *** Send the key down

   If nShiftKey = 256 Then
      ' *** if shift key needs to be pressed
      nShiftScan = MapVirtualKey(VK_SHIFT, 0)
      ' *** press down the shift key
      keybd_event VK_SHIFT, nShiftScan, 0, 0
   End If

   ' *** press key to be sent
   keybd_event vk, nScan, 0, 0
   ' *** Send the key up

   If nShiftKey = 256 Then
      ' *** keyup for shift key
      keybd_event VK_SHIFT, nShiftScan, KEYEVENTF_KEYUP, 0
   End If

   ' ***keyup for key sent
   keybd_event vk, nScan, KEYEVENTF_KEYUP, 0
   
End Sub

Public Sub SendKeys(ByVal sKeys As String)

   Dim x          As Integer
   
   ' *** Loop thru string to send one key at a time
   For x = 1 To Len(sKeys)
      ' *** Activate target application
      AppActivate (mvarDestination)
      ' *** Send one key to target
      SendAKey Mid$(sKeys, x, 1)
   Next

End Sub

Public Property Let Destination(ByVal vData As Long)
   'used when assigning a value to the property, on the left si
   '     de of an assignment.
   
   ' ***Syntax: X.Destination = 5
   mvarDestination = vData
   
End Property

Public Property Get Destination() As Long
   
   'used when retrieving value of a property, on the right side
   '      of an assignment.
   ' ***Syntax: Debug.Print X.Destination
   Destination = mvarDestination
   
End Property

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

856 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