*/
Do you receive the Programmer's Heaven newsletter? If not, why not subscribe?
*/

View \MODUS.FRM

Example of Shelling to a Windows Program

Submitted By: Unknown
Rating: (Not rated) (Rate It)


VERSION 4.00
Begin VB.Form Form1
   Caption         =   "Example of Shelling to a Windows Program "
   ClientHeight    =   2955
   ClientLeft      =   1140
   ClientTop       =   1515
   ClientWidth     =   6690
   Height          =   3360
   Left            =   1080
   LinkTopic       =   "Form1"
   ScaleHeight     =   2955
   ScaleWidth      =   6690
   Top             =   1170
   Width           =   6810
   Begin VB.CommandButton Command1
      Caption         =   "Start NotePad"
      Height          =   735
      Left            =   3960
      TabIndex        =   0
      Top             =   720
      Width           =   2055
   End
   Begin VB.Label Label7
      BackColor       =   &H00FFFFFF&
      BorderStyle     =   1  'Fixed Single
      Caption         =   $"MODUS.frx":0000
      ForeColor       =   &H00FF0000&
      Height          =   975
      Left            =   480
      TabIndex        =   7
      Top             =   1800
      Width           =   6015
   End
   Begin VB.Label Label6
      Caption         =   "Label6"
      Height          =   255
      Left            =   1800
      TabIndex        =   6
      Top             =   1320
      Width           =   1695
   End
   Begin VB.Label Label5
      Caption         =   "Label5"
      Height          =   255
      Left            =   1800
      TabIndex        =   5
      Top             =   840
      Width           =   1695
   End
   Begin VB.Label Label4
      Caption         =   "Label4"
      Height          =   255
      Left            =   1800
      TabIndex        =   4
      Top             =   480
      Width           =   1695
   End
   Begin VB.Label Label3
      Caption         =   "Time used:"
      Height          =   255
      Left            =   600
      TabIndex        =   3
      Top             =   1320
      Width           =   1095
   End
   Begin VB.Label Label2
      Caption         =   "Finish Time :"
      Height          =   255
      Left            =   600
      TabIndex        =   2
      Top             =   840
      Width           =   1095
   End
   Begin VB.Label Label1
      Caption         =   "Start Time :"
      Height          =   255
      Left            =   600
      TabIndex        =   1
      Top             =   480
      Width           =   1095
   End
End
Attribute VB_Name = "Form1"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
Option Explicit

'*************************************************************************************************************************************************************************************************************************************
' Windows systems directory and Windows Directory Declarations (API)
'*************************************************************************************************************************************************************************************************************************************

Private Declare Function GetSystemDirectory Lib "Kernel" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
Private Declare Function GetWindowsDirectory Lib "Kernel" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
'*************************************************************************************************************************************************************************************************************************************
' GetModuleUsage Declaration to see when the shelled process has terminated
'*************************************************************************************************************************************************************************************************************************************
Private Declare Function GetModuleUsage% Lib "Kernel" (ByVal hModule%)




Private Sub Command1_Click()

Dim iRetVal%                                            ' define the program variables (for this event)
Dim sBuffer$
Dim sSam$
Dim lResult%
Dim Z%

Dim Time(1 To 3) As Date                                ' define the time variable in an array

On Error GoTo ErrorHandler:                             ' define the error handling routine


Time(1) = Now                                           ' set the time that the event started
Label4.Caption = Format$(Time(1), "ttttt")              ' display the time in a label


sBuffer = String$(145, Chr$(0))
   
lResult = GetWindowsDirectory(sBuffer, Len(sBuffer))    ' using API find the windows directory

If lResult <> 0 Then                                    ' if lresult <> 0 then no errors reported
    sSam$ = Left$(sBuffer, lResult) + "\NOTEPAD.EXE"    ' fix the path and filename
    iRetVal = Shell(sSam$, 1)                           ' shell out and Run Windows Notepad
    Form1.WindowState = 1                               ' minimise the VB Window
End If

 While GetModuleUsage(iRetVal) > 0                      ' Using getmoduleusage API find out if
                                                        ' the Shelled program has finished?
      Z% = DoEvents()                                   ' If not, yield to Windows for
                                                        ' windows messages to be processed.
 Wend                                                   ' loop and start the process again
   
Form1.WindowState = 0                                   ' return the state of the VB window
                                                        ' to what is was prior to being minimisd
                                                       
Time(2) = Now                                           ' do the time calculations
Label5.Caption = Format$(Time(2), "ttttt")
Time(3) = Time(2) - Time(1)
Label6.Caption = Format$(Time(3), "hh:mm:ss")
     
'show a message box so you know it worked
MsgBox "The Shelled NotePad application has just terminated", 64


ErrorHandler:   ' generic Error-handling routine.
   
  If Err.Number <> 0 Then
    Select Case Err.Number  ' Evaluate error number.
        Case Err.Number ' tell me anyother errors
            MsgBox "Error # " + Str(Err.Number) + Chr(13) + _
                   "was generated by " + Trim(Err.Source) + Chr(13) + _
                   Trim(Err.Description), 0, "Error"
        Exit Sub
   
    End Select
  End If


End Sub

Private Sub Form_Load()
' Centers this form on the screen
   Left = (Screen.Width - Width) / 2
   Top = (Screen.Height - Height) / 2 - 150  ' -150 so that the program appears above the task bar in Win95
   Me.Move Left, Top

End Sub


Private Sub Form_Unload(Cancel As Integer)

MsgBox "This was a small program designed to be of assistance to beginners in VB.  For any information regarding this program or other examples uploaded by the author, then contact :" & vbCrLf & "" & vbCrLf & "                                Darrin Edwards" & vbCrLf & "                   Innovative Solutions (Australia)" & vbCrLf & "          on CIS at 100234,523.@compuserve.com", vbOKOnly + vbInformation, "Innovative Solutions (Australia)"


End Sub

corner
© 1996-2008 CommunityHeaven LLC. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.