1
/* LIBUSB-WIN32, Generic Windows USB Library
2
* Copyright (c) 2002-2006 Stephan Meyer <ste_meyer@web.de>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#define _WIN32_IE 0x0400
37
#define __INF_WIZARD_C__
38
#include "inf_wizard_rc.rc"
41
#define _STRINGIFY(x) #x
42
#define STRINGIFY(x) _STRINGIFY(x)
45
DEFINE_GUID(GUID_DEVINTERFACE_USB_HUB, 0xf18a0e88, 0xc30c, 0x11d0, 0x88, \
46
0x15, 0x00, 0xa0, 0xc9, 0x06, 0xbe, 0xd8);
48
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, \
49
0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
51
const char cat_file_content[] =
52
"This file will contain the digital signature of the files to be installed\n"
54
"This file will be provided by Microsoft upon certification of your "
57
const char info_text_0[] =
58
"This program will create an .inf file for your device.\n\n"
59
"Before clicking \"Next\" make sure that your device is connected to the "
62
const char info_text_1[] =
63
"An .inf and .cat file has been created successfully for the following "
66
const char list_header_text[] =
67
"Select your device from the list of detected devices below.\n"
68
"If your device isn't listed then either connect it or just click \"Next\"\n"
69
"and enter your device description manually\n";
71
const char inf_header[] =
73
"Signature = \"$Chicago$\"\n"
74
"provider = %manufacturer%\n"
75
"DriverVer = " STRINGIFY(INF_DATE) "," STRINGIFY(INF_VERSION) "\n";
77
const char inf_body[] =
78
"Class = LibUsbDevices\n"
79
"ClassGUID = {EB781AAF-9C70-4523-A5DF-642A87ECA567}\n"
82
"AddReg=libusb_class_install_add_reg\n"
85
"AddReg=libusb_class_install_add_reg\n"
87
"[libusb_class_install_add_reg]\n"
88
"HKR,,,,\"LibUSB-Win32 Devices\"\n"
89
"HKR,,Icon,,\"-20\"\n"
92
"%manufacturer%=Devices,NT,NTAMD64\n"
94
";--------------------------------------------------------------------------\n"
96
";--------------------------------------------------------------------------\n"
98
"[SourceDisksNames]\n"
99
"1 = \"Libusb-Win32 Driver Installation Disk\",,\n"
101
"[SourceDisksFiles]\n"
102
"libusb0.sys = 1,,\n"
103
"libusb0.dll = 1,,\n"
104
"libusb0_x64.sys = 1,,\n"
105
"libusb0_x64.dll = 1,,\n"
107
"[DestinationDirs]\n"
108
"libusb_files_sys = 10,system32\\drivers\n"
109
"libusb_files_sys_x64 = 10,system32\\drivers\n"
110
"libusb_files_dll = 10,system32\n"
111
"libusb_files_dll_wow64 = 10,syswow64\n"
112
"libusb_files_dll_x64 = 10,system32\n"
114
"[libusb_files_sys]\n"
117
"[libusb_files_sys_x64]\n"
118
"libusb0.sys,libusb0_x64.sys\n"
120
"[libusb_files_dll]\n"
123
"[libusb_files_dll_wow64]\n"
126
"[libusb_files_dll_x64]\n"
127
"libusb0.dll,libusb0_x64.dll\n"
129
";--------------------------------------------------------------------------\n"
131
";--------------------------------------------------------------------------\n"
134
"CopyFiles = libusb_files_sys, libusb_files_dll\n"
135
"AddReg = libusb_add_reg\n"
138
"CopyFiles = libusb_files_sys, libusb_files_dll\n"
140
"[LIBUSB_DEV.NTAMD64]\n"
141
"CopyFiles = libusb_files_sys_x64, libusb_files_dll_wow64, libusb_files_dll_x64\n"
144
"DelReg = libusb_del_reg_hw\n"
145
"AddReg = libusb_add_reg_hw\n"
147
"[LIBUSB_DEV.NT.HW]\n"
148
"DelReg = libusb_del_reg_hw\n"
149
"AddReg = libusb_add_reg_hw\n"
151
"[LIBUSB_DEV.NTAMD64.HW]\n"
152
"DelReg = libusb_del_reg_hw\n"
153
"AddReg = libusb_add_reg_hw\n"
155
"[LIBUSB_DEV.NT.Services]\n"
156
"AddService = libusb0, 0x00000002, libusb_add_service\n"
158
"[LIBUSB_DEV.NTAMD64.Services]\n"
159
"AddService = libusb0, 0x00000002, libusb_add_service\n"
162
"HKR,,DevLoader,,*ntkern\n"
163
"HKR,,NTMPDriver,,libusb0.sys\n"
165
"; Older versions of this .inf file installed filter drivers. They are not\n"
166
"; needed any more and must be removed\n"
167
"[libusb_del_reg_hw]\n"
168
"HKR,,LowerFilters\n"
169
"HKR,,UpperFilters\n"
171
"; Device properties\n"
172
"[libusb_add_reg_hw]\n"
173
"HKR,,SurpriseRemovalOK, 0x00010001, 1\n"
175
";--------------------------------------------------------------------------\n"
177
";--------------------------------------------------------------------------\n"
179
"[libusb_add_service]\n"
180
"DisplayName = \"LibUsb-Win32 - Kernel Driver "
181
STRINGIFY(INF_DATE) ", " STRINGIFY(INF_VERSION) "\"\n"
185
"ServiceBinary = %12%\\libusb0.sys\n"
187
";--------------------------------------------------------------------------\n"
189
";--------------------------------------------------------------------------\n"
192
const char strings_header[] =
194
";--------------------------------------------------------------------------\n"
196
";--------------------------------------------------------------------------\n"
203
char description[MAX_PATH];
204
char manufacturer[MAX_PATH];
208
BOOL CALLBACK dialog_proc_0(HWND dialog, UINT message,
209
WPARAM w_param, LPARAM l_param);
210
BOOL CALLBACK dialog_proc_1(HWND dialog, UINT message,
211
WPARAM w_param, LPARAM l_param);
212
BOOL CALLBACK dialog_proc_2(HWND dialog, UINT message,
213
WPARAM w_param, LPARAM l_param);
214
BOOL CALLBACK dialog_proc_3(HWND dialog, UINT message,
215
WPARAM w_param, LPARAM l_param);
217
static void device_list_init(HWND list);
218
static void device_list_refresh(HWND list);
219
static void device_list_add(HWND list, device_context_t *device);
220
static void device_list_clean(HWND list);
222
static int save_file(HWND dialog, device_context_t *device);
224
int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev_instance,
225
LPSTR cmd_line, int cmd_show)
228
device_context_t device;
230
LoadLibrary("comctl32.dll");
231
InitCommonControls();
233
memset(&device, 0, sizeof(device));
235
next_dialog = ID_DIALOG_0;
242
next_dialog = (int)DialogBoxParam(instance,
243
MAKEINTRESOURCE(next_dialog),
244
NULL, (DLGPROC)dialog_proc_0,
249
next_dialog = (int)DialogBoxParam(instance,
250
MAKEINTRESOURCE(next_dialog),
251
NULL, (DLGPROC)dialog_proc_1,
255
next_dialog = (int)DialogBoxParam(instance,
256
MAKEINTRESOURCE(next_dialog),
257
NULL, (DLGPROC)dialog_proc_2,
261
next_dialog = (int)DialogBoxParam(instance,
262
MAKEINTRESOURCE(next_dialog),
263
NULL, (DLGPROC)dialog_proc_3,
275
BOOL CALLBACK dialog_proc_0(HWND dialog, UINT message,
276
WPARAM w_param, LPARAM l_param)
281
SetWindowText(GetDlgItem(dialog, ID_INFO_TEXT), info_text_0);
285
switch(LOWORD(w_param))
288
EndDialog(dialog, ID_DIALOG_1);
290
case ID_BUTTON_CANCEL:
292
EndDialog(dialog, 0);
300
BOOL CALLBACK dialog_proc_1(HWND dialog, UINT message,
301
WPARAM w_param, LPARAM l_param)
303
static HDEVNOTIFY notification_handle_hub = NULL;
304
static HDEVNOTIFY notification_handle_dev = NULL;
305
DEV_BROADCAST_HDR *hdr = (DEV_BROADCAST_HDR *) l_param;
306
DEV_BROADCAST_DEVICEINTERFACE dev_if;
307
static device_context_t *device = NULL;
308
HWND list = GetDlgItem(dialog, ID_LIST);
314
device = (device_context_t *)l_param;
315
memset(device, 0, sizeof(*device));
317
SetWindowText(GetDlgItem(dialog, ID_LIST_HEADER_TEXT), list_header_text);
318
device_list_init(list);
319
device_list_refresh(list);
321
dev_if.dbcc_size = sizeof(dev_if);
322
dev_if.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
324
dev_if.dbcc_classguid = GUID_DEVINTERFACE_USB_HUB;
325
notification_handle_hub = RegisterDeviceNotification(dialog, &dev_if, 0);
327
dev_if.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;
328
notification_handle_dev = RegisterDeviceNotification(dialog, &dev_if, 0);
332
case WM_DEVICECHANGE:
335
case DBT_DEVICEREMOVECOMPLETE:
336
if(hdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
337
device_list_refresh(list);
339
case DBT_DEVICEARRIVAL:
340
if(hdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
341
device_list_refresh(list);
349
switch(LOWORD(w_param))
352
if(notification_handle_hub)
353
UnregisterDeviceNotification(notification_handle_hub);
354
if(notification_handle_dev)
355
UnregisterDeviceNotification(notification_handle_dev);
357
memset(&item, 0, sizeof(item));
358
item.mask = LVIF_TEXT | LVIF_PARAM;
359
item.iItem = ListView_GetNextItem(list, -1, LVNI_SELECTED);
361
memset(device, 0, sizeof(*device));
365
if(ListView_GetItem(list, &item))
369
memcpy(device, (void *)item.lParam, sizeof(*device));
376
device->vid = 0x12AB;
377
device->pid = 0x12AB;
380
if(!device->manufacturer[0])
381
strcpy(device->manufacturer, "Insert manufacturer name");
382
if(!device->description[0])
383
strcpy(device->description, "Insert device description");
385
if(notification_handle_hub)
386
UnregisterDeviceNotification(notification_handle_hub);
387
if(notification_handle_dev)
388
UnregisterDeviceNotification(notification_handle_dev);
390
device_list_clean(list);
392
EndDialog(dialog, ID_DIALOG_2);
396
device_list_clean(list);
397
if(notification_handle_hub)
398
UnregisterDeviceNotification(notification_handle_hub);
399
if(notification_handle_dev)
400
UnregisterDeviceNotification(notification_handle_dev);
401
EndDialog(dialog, ID_DIALOG_0);
404
case ID_BUTTON_CANCEL:
406
device_list_clean(list);
407
if(notification_handle_hub)
408
UnregisterDeviceNotification(notification_handle_hub);
409
if(notification_handle_dev)
410
UnregisterDeviceNotification(notification_handle_dev);
411
EndDialog(dialog, 0);
419
BOOL CALLBACK dialog_proc_2(HWND dialog, UINT message,
420
WPARAM w_param, LPARAM l_param)
422
static device_context_t *device = NULL;
428
device = (device_context_t *)l_param;
432
memset(tmp, 0, sizeof(tmp));
433
sprintf(tmp, "0x%04X", device->vid);
434
SetWindowText(GetDlgItem(dialog, ID_TEXT_VID), tmp);
436
memset(tmp, 0, sizeof(tmp));
437
sprintf(tmp, "0x%04X", device->pid);
438
SetWindowText(GetDlgItem(dialog, ID_TEXT_PID), tmp);
440
SetWindowText(GetDlgItem(dialog, ID_TEXT_MANUFACTURER),
441
device->manufacturer);
442
SetWindowText(GetDlgItem(dialog, ID_TEXT_DEV_NAME),
443
device->description);
448
switch(LOWORD(w_param))
451
memset(device, 0, sizeof(*device));
453
GetWindowText(GetDlgItem(dialog, ID_TEXT_MANUFACTURER),
454
device->manufacturer, sizeof(tmp));
455
GetWindowText(GetDlgItem(dialog, ID_TEXT_DEV_NAME),
456
device->description, sizeof(tmp));
458
GetWindowText(GetDlgItem(dialog, ID_TEXT_VID), tmp, sizeof(tmp));
459
sscanf(tmp, "0x%04x", &device->vid);
461
GetWindowText(GetDlgItem(dialog, ID_TEXT_PID), tmp, sizeof(tmp));
462
sscanf(tmp, "0x%04x", &device->pid);
464
if(save_file(dialog, device))
465
EndDialog(dialog, ID_DIALOG_3);
468
EndDialog(dialog, ID_DIALOG_1);
470
case ID_BUTTON_CANCEL:
472
EndDialog(dialog, 0);
480
BOOL CALLBACK dialog_proc_3(HWND dialog, UINT message,
481
WPARAM w_param, LPARAM l_param)
483
static device_context_t *device = NULL;
489
device = (device_context_t *)l_param;
493
"Vendor ID:\t\t 0x%04X\n\n"
494
"Product ID:\t\t 0x%04X\n\n"
495
"Device description:\t %s\n\n"
496
"Manufacturer:\t\t %s\n\n",
501
device->manufacturer);
503
SetWindowText(GetDlgItem(dialog, ID_INFO_TEXT), tmp);
507
switch(LOWORD(w_param))
511
EndDialog(dialog, 0);
519
static void device_list_init(HWND list)
523
ListView_SetExtendedListViewStyle(list, LVS_EX_FULLROWSELECT);
525
memset(&lvc, 0, sizeof(lvc));
527
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT;
528
lvc.fmt = LVCFMT_LEFT;
532
lvc.pszText = "Vendor ID";
533
ListView_InsertColumn(list, 1, &lvc);
537
lvc.pszText = "Product ID";
538
ListView_InsertColumn(list, 2, &lvc);
542
lvc.pszText = "Description";
543
ListView_InsertColumn(list, 3, &lvc);
546
static void device_list_refresh(HWND list)
549
SP_DEVINFO_DATA dev_info_data;
551
device_context_t *device;
554
device_list_clean(list);
556
dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
559
dev_info = SetupDiGetClassDevs(NULL, "USB", NULL,
560
DIGCF_ALLCLASSES | DIGCF_PRESENT);
562
if(dev_info == INVALID_HANDLE_VALUE)
567
while(SetupDiEnumDeviceInfo(dev_info, dev_index, &dev_info_data))
569
if(usb_registry_match(dev_info, &dev_info_data))
571
device = (device_context_t *) malloc(sizeof(device_context_t));
572
memset(device, 0, sizeof(*device));
574
usb_registry_get_property(SPDRP_HARDWAREID, dev_info,
576
tmp, sizeof(tmp) - 1);
578
sscanf(tmp + sizeof("USB\\VID_") - 1, "%04x", &device->vid);
579
sscanf(tmp + sizeof("USB\\VID_XXXX&PID_") - 1, "%04x", &device->pid);
581
usb_registry_get_property(SPDRP_DEVICEDESC, dev_info,
583
tmp, sizeof(tmp) - 1);
584
strcpy(device->description, tmp);
586
usb_registry_get_property(SPDRP_MFG, dev_info,
588
tmp, sizeof(tmp) - 1);
589
strcpy(device->manufacturer, tmp);
591
device_list_add(list, device);
597
SetupDiDestroyDeviceInfoList(dev_info);
600
static void device_list_add(HWND list, device_context_t *device)
606
memset(&item, 0, sizeof(item));
607
memset(vid, 0, sizeof(vid));
608
memset(pid, 0, sizeof(pid));
610
sprintf(vid, "0x%04X", device->vid);
611
sprintf(pid, "0x%04X", device->pid);
613
item.mask = LVIF_TEXT | LVIF_PARAM;
614
item.lParam = (LPARAM)device;
616
ListView_InsertItem(list, &item);
618
ListView_SetItemText(list, 0, 0, vid);
619
ListView_SetItemText(list, 0, 1, pid);
620
ListView_SetItemText(list, 0, 2, device->description);
623
static void device_list_clean(HWND list)
627
memset(&item, 0, sizeof(LVITEM));
629
while(ListView_GetItem(list, &item))
632
free((void *)item.lParam);
634
ListView_DeleteItem(list, 0);
635
memset(&item, 0, sizeof(LVITEM));
639
static int save_file(HWND dialog, device_context_t *device)
641
OPENFILENAME open_file;
642
char inf_name[MAX_PATH];
643
char inf_path[MAX_PATH];
645
char cat_name[MAX_PATH];
646
char cat_path[MAX_PATH];
648
char cat_name_x64[MAX_PATH];
649
char cat_path_x64[MAX_PATH];
651
char error[MAX_PATH];
654
memset(&open_file, 0, sizeof(open_file));
655
strcpy(inf_path, "your_file.inf");
657
open_file.lStructSize = sizeof(OPENFILENAME);
658
open_file.hwndOwner = dialog;
659
open_file.lpstrFile = inf_path;
660
open_file.nMaxFile = sizeof(inf_path);
661
open_file.lpstrFilter = "*.inf\0*.inf\0";
662
open_file.nFilterIndex = 1;
663
open_file.lpstrFileTitle = inf_name;
664
open_file.nMaxFileTitle = sizeof(inf_name);
665
open_file.lpstrInitialDir = NULL;
666
open_file.Flags = OFN_PATHMUSTEXIST;
667
open_file.lpstrDefExt = "inf";
669
if(GetSaveFileName(&open_file))
671
strcpy(cat_path, inf_path);
672
strcpy(cat_name, inf_name);
673
strcpy(cat_path_x64, inf_path);
674
strcpy(cat_name_x64, inf_name);
676
strcpy(strstr(cat_path, ".inf"), ".cat");
677
strcpy(strstr(cat_name, ".inf"), ".cat");
678
strcpy(strstr(cat_path_x64, ".inf"), "_x64.cat");
679
strcpy(strstr(cat_name_x64, ".inf"), "_x64.cat");
681
file = fopen(inf_path, "w");
685
fprintf(file, "%s", inf_header);
686
fprintf(file, "CatalogFile = %s\n", cat_name);
687
fprintf(file, "CatalogFile.NT = %s\n", cat_name);
688
fprintf(file, "CatalogFile.NTAMD64 = %s\n\n", cat_name_x64);
689
fprintf(file, "%s", inf_body);
691
fprintf(file, "[Devices]\n");
692
fprintf(file, "\"%s\"=LIBUSB_DEV, USB\\VID_%04x&PID_%04x\n\n",
694
device->vid, device->pid);
695
fprintf(file, "[Devices.NT]\n");
696
fprintf(file, "\"%s\"=LIBUSB_DEV, USB\\VID_%04x&PID_%04x\n\n",
698
device->vid, device->pid);
699
fprintf(file, "[Devices.NTAMD64]\n");
700
fprintf(file, "\"%s\"=LIBUSB_DEV, USB\\VID_%04x&PID_%04x\n\n",
702
device->vid, device->pid);
704
fprintf(file, strings_header);
705
fprintf(file, "manufacturer = \"%s\"\n", device->manufacturer);
711
sprintf(error, "Error: unable to open file: %s", inf_name);
712
MessageBox(dialog, error, "Error",
713
MB_OK | MB_APPLMODAL | MB_ICONWARNING);
717
file = fopen(cat_path, "w");
721
fprintf(file, "%s", cat_file_content);
726
sprintf(error, "Error: unable to open file: %s", cat_name);
727
MessageBox(dialog, error, "Error",
728
MB_OK | MB_APPLMODAL | MB_ICONWARNING);
731
file = fopen(cat_path_x64, "w");
735
fprintf(file, "%s", cat_file_content);
740
sprintf(error, "Error: unable to open file: %s", cat_name_x64);
741
MessageBox(dialog, error, "Error",
742
MB_OK | MB_APPLMODAL | MB_ICONWARNING);