-
Notifications
You must be signed in to change notification settings - Fork 4
/
install.go
69 lines (57 loc) · 2.32 KB
/
install.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package windevice
import (
"fmt"
"github.com/gentlemanautomaton/windevice/devicecreation"
"github.com/gentlemanautomaton/windevice/deviceid"
"github.com/gentlemanautomaton/windevice/deviceregistry"
"github.com/gentlemanautomaton/windevice/difunc"
"github.com/gentlemanautomaton/windevice/infpath"
"github.com/gentlemanautomaton/windevice/installflag"
"github.com/gentlemanautomaton/windevice/newdevapi"
"github.com/gentlemanautomaton/windevice/setupapi"
)
// Install attempts to install a device with the provided hardware identifier,
// setup information file and description.
func Install(id deviceid.Hardware, path, description string, flags installflag.Value) (instance deviceid.DeviceInstance, needReboot bool, err error) {
// Make sure the supplied hardware ID is valid
if err := id.Validate(); err != nil {
return "", false, err
}
// Make sure the supplied INF file path is valid
path, err = infpath.Prepare(path)
if err != nil {
return "", false, err
}
// Ask windows to retrieve the name and GUID from the INF file
name, guid, err := setupapi.GetInfClass(path)
if err != nil {
return "", false, fmt.Errorf("failed to read inf file: %v", err)
}
// Prepare a fresh device information set
devices, err := setupapi.CreateDeviceInfoList(&guid)
if err != nil {
return "", false, fmt.Errorf("failed to create device information set: %v", err)
}
defer setupapi.DestroyDeviceInfoList(devices)
// Add a new device to the set
device, err := setupapi.CreateDeviceInfo(devices, name, guid, description, devicecreation.GenerateID)
if err != nil {
return "", false, fmt.Errorf("failed to create device: %v", err)
}
// Set the hardware ID
if err := setupapi.SetDeviceRegistryStrings(devices, device, deviceregistry.HardwareID, []string{string(id)}); err != nil {
return "", false, fmt.Errorf("failed to set hardware ID: %v", err)
}
// Register the device
if err := setupapi.CallClassInstaller(difunc.RegisterDevice, devices, &device); err != nil {
return "", false, fmt.Errorf("failed to register device: %v", err)
}
// Update the device driver
needReboot, err = newdevapi.UpdateDriverForPlugAndPlayDevices(id, path, flags)
if err != nil {
return "", needReboot, err
}
// Get the device instance ID of the new device
instance, err = setupapi.GetDeviceInstanceID(devices, device)
return instance, needReboot, err
}