Attribute VB_Name = "Install"
'=====================================================================
'     Name: Install.bas
'     Purpose: To define functions that install the driver and chartDll
'     Notes:
'     Last Updated     Initials    Description
'       02/23/04         RY       Original code written
'
'=====================================================================


Public Declare Function ShellExecute Lib "shell32.dll" Alias _
    "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As _
    String, ByVal lpszFile As String, ByVal lpszParams As String, _
    ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
    
Private Declare Function GetCurrentDirectory Lib "kernel32" _
        Alias "GetCurrentDirectoryA" (ByVal nBufferLength As Long, _
        ByVal lpBuffer As String) As Long

Private Declare Function GetSystemDirectory Lib "kernel32" _
    Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, _
    ByVal nSize As Long) As Long

Private Declare Function GetTempPath Lib "kernel32" _
Alias "GetTempPathA" (ByVal nBufferLength As Long, _
ByVal lpBuffer As String) As Long

Private Declare Function GetWindowsDirectory Lib "kernel32" _
    Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, _
    ByVal nSize As Long) As Long
    
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)



Public Sub load_driver()
Dim hwnd As Long
Dim return_value As Long
Dim curDir As String
Dim sysDir As String
Dim tmpDir As String
Dim winDir As String
Dim manip_path As String
Dim manip_len As Integer
curDir = Space(500)
sysDir = Space(500)
tmpDir = Space(500)
winDir = Space(500)
curDir = Left(curDir, GetCurrentDirectory(Len(curDir), curDir))
sysDir = Left(sysDir, GetSystemDirectory(sysDir, Len(sysDir)))
tmpDir = Left(tmpDir, GetTempPath(Len(tmpDir), tmpDir))
winDir = Left(winDir, GetWindowsDirectory(winDir, Len(winDir)))

' Since the installer does not copy windrvr.exe and all that stuff directly to program folder and since
' they are stored in ./source/vb instead of ./program changing app path
On Error GoTo Err_handle_driver

manip_path = App.Path
manip_len = Len(manip_path)
manip_len = manip_len - 7
Mid(manip_path, manip_len) = "\Source\"
manip_path = manip_path & "vb"
If (UCase(Mid$(App.Path, manip_len))) = UCase("\Program") Then ' installed using setup

Else
    manip_path = App.Path
End If
'MsgBox manip_path
    If Dir$(manip_path & "\driverinstall.txt") = "" Then
        FileCopy manip_path & "\prvdrv.dll", winDir & "\prvdrv.dll"
        If Dir$(winDir & "\system\vmm32\") <> "" Then
            FileCopy manip_path & "\Windrvr.vxd", winDir & "\system\vmm32\Windrvr.vxd"
        End If
        If Dir$(winDir & "\system32\drivers\") <> "" Then
            FileCopy manip_path & "\Windrvr.sys", winDir & "\system32\drivers\Windrvr.sys"
        End If
        return_value = ShellExecute(hwnd, "open", manip_path & "\wdreg.exe", "install", "C:\", 1)  '<---- 1=Show normal
        Sleep (100)
        F3 = FreeFile
        Open manip_path & "\driverinstall.txt" For Output As F3
        Close #F3
    End If

Exit_load_driver:
    Exit Sub

Err_handle_driver:
    HandleError ("Could not load driver...")
    'MsgBox "Could not load driver...", , "parvError"
    Resume Exit_load_driver

    End Sub
Public Sub HandleError(Err As String)
    MsgBox Err, , "parvError"
End Sub

Public Sub install_chartdll()
Dim hwnd As Long
Dim return_value As Long
Dim curDir As String
Dim sysDir As String
Dim tmpDir As String
Dim winDir As String
Dim manip_path As String
Dim manip_len As Integer
curDir = Space(500)
sysDir = Space(500)
tmpDir = Space(500)
winDir = Space(500)
curDir = Left(curDir, GetCurrentDirectory(Len(curDir), curDir))
sysDir = Left(sysDir, GetSystemDirectory(sysDir, Len(sysDir)))
tmpDir = Left(tmpDir, GetTempPath(Len(tmpDir), tmpDir))
winDir = Left(winDir, GetWindowsDirectory(winDir, Len(winDir)))

On Error GoTo Err_Handle_chartdll

manip_path = App.Path
manip_len = Len(manip_path)
manip_len = manip_len - 7
Mid(manip_path, manip_len) = "\Source\"
manip_path = manip_path & "vb"

If (UCase(Mid$(App.Path, manip_len))) = UCase("\Program") Then ' installed using setup
Else
    manip_path = App.Path
End If

If Dir$(manip_path & "\dllinstall.txt") = "" Then
    If Dir$(winDir & "\system\") <> "" Then
        If Dir$(winDir & "\system\msstdfmt.dll") = "" Then
            FileCopy manip_path & "\msstdfmt.dll", winDir & "\system\msstdfmt.dll"
        End If
        return_value = ShellExecute(hwnd, "open", "regsvr32.exe", winDir & "\system\msstdfmt.dll", "C:\", 1) '<---- 1=Show normal
    End If
    If Dir$(winDir & "\system32\") <> "" Then
        If Dir$(winDir & "\system32\msstdfmt.dll") = "" Then
           FileCopy manip_path & "\msstdfmt.dll", winDir & "\system32\msstdfmt.dll"
        End If
        return_value = ShellExecute(hwnd, "open", "regsvr32.exe", winDir & "\system32\msstdfmt.dll", "C:\", 1) '<---- 1=Show normal
    End If
    f = FreeFile
    Open manip_path & "\dllinstall.txt" For Output As f
    Close #f
 End If

Exit_install_chartdll:
    Exit Sub

Err_Handle_chartdll:
    HandleError "Chart Dll install failed.."
    Resume Exit_install_chartdll
    'MsgBox "Chart Dll install failed..", , "parvError"

End Sub
