Debian Codegraph @

usb/ArgyllCMS_uninstall_USB.c

raw symbols SCIP
1
2 /*
3 * Argyll Color Management System
4 *
5 * Author: Graeme W. Gill
6 * Date: 2023/11/16
7 *
8 * Copyright 2023 Graeme W. Gill
9 * All rights reserved.
10 *
11 * This material is licenced under the GNU GENERAL PUBLIC LICENSE Version 2 or later :-
12 * see the License2.txt file for licencing details.
13 */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <stdarg.h>
18 #include <fcntl.h>
19 #include <string.h>
20 #include "copyright.h"
21 #include "aconfig.h"
22
23 #include "windows.h"
24 #include "setupapi.h"
25
26 #ifdef SUOI_FORCEDELETE /* If SDK doesn't have declaration... */
27
28 static void loadSDK() {
29 }
30
31 #else /* If SDK doesn't have declaration... */
32
33 # define SUOI_FORCEDELETE 0x00000001
34
35 BOOL (WINAPI *SetupUninstallOEMInfA)(PCSTR FileName, DWORD Flags, PVOID Reserved) = NULL;
36
37 static void loadSDK() {
38 HMODULE dllh = NULL;
39
40 if ((dllh = LoadLibraryA("Setupapi.dll")) == NULL)
41 return;
42
43 *(FARPROC*)&SetupUninstallOEMInfA = GetProcAddress(dllh, "SetupUninstallOEMInfA");
44 }
45
46 #endif
47
48 void error(char *fmt, ...), warning(char *fmt, ...);
49
50 void usage(void) {
51 fprintf(stderr,"Uninstall ArgyllCMS libusb-win32 USB drivers%s\n",ARGYLL_VERSION_STR);
52 fprintf(stderr,"Author: Graeme W. Gill\n");
53 fprintf(stderr,"usage: ArgyllCMS_uninstall_USB [-v level]\n");
54 fprintf(stderr," -v level Verbose level 0-2 (default 1)\n");
55 exit(1);
56 }
57
58 int
59 main(int argc, char *argv[]) {
60 int fa,nfa; /* argument we're looking at */
61 int verb = 1; /* Default = none */
62 int rv = 0;
63
64 if (argc < 1)
65 usage();
66
67 /* Process the arguments */
68 for(fa = 1;fa < argc;fa++) {
69 nfa = fa; /* skip to nfa if next argument is used */
70 if (argv[fa][0] == '-') { /* Look for any flags */
71 char *na = NULL; /* next argument after flag, null if none */
72
73 if (argv[fa][2] != '\000')
74 na = &argv[fa][2]; /* next is directly after flag */
75 else {
76 if ((fa+1) < argc) {
77 if (argv[fa+1][0] != '-') {
78 nfa = fa + 1;
79 na = argv[nfa]; /* next is seperate non-flag argument */
80 }
81 }
82 }
83
84 if (argv[fa][1] == '?')
85 usage();
86
87 /* Verbosity */
88 else if (argv[fa][1] == 'v') {
89 fa = nfa;
90 if (na == NULL) usage();
91 verb = atoi(na);
92 }
93
94 else
95 usage();
96 }
97 else
98 break;
99 }
100
101 if (verb >= 1)
102 printf("Un-installing ArgyllCMS libusb0 instruments and drivers:\n");
103
104 /* Un-install Devices */
105 {
106 // ClassGuid {817cffe0-328b-11df-9b9f-0002a5d5c51b}
107 GUID guid = { 0x817cffe0, 0x328b, 0x11df, { 0x9b, 0x9f, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
108 HDEVINFO dinfo;
109 SP_DEVINFO_DATA infodata = { 0 } ;
110 int index = 0;
111
112 infodata.cbSize = sizeof(SP_DEVINFO_DATA);
113
114 if ((dinfo = SetupDiGetClassDevsA(&guid, "USB", NULL, 0)) == NULL) {
115 error("Didn't find any ArgyllCM libusb0 devices (Error 0x%x)",GetLastError());
116 }
117
118 if (verb >= 2) printf("Un-installing Devices\n");
119
120 index = 0;
121 while (SetupDiEnumDeviceInfo(
122 dinfo,
123 index,
124 &infodata)) {
125 int dvix = 0;
126
127 if (verb >= 2)
128 printf("Un-installing device %d, GUID = %08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
129 index+1,
130 infodata.ClassGuid.Data1,
131 infodata.ClassGuid.Data2,
132 infodata.ClassGuid.Data3,
133 infodata.ClassGuid.Data4[0],
134 infodata.ClassGuid.Data4[1],
135 infodata.ClassGuid.Data4[2],
136 infodata.ClassGuid.Data4[3],
137 infodata.ClassGuid.Data4[4],
138 infodata.ClassGuid.Data4[5],
139 infodata.ClassGuid.Data4[6],
140 infodata.ClassGuid.Data4[7]);
141
142 if (!SetupDiCallClassInstaller(DIF_REMOVE, dinfo, &infodata)) {
143 if (GetLastError() == 0xe0000235)
144 error("Un-installing Device '%d' failed because a 32 bit un-installer won't "
145 "work on a 64 bit system.",index+1);
146 error("Un-installing Device %d failed (Error 0x%x)",index+1, GetLastError());
147 }
148 index++;
149 }
150 SetupDiDestroyDeviceInfoList(dinfo);
151 }
152
153 /* Un-install Driver */
154 {
155 // ClassGuid {817cffe0-328b-11df-9b9f-0002a5d5c51b}
156 GUID guid = { 0x817cffe0, 0x328b, 0x11df, { 0x9b, 0x9f, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
157 HDEVINFO dinfo;
158 SP_DRVINFO_DATA_A driverinfo = { 0 };
159 SP_DRVINFO_DETAIL_DATA_A driverdetail = { 0 };
160 CHAR InfName[MAX_PATH], *p;
161 DWORD dsize = 0;
162 int dvix = 0;
163
164 driverinfo.cbSize = sizeof(SP_DRVINFO_DATA_A);
165 driverdetail.cbSize = sizeof(SP_DRVINFO_DETAIL_DATA_A);
166
167 if ((dinfo = SetupDiCreateDeviceInfoList(&guid, NULL)) == INVALID_HANDLE_VALUE) {
168 error("Creating Device Info failed (Error 0x%x)",GetLastError());
169 }
170
171 if (!SetupDiBuildDriverInfoList(dinfo, NULL, SPDIT_CLASSDRIVER)) {
172 error("Failed to locate ArgyllCM libusb0 Driver class (Error 0x%x)",GetLastError());
173 }
174
175 /* Find first device that uses this driver */
176 if (!SetupDiEnumDriverInfoA(
177 dinfo,
178 NULL,
179 SPDIT_CLASSDRIVER,
180 0,
181 &driverinfo)) {
182 error("Didn't find any Devices that use the ArgyllCMS driver (Error 0x%x)\n",GetLastError());
183 }
184
185 if (!SetupDiGetDriverInfoDetailA(dinfo, NULL, &driverinfo, &driverdetail,
186 driverdetail.cbSize, &dsize) && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
187 error("Retrieving detailed Driver information failed (Error 0x%x)\n",GetLastError());
188 }
189
190 if (strlen(driverdetail.InfFileName) > 5
191 && (p = strrchr(driverdetail.InfFileName,'\\')) != NULL) {
192 strcpy(InfName, p+1);
193
194 loadSDK();
195
196 if (SetupUninstallOEMInfA == NULL)
197 error("Couldn't find SetupUninstallOEMInfA API");
198
199 if (verb >= 1) printf("Un-installing Driver '%s' InfFile '%s'\n",
200 driverinfo.ProviderName,InfName);
201
202 if (!SetupUninstallOEMInfA(InfName, SUOI_FORCEDELETE, NULL)) {
203 if (GetLastError() == 0xe0000235)
204 error("Un-install driver '%s' failed because a 32 bit un-installer won't "
205 "work on a 64 bit system.",driverinfo.ProviderName);
206 error("Un-install driver '%s' failed with (Error 0x%x)",
207 driverinfo.ProviderName,GetLastError());
208 }
209 } else {
210 error("Driver '%s' InfPath '%s' is invalid\n",
211 driverinfo.ProviderName,driverdetail.InfFileName);
212 }
213 SetupDiDestroyDeviceInfoList(dinfo);
214 }
215
216 if (verb >= 1)
217 printf("Un-install success!\n");
218
219 Sleep(4000); /* Allow message to be read */
220 return 0;
221 }
222
223
224 /* Basic printf type error() and warning() routines */
225
226 void
227 error(char *fmt, ...)
228 {
229 va_list args;
230
231 fprintf(stderr," ");
232 va_start(args, fmt);
233 vfprintf(stderr, fmt, args);
234 va_end(args);
235 fprintf(stderr, "\n");
236 exit (-1);
237 }
238
239 void
240 warning(char *fmt, ...)
241 {
242 va_list args;
243
244 fprintf(stderr,"Warning - ");
245 va_start(args, fmt);
246 vfprintf(stderr, fmt, args);
247 va_end(args);
248 fprintf(stderr, "\n");
249 }
250