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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
#include <iostream>
#include <vector>
#include <windows.h>
#include <swdevice.h>
#include <conio.h>
#include <wrl.h>
VOID WINAPI
CreationCallback(
_In_ HSWDEVICE hSwDevice,
_In_ HRESULT hrCreateResult,
_In_opt_ PVOID pContext,
_In_opt_ PCWSTR pszDeviceInstanceId
)
{
HANDLE hEvent = *(HANDLE*) pContext;
SetEvent(hEvent);
UNREFERENCED_PARAMETER(hSwDevice);
UNREFERENCED_PARAMETER(hrCreateResult);
UNREFERENCED_PARAMETER(pszDeviceInstanceId);
}
int __cdecl main(int argc, wchar_t *argv[])
{
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(argv);
HANDLE hEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
HSWDEVICE hSwDevice;
SW_DEVICE_CREATE_INFO createInfo = { 0 };
PCWSTR description = L"Idd Sample Driver";
// These match the Pnp id's in the inf file so OS will load the driver when the device is created
PCWSTR instanceId = L"IddSampleDriver";
PCWSTR hardwareIds = L"IddSampleDriver\0\0";
PCWSTR compatibleIds = L"IddSampleDriver\0\0";
createInfo.cbSize = sizeof(createInfo);
createInfo.pszzCompatibleIds = compatibleIds;
createInfo.pszInstanceId = instanceId;
createInfo.pszzHardwareIds = hardwareIds;
createInfo.pszDeviceDescription = description;
createInfo.CapabilityFlags = SWDeviceCapabilitiesRemovable |
SWDeviceCapabilitiesSilentInstall |
SWDeviceCapabilitiesDriverRequired;
// Create the device
HRESULT hr = SwDeviceCreate(L"IddSampleDriver",
L"HTREE\\ROOT\\0",
&createInfo,
0,
nullptr,
CreationCallback,
&hEvent,
&hSwDevice);
if (FAILED(hr))
{
printf("SwDeviceCreate failed with 0x%lx\n", hr);
return 1;
}
// Wait for callback to signal that the device has been created
printf("Waiting for device to be created....\n");
DWORD waitResult = WaitForSingleObject(hEvent, 10*1000);
if (waitResult != WAIT_OBJECT_0)
{
printf("Wait for device creation failed\n");
return 1;
}
printf("Device created\n\n");
// Now wait for user to indicate the device should be stopped
printf("Press 'x' to exit and destory the software device\n");
bool bExit = false;
do
{
// Wait for key press
int key = _getch();
if (key == 'x' || key == 'X')
{
bExit = true;
}
}while (!bExit);
// Stop the device, this will cause the sample to be unloaded
SwDeviceClose(hSwDevice);
return 0;
}
|