Driver installation and update: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (direct link)
m (→‎The Header: Added ALL constant definitions for UpdateDriverForPlugAndPlayDevices(..))
 
(7 intermediate revisions by 6 users not shown)
Line 4: Line 4:
I always thought it's not very nice nor professional to have users hunt for a driver when a device is attached to their system. Especially if there's some application software for that device that they install anyway.
I always thought it's not very nice nor professional to have users hunt for a driver when a device is attached to their system. Especially if there's some application software for that device that they install anyway.


Wouldn't it be nice if the user could just install the software, which would install the driver automatically? And then they plug in their device and windows already knows where the driver is! It'd be even better if the driver could be updated without having to disconnect the device, right? Well, the attached script does that too -- on 2000/XP, that is.
Wouldn't it be nice if the user could just install the software, which would install the driver automatically? And then they plug in their device and windows already knows where the driver is! It'd be even better if the driver could be updated without having to disconnect the device, right? Well, the attached script does that too -- on 2000/XP, that is. For Vista and above try the MS [http://msdn.microsoft.com/en-us/library/ff550419%28v=vs.85%29.aspx  PnPUtil] utility.


First, let's define some winapi functions. These go into drvsetup.nsh
First, let's define some winapi functions. These go into drvsetup.nsh
Line 10: Line 10:
== The Header ==
== The Header ==
<highlight-nsis>
<highlight-nsis>
; BOOL UpdateDriverForPlugAndPlayDevices(HWND, PSTR, PSTR, DWORD, PBOOL);
/*
  BOOL UpdateDriverForPlugAndPlayDevices(
    _In_opt_  HWND hwndParent,
    _In_      LPCTSTR HardwareId,
    _In_      LPCTSTR FullInfPath,
    _In_      DWORD InstallFlags,
    _Out_opt_  PBOOL bRebootRequired
  );
*/
!define sysUpdateDriverForPlugAndPlayDevices "newdev::UpdateDriverForPlugAndPlayDevices(i, t, t, i, *i) i"
!define sysUpdateDriverForPlugAndPlayDevices "newdev::UpdateDriverForPlugAndPlayDevices(i, t, t, i, *i) i"
!define NO_ERROR              0
!define ERROR_FILE_NOT_FOUND  2
!define ERROR_IN_WOW64        -536870347  ; 0xE0000235
!define ERROR_INVALID_FLAGS  1004        ; 0x3EC
!define ERROR_NO_SUCH_DEVINST -536870389  ; 0xE000020B
; the masked value of ERROR_NO_SUCH_DEVINST is 523
; the masked value of ERROR_NO_SUCH_DEVINST is 523
!define ERROR_NO_SUCH_DEVINST -536870389
 
!define INSTALLFLAG_FORCE          0x1
!define INSTALLFLAG_READONLY        0x2
!define INSTALLFLAG_NONINTERACTIVE  0x4
; Use FORCE flag ex: pushing an older release over top of new one.
 


;BOOL SetupCopyOEMInf(PSTR, PSTR, DWORD, DWORD, PSTR, DWORD, PDWORD, PSTR);
;BOOL SetupCopyOEMInf(PSTR, PSTR, DWORD, DWORD, PSTR, DWORD, PDWORD, PSTR);

Latest revision as of 00:00, 16 January 2013

Author: kuba (talk, contrib)


Description

I always thought it's not very nice nor professional to have users hunt for a driver when a device is attached to their system. Especially if there's some application software for that device that they install anyway.

Wouldn't it be nice if the user could just install the software, which would install the driver automatically? And then they plug in their device and windows already knows where the driver is! It'd be even better if the driver could be updated without having to disconnect the device, right? Well, the attached script does that too -- on 2000/XP, that is. For Vista and above try the MS PnPUtil utility.

First, let's define some winapi functions. These go into drvsetup.nsh

The Header

/*
  BOOL UpdateDriverForPlugAndPlayDevices(
    _In_opt_   HWND hwndParent,
    _In_       LPCTSTR HardwareId,
    _In_       LPCTSTR FullInfPath,
    _In_       DWORD InstallFlags,
    _Out_opt_  PBOOL bRebootRequired
  );
*/
!define sysUpdateDriverForPlugAndPlayDevices "newdev::UpdateDriverForPlugAndPlayDevices(i, t, t, i, *i) i"
!define NO_ERROR              0
!define ERROR_FILE_NOT_FOUND  2
!define ERROR_IN_WOW64        -536870347  ; 0xE0000235
!define ERROR_INVALID_FLAGS   1004        ; 0x3EC
!define ERROR_NO_SUCH_DEVINST -536870389  ; 0xE000020B
; the masked value of ERROR_NO_SUCH_DEVINST is 523
 
!define INSTALLFLAG_FORCE           0x1
!define INSTALLFLAG_READONLY        0x2
!define INSTALLFLAG_NONINTERACTIVE  0x4
; Use FORCE flag ex: pushing an older release over top of new one.
 
 
;BOOL SetupCopyOEMInf(PSTR, PSTR, DWORD, DWORD, PSTR, DWORD, PDWORD, PSTR);
!define sysSetupCopyOEMInf "setupapi::SetupCopyOEMInf(t, t, i, i, i, i, *i, t) i"
!define SPOST_NONE 0
!define SPOST_PATH 1
!define SPOST_URL 2
!define SP_COPY_DELETESOURCE 0x1
!define SP_COPY_REPLACEONLY 0x2
!define SP_COPY_NOOVERWRITE 0x8
!define SP_COPY_OEMINF_CATALOG_ONLY 0x40000

And then the actual function, which goes into driver.nsh. Note that it assumes you have GetWindowsVersion function pasted into "winver.nsh".

The Function

!include "winver.nsh"
!include "drvsetup.nsh"
 
;
; Written by Kuba Ober
; Copyright (c) 2004 Kuba Ober
;
; Permission is hereby granted, free of charge, to any person obtaining a 
; copy of this software and associated documentation files (the "Software"), 
; to deal in the Software without restriction, including without limitation 
; the rights to use, copy, modify, merge, publish, distribute, sublicense, 
; and/or sell copies of the Software, and to permit persons to whom the 
; Software is furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in 
; all copies or substantial portions of the Software.
; 
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
; DEALINGS IN THE SOFTWARE.
 
;
; U S A G E
;
; Push "c:\program files\yoursoftware\driver" 
;  -- the directory of the .inf file
; Push "c:\program files\yoursoftware\driver\driver.inf"
;  -- the filepath of the .inf file (directory + filename)
; Push "USB\VID_1234&PID_5678"
;  -- the HID (Hardware ID) of your device
; Call InstallUpgradeDriver
;
; Your driver (minimally the .inf and .sys files) should already by installed
; by your NSIS script.
;
; Typically, you would put the driver either in $INSTDIR or $INSTDIR\Driver
; It's up to you, of course.
;
; The driver (i.e. .inf, .sys and related files) must be present for the
; lifetime of your application, you shouldn't remove them after calling
; this function!
;
; You DON'T want to put the driver in any of system directories. Windows
; will do it when the device is first plugged in.
 
Function InstallUpgradeDriver
 
 Pop $R0 ; HID
 Pop $R1 ; INFPATH
 Pop $R2 ; INFDIR
 
 ; Get the Windows version
 Call GetWindowsVersion
 Pop $R3 ; Windows Version
 ;DetailPrint 'Windows Version: $R3'
 StrCmp $R3 '2000' lbl_upgrade
 StrCmp $R3 'XP' lbl_upgrade
 StrCmp $R3 '2003' lbl_upgrade
 DetailPrint "Windows $R3 doesn't support automatic driver updates."
 
 ; Upgrade the driver if the device is already plugged in
 Goto lbl_noupgrade
lbl_upgrade:
 System::Get '${sysUpdateDriverForPlugAndPlayDevices}'
 Pop $0
 StrCmp $0 'error' lbl_noapi
 DetailPrint "Updating the driver..."
 ; 0, HID, INFPATH, 0, 0
 Push $INSTDIR ; Otherwise this function will swallow it, dunno why
 System::Call '${sysUpdateDriverForPlugAndPlayDevices}?e (0, R0, R1, 0, 0) .r0'
 Pop $1 ; last error
 Pop $INSTDIR
 IntCmp $0 1 lbl_done
 IntCmp $1 ${ERROR_NO_SUCH_DEVINST} lbl_notplugged
 
 DetailPrint "Driver update has failed. ($R3:$0,$1)"
 Goto lbl_noupgrade
lbl_notplugged:
 DetailPrint "The device is not plugged in, cannot update the driver."
 Goto lbl_noupgrade
lbl_noapi:
 DetailPrint "Your Windows $R3 doesn't support driver updates."
 
lbl_noupgrade:
 ; Pre-install the driver
 System::Get '${sysSetupCopyOEMInf}'
 Pop $0
 StrCmp $0 'error' lbl_inoapi
 DetailPrint "Installing the driver..."
 ; INFPATH, INFDIR, SPOST_PATH, "", 0, 0, 0, 0
 System::Call '${sysSetupCopyOEMInf}?e (R1, R2, ${SPOST_PATH}, 0, 0, 0, 0, 0) .r0'
 Pop $1 ; last error
 IntCmp $0 1 lbl_nodriver
 DetailPrint 'Driver pre-installation has failed with error #$1 ($R3)'
 Goto lbl_done
lbl_inoapi:
 DetailPrint "Your Windows $R3 doesn't support driver pre-installation."
lbl_nodriver:
lbl_done:
 
FunctionEnd