'**************************************
'Windows API/Global Declarations for :Execute_Program
'**************************************
'Declares for Execute_Program
Declare Sub BringWindowToTop Lib "user" (ByVal hWnd As Integer)
Declare Function SendMessage Lib "User" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Long
Const WM_CLOSE = &H10
Declare Sub SetWindowPos Lib "user" (ByVal hWnd%, ByVal hwndAfter%, ByVal x%, ByVal y%, ByVal cx%, ByVal cy%, ByVal swp%)
Const HWND_TOP = 0
Const HWND_BOTTOM = 1
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const GWL_ID = (-12)
Const GW_HWNDNEXT = 2
Const GW_CHILD = 5
Const FWP_STARTSWITH = 0
Const FWP_CONTAINS = 1
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Integer, ByVal lpszOp As String, ByVal lpszFile As String, ByVal spszParams As String, ByVal lpszDir As String, ByVal fsShowCmd As Integer) As Integer
Const SW_SHOW = 5
'**************************************
' Name: Execute_Program
' Description:Runs a program and handles all possible errors (such as running out
' of memory, file can't be opened, etc.) Also, unlike the VB Shell command, it allows
' you to specify a 'default working directory'!
' Also, allows you to run a file that is only an association--example:you can run a .txt file with this function!
' By: Ian Ippolito (RAC)
'
' Minor upadates to allow integration with 32 bit environments and to allow error message "No association for this file."
' By Sarah Connor
'
' Inputs:strFilePath--program to run
' strParms--program command line parms (if any)
' strDir--default working directory
'
' Returns:returns TRUE=successful
' FALSE=failed
'
' Assumes:None
'
' Side Effects:None
' This code is copyrighted and has limited warranties.
' Please see [url]http://www.Planet-Source-Code.com/x...ts/ShowCode.htm[/url] for details.
'**************************************
Function Execute_Program(ByVal strFilePath As String, ByVal strParms As String, ByVal strDir As String) As Integer
'run program
Dim hwndProgram As Integer
hwndProgram = ShellExecute(0, "Open", strFilePath, strParms, strDir, SW_SHOW)
'evaluate errors
Select Case (hwndProgram)
Case 0
MsgBox("Insufficent system memory or corrupt program file.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 2
MsgBox("File not found.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 3
MsgBox("Invalid path.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 5
MsgBox("Sharing or Protection Error.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 6
MsgBox("Seperate data segments are required for each task.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 8
MsgBox("Insufficient memory to run the program.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 10
MsgBox("Incorrect Windows version.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 11
MsgBox("Invalid program file.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 12
MsgBox("Program file requires a different operating system.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 13
MsgBox("Program requires MS-DOS 4.0.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 14
MsgBox("Unknown program file type.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 15
MsgBox("Windows program does not support protected memory mode.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 16
MsgBox("Invalid use of data segments when loading a second instance of a program.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 19
MsgBox("Attempt to run a compressed program file.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 20
MsgBox("Invalid dynamic link library.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 21
MsgBox("Program requires Windows 32-bit extensions.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 31
MsgBox("No association for this file", vbOKOnly + vbInformation, strFilePath)
Exit Function
End Select
Execute_Program = True
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' To open an INI file with the associated file handler (generally NOTEPAD.EXE) use:
Execute_Program("c:\windows\win.ini", "", "")
' To open an JPG image with the associated file handler use:
Execute_Program("c:\mypic.jpg", "", "")
End Sub