Jump to content

Recommended Posts

Posted

Hi there...

i´ve written a little X86/X64 Driver Installer on a X86 OS. It works under X86 but not right under X64.

 

Spoiler
#RequireAdmin
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <Constants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <String.au3>
#include <Array.au3>
#include <Date.au3>
#include <File.au3>
#NoTrayIcon


If @OSBuild < "7600" Then
    MsgBox(16,"Attention", "This Software require min. Windows 7.")
    Exit
EndIf

If @OSArch = "X64" Then
    DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1)
    $hklm = "HKLM64"
Else
    $hklm = "HKLM"
EndIf

$sysdir = @WindowsDir & "\System32"
$var = _IsServiceRunning('MyService')
$sOutput = ""
$nstring = ""


#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("My Driver", 213, 130, -1, -1)

$Button1 = GUICtrlCreateButton("", 13, 19, 187, 49)
    If $var = False Then
        GUICtrlSetData($Button1, "Install MyDriver")
    Else
        GUICtrlSetData($Button1, "Uninstall MyDriver")
    EndIf


GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _exit()
        Case $Button1
            If _IsServiceRunning('MyService') = False Then
                _InstallMyService()
            Else
                _RemoveMyService('64', 'Driver successfully removed.')
            EndIf

    EndSwitch
WEnd

Func _InstallMyService()

                ;before install
                GUICtrlSetState($Button1, $GUI_disable)
                ;fileinstall
                DirCreate(@TempDir & "\MyDriver")
                If @OSArch = "X64" Then
                    _files(1)
                    _files(2)
                Else
                    _files(3)
                    _files(4)
                EndIf
                ;RegWrite
                _registry(1)
                ;devcon
                cmdread('devcon.exe install ', @TempDir & '\MyDriver\MyDriver.INF ', 'root\MyDevice')

                    ;MsgBox(0, "", $sOutput) ;Just for Testing

                    If _IsServiceRunning('MyService') = True Then
                        DirRemove(@TempDir & "\MyDriver", 1)
                        MsgBox(64,"Info", "Driver successfully installed.", 0 ,$Form1)
                        GUICtrlSetData($Button1, "Uninstall Driver")
                        GUICtrlSetState($Button1, $GUI_enable)
                    EndIf

EndFunc


Func _RemoveMyService($msg1, $msg2)

        GUICtrlSetState($Button1, $GUI_disable)

        If @OSArch = "X64" Then
            _files(2)
        Else
            _files(4)
        EndIf

        _oem() ;to get INF Name/Location for uninstall

        If StringInStr(cmdread('devcon.exe remove', ' root\MyDevice', ' | findstr /R /I /C:"\<devcon"'), "failed") Then MsgBox(64,"Info", "Can't remove Device!", 0 ,$Form1)

        cmdread('sc.exe ', 'delete ', '"MyService"')

        If $nstring = True Then cmdread('dpinst.exe /u ', $nstring, ' /d /s')

        _registry()

        FileDelete($sysdir & "\drivers\MyDriver.sys")

        If _IsServiceRunning('MyService') = False Then
            MsgBox($msg1,"Info", $msg2, 0 ,$Form1)
            GUICtrlSetState($Button1, $GUI_enable)
            GUICtrlSetData($Button1, "Install Driver")
        EndIf

EndFunc


Func _oem()

        Global $nstring = ""
        $pattern = "(.\:|\\\\[^\\]+)(\\*.*\\)(.*)"

        $var = StringRegExp(cmdread('~dc.exe drivernodes "root\MyDevice"', ' | findstr /R /I /C:"\<oem."', ''), $pattern, 1)
            If IsArray ($var) Then
                For $i = 0 To UBound ($var) - 1
                    $nstring &= $var [$i]
                Next
                Return ($nstring)
            Else
                Return False
            EndIf

EndFunc


Func cmdread($cmd1, $cmd2, $cmd3)

    Local $iPID = Run(@ComSpec & " /c " & $cmd1 & $cmd2 & $cmd3, $sysdir, @SW_HIDE, $STDERR_MERGED)
    Global $sOutput = ""
        Do
            $sOutput &= StdoutRead($iPID)
        Until @error

    Return $sOutput

EndFunc


Func _registry($aFlag = 0)
    If $aFlag = 1 Then

        $path = $hklm & "\System\CurrentControlSet\MyDevice"
        $rd = "REG_DWORD"

        RegWrite($path, "Something", $rd, "2")

    Else

        RegDelete($hklm & "\System\CurrentControlSet\MyDevice")

    EndIf

EndFunc

Func _IsServiceRunning($sService)
    Local $oShell = ObjCreate('shell.application')
    If @error Then
        Return SetError(1, 0, False)
    EndIf
    Return $oShell.IsServiceRunning($sService)
EndFunc   ;==>_IsServiceRunning

Func _files($cFlag)

    Switch $cFlag

        Case 1;X64
            FileInstall("C:\MyDriver\X64\devcon.exe", $sysdir & "\devcon.exe", 1)
            FileInstall("C:\MyDriver\mydriver.inf", @TempDir & "\MyDriver\MYDRIVER.INF", 1)
            FileInstall("C:\MyDriver\mydriver.cat", @TempDir & "\MyDriver\MYDRIVER.CAT", 1)
            FileInstall("C:\MyDriver\mydriver.sys", @TempDir & "\MyDriver\MYDRIVER.SYS", 1)

        Case 2;X64
            FileInstall("C:\MyDriver\X64\devcon.exe", $sysdir & "\devcon.exe", 1)
            If FileExists($sysdir & "\dpinst.exe") Then
                FileMove($sysdir & "\dpinst.exe", $sysdir & "\dpinst.exe_bak", 1)
                FileInstall("C:\MyDriver\X64\dpinst.exe", $sysdir & "\dpinst.exe", 1)
            Else
                FileInstall("C:\MyDriver\X64\dpinst.exe", $sysdir & "\dpinst.exe", 1)
            EndIf

        Case 3;X86
            FileInstall("C:\MyDriver\X86\devcon.exe", $sysdir & "\devcon.exe", 1)
            FileInstall("C:\MyDriver\mydriver.inf", @TempDir & "\MyDriver\MYDRIVER.INF", 1)
            FileInstall("C:\MyDriver\mydriver.cat", @TempDir & "\MyDriver\MYDRIVER.CAT", 1)
            FileInstall("C:\MyDriver\mydriver.sys", @TempDir & "\MyDriver\MYDRIVER.SYS", 1)

        Case 4;X86
            FileInstall("C:\MyDriver\X86\devcon.exe", $sysdir & "\devcon.exe", 1)
            If FileExists($sysdir & "\dpinst.exe") Then
                FileMove($sysdir & "\dpinst.exe", $sysdir & "\dpinst.exe_bak", 1)
                FileInstall("C:\MyDriver\X86\dpinst.exe", $sysdir & "\dpinst.exe", 1)
            Else
                FileInstall("C:\MyDriver\X86\dpinst.exe", $sysdir & "\dpinst.exe", 1)
            EndIf

        Case 5
            If FileExists($sysdir & "\dpinst.exe_bak") Then
                FileMove($sysdir & "\dpinst.exe_bak", $sysdir & "\dpinst.exe", 1)
            Else
                FileDelete($sysdir & "\dpinst.exe")
            EndIf

            FileDelete($sysdir & "\devcon.exe")

    EndSwitch

EndFunc

Func _exit()

    _files(5)

    DirRemove(@TempDir & "\MyDriver", 1)

    Exit

EndFunc

 

I use devcon for install and it shows "Driver successfully installed" but there are no Service-Entries like Type, Start type, Display Name, Executable and after reboot its all undone :( If i use CMDLINE with "devcon.exe install mydriver.inf root\mydevice" it works perfect - why not in the Script? Thx for reply :thumbsup:

Posted (edited)

I don't really know, will test here.

EDIT: Not saying this is a problem, just trying to understand, can you explain why you use

$hklm = "HKLM64" ?

What is the specific 64bit registry key?

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet isn't the same for both archs?

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Posted

Thx for reply!

Func _registry() is not so important for the Driver installation.

$hklm = "HKLM64" is for Regwrite into X64 Registry.

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...