-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeletePrintDemo2.cpp
More file actions
514 lines (439 loc) · 14.5 KB
/
Copy pathDeletePrintDemo2.cpp
File metadata and controls
514 lines (439 loc) · 14.5 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// DeletePrintDemo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <guiddef.h>
#include <windows.h>
#include <setupapi.h>
#include <vector>
#include <iostream>
#include <strsafe.h>
#include <shlwapi.h>
#include <devguid.h> // for GUID_DEVCLASS_CDROM etc
#include <cfgmgr32.h> // for MAX_DEVICE_ID_LEN, CM_Get_Parent and CM_Get_Device_ID
#define INITGUID
#include <tchar.h>
#include <stdio.h>
using namespace std;
#ifdef DEFINE_DEVPROPKEY
#undef DEFINE_DEVPROPKEY
#endif
#ifdef INITGUID
#define DEFINE_DEVPROPKEY(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8, pid) EXTERN_C const DEVPROPKEY DECLSPEC_SELECTANY name = { { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }, pid }
#else
#define DEFINE_DEVPROPKEY(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8, pid) EXTERN_C const DEVPROPKEY name
#endif // INITGUID
// include DEVPKEY_Device_BusReportedDeviceDesc from WinDDK\7600.16385.1\inc\api\devpkey.h
DEFINE_DEVPROPKEY(DEVPKEY_Device_BusReportedDeviceDesc, 0x540b947e, 0x8b40, 0x45bc, 0xa8, 0xa2, 0x6a, 0x0b, 0x89, 0x4c, 0xbd, 0xa2, 4); // DEVPROP_TYPE_STRING
DEFINE_DEVPROPKEY(DEVPKEY_Device_ContainerId, 0x8c7ed206, 0x3f8a, 0x4827, 0xb3, 0xab, 0xae, 0x9e, 0x1f, 0xae, 0xfc, 0x6c, 2); // DEVPROP_TYPE_GUID
DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 14); // DEVPROP_TYPE_STRING
DEFINE_DEVPROPKEY(DEVPKEY_DeviceDisplay_Category, 0x78c34fc8, 0x104a, 0x4aca, 0x9e, 0xa4, 0x52, 0x4d, 0x52, 0x99, 0x6e, 0x57, 0x5a); // DEVPROP_TYPE_STRING_LIST
DEFINE_DEVPROPKEY(DEVPKEY_Device_LocationInfo, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 15); // DEVPROP_TYPE_STRING
DEFINE_DEVPROPKEY(DEVPKEY_Device_Manufacturer, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 13); // DEVPROP_TYPE_STRING
DEFINE_DEVPROPKEY(DEVPKEY_Device_SecuritySDS, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 26); // DEVPROP_TYPE_SECURITY_DESCRIPTOR_STRING
#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
#pragma comment(lib,"setupapi.lib")
typedef BOOL(WINAPI *FN_SetupDiGetDevicePropertyW)(
__in HDEVINFO DeviceInfoSet,
__in PSP_DEVINFO_DATA DeviceInfoData,
__in const DEVPROPKEY *PropertyKey,
__out DEVPROPTYPE *PropertyType,
__out_opt PBYTE PropertyBuffer,
__in DWORD PropertyBufferSize,
__out_opt PDWORD RequiredSize,
__in DWORD Flags
);
FN_SetupDiGetDevicePropertyW fn_SetupDiGetDevicePropertyW = (FN_SetupDiGetDevicePropertyW)
GetProcAddress(GetModuleHandle(TEXT("Setupapi.dll")), "SetupDiGetDevicePropertyW");
//U盘 interface class GUID
GUID IID_CLASS_WCEUSBS ={0xa5dcbf10, 0x6530, 0x11d2, 0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed};
GUID GUID_DEVINTERFACE_USB_DEVICE = {0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED};
GUID GUID_DEVINTERFACE_USB_DEVICE2 = {0x4d36e979, 0xe325, 0x11ce, 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18};
// 打印机信息结构体
typedef struct _PCS_PRINT_INFO
{
DWORD dwPrintIndex; // 打印机在数组中的索引值
WCHAR wcPrintName[MAX_PATH]; // 打印机的名称,网络打印机名称是参照windows的用IP和共享名称组合的
WCHAR wcPrintModel[MAX_PATH]; // 打印机的型号
WCHAR wcTruethPrintName[MAX_PATH]; // 真实的打印机名称,用户获取打印机句柄
WCHAR wcShareIp[MAX_PATH]; // 共享打印机的IP,如果时本地共享打印机,则此IP是本地IP
WCHAR wcShareName[MAX_PATH]; // 共享打印机共享出来的名称
WCHAR wcPortName[MAX_PATH]; // 打印机的端口名
DWORD dwPrintType; // 打印机的类型,参考PCSPRINTTYPE
BOOL bIsDefaultPrint; // 是否为默认打印机
}PCS_PRINT_INFO, *PPCS_PRINT_INFO;
void RemoveDevice(TCHAR* device_name)
{
BOOL bRes = FALSE;
LPGUID pInterfaceGuid = &GUID_DEVINTERFACE_USB_DEVICE;
SP_DEVINFO_DATA devinfo_data = {0};
SP_DEVICE_INTERFACE_DATA inf_data = {0};
PSP_DEVICE_INTERFACE_DETAIL_DATA_W pinf_detail_data = NULL;
unsigned long inf_detail_size = 0;
HDEVINFO hDeviceInfo = SetupDiGetClassDevs( pInterfaceGuid,
NULL,
NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (INVALID_HANDLE_VALUE == hDeviceInfo)
{
return;
}
SP_DEVICE_INTERFACE_DATA spDevInterData;
memset(&spDevInterData, 0x00, sizeof(SP_DEVICE_INTERFACE_DATA));
spDevInterData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
DWORD dwIndex = 0;
while (TRUE)
{
ZeroMemory(&devinfo_data, sizeof(devinfo_data));
devinfo_data.cbSize = sizeof(devinfo_data);
if (!SetupDiEnumDeviceInfo(hDeviceInfo, dwIndex, &devinfo_data))
{
if (GetLastError() == ERROR_NO_MORE_ITEMS)
{
printf("SetupDiEnumDeviceInfo fail err =%d\n",GetLastError());
break;
}
printf("SetupDiEnumDeviceInfo fail err =%d\n",GetLastError());
continue;
}
TCHAR buf[MAX_PATH];
DWORD nSize = 0;
DWORD DataT;
ULONG len = 4096;
WCHAR szBuffer[4096];
CONFIGRET cr = CR_SUCCESS;
/*inf_data.cbSize = sizeof(inf_data);
if ( !SetupDiEnumDeviceInterfaces(hDeviceInfo, &devinfo_data, &GUID_DEVINTERFACE_USB_DEVICE, dwIndex, &inf_data) )
{
printf("SetupDiEnumDeviceInterfaces fail err =%d\n",GetLastError());
break;
}
SetupDiGetDeviceInterfaceDetailW(hDeviceInfo, &inf_data, NULL, 0, &inf_detail_size, NULL);*/
cr = CM_Get_DevNode_Registry_Property(devinfo_data.DevInst,
CM_DRP_HARDWAREID,
NULL,
szBuffer,
&len, 0);
DEVINST devinstNext = 0;
cr = CM_Get_Child(&devinstNext,devinfo_data.DevInst,NULL);
devinfo_data.DevInst = devinstNext;
cr = CM_Get_DevNode_Registry_Property(devinfo_data.DevInst,
CM_DRP_HARDWAREID,
NULL,
szBuffer,
&len, 0);
//cr = CM_Get_Child(&devinfo_data.DevInst,devinfo_data.DevInst,NULL);
/*if (fn_SetupDiGetDevicePropertyW && fn_SetupDiGetDevicePropertyW(hDeviceInfo, &devinfo_data, &DEVPKEY_Device_BusReportedDeviceDesc,
&DataT, (BYTE*)szBuffer, sizeof(szBuffer), &nSize, 0))
{
if (fn_SetupDiGetDevicePropertyW(hDeviceInfo, &devinfo_data, &DEVPKEY_Device_BusReportedDeviceDesc,
&DataT, (BYTE*)szBuffer, sizeof(szBuffer), &nSize, 0))
{
_tprintf(TEXT(" Bus Reported Device Description: \"%ls\"\n"), szBuffer);
}
}*/
if (_tcsicmp(szBuffer , device_name) == 0)
{
if( SetupDiRemoveDevice(hDeviceInfo, &devinfo_data) )
{
printf("success %d\n",GetLastError());
break;
}
else
{
printf("failZZZZZ%d\n",GetLastError());
}
}
dwIndex++;
}
if (!SetupDiDestroyDeviceInfoList(hDeviceInfo))
{
OutputDebugStringW(L"SetupDiDestroyDeviceInfoList Error");
}
}
BOOL SearchDevice(vector<wstring> &vDevicePath)
{
BOOL bRes = FALSE;
LPGUID pInterfaceGuid = &GUID_DEVINTERFACE_USB_DEVICE;
SP_DEVINFO_DATA devinfo_data = {0};
HDEVINFO hDeviceInfo = SetupDiGetClassDevs( pInterfaceGuid,
NULL,
NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (INVALID_HANDLE_VALUE == hDeviceInfo)
{
goto Exit;
}
SP_DEVICE_INTERFACE_DATA spDevInterData;
memset(&spDevInterData, 0x00, sizeof(SP_DEVICE_INTERFACE_DATA));
spDevInterData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
DWORD dwIndex = 0;
while (TRUE)
{
ZeroMemory(&devinfo_data, sizeof(devinfo_data));
devinfo_data.cbSize = sizeof(devinfo_data);
if (!SetupDiEnumDeviceInfo(hDeviceInfo, dwIndex, &devinfo_data))
{
if (GetLastError() == ERROR_NO_MORE_ITEMS)
{
printf("SetupDiEnumDeviceInfo fail err =%d\n",GetLastError());
break;
}
printf("SetupDiEnumDeviceInfo fail err =%d\n",GetLastError());
continue;
}
TCHAR buf[MAX_PATH];
DWORD nSize = 0;
DWORD DataT;
/*if (!SetupDiGetDeviceInstanceId(hDeviceInfo,&devinfo_data,buf,sizeof(buf),&nSize))
{
}
else
{*/
WCHAR szBuffer[4096];
if (fn_SetupDiGetDevicePropertyW && fn_SetupDiGetDevicePropertyW(hDeviceInfo, &devinfo_data, &DEVPKEY_Device_BusReportedDeviceDesc,
&DataT, (BYTE*)szBuffer, sizeof(szBuffer), &nSize, 0)) {
if (fn_SetupDiGetDevicePropertyW(hDeviceInfo, &devinfo_data, &DEVPKEY_Device_BusReportedDeviceDesc,
&DataT, (BYTE*)szBuffer, sizeof(szBuffer), &nSize, 0))
_tprintf(TEXT(" Bus Reported Device Description: \"%ls\"\n"), szBuffer);
}
//}
if (!SetupDiEnumDeviceInterfaces( hDeviceInfo,
&devinfo_data,
pInterfaceGuid,
dwIndex,
&spDevInterData))
{
if (ERROR_NO_MORE_ITEMS == GetLastError())
{
printf("No more interface fail err = %d\n",GetLastError());
}
else
{
printf("SetupDiEnumDeviceInterfaces Error fail err =%d\n",GetLastError());
}
goto Exit;
}
DWORD dwRequiredLength = 0;
if (!SetupDiGetDeviceInterfaceDetail( hDeviceInfo,
&spDevInterData,
NULL,
0,
&dwRequiredLength,
NULL))
{
if (ERROR_INSUFFICIENT_BUFFER != GetLastError())
{
printf("calculate require length fail err =%d\n",GetLastError());
//goto Exit;
}
}
PSP_DEVICE_INTERFACE_DETAIL_DATA pSpDIDetailData;
pSpDIDetailData = NULL;
pSpDIDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwRequiredLength);
if(NULL == pSpDIDetailData)
{
OutputDebugStringW(L"HeapAlloc Memory Failed");
if (!SetupDiDestroyDeviceInfoList(hDeviceInfo))
{
OutputDebugStringW(L"SetupDiDestroyDeviceInfoList Error");
}
goto Exit;
}
pSpDIDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail( hDeviceInfo,
&spDevInterData,
pSpDIDetailData,
dwRequiredLength,
&dwRequiredLength,
NULL))
{
OutputDebugStringW(L"SetupDiGetDeviceInterfaceDetail Error");
goto Exit;
}
char serial_number[1024] = {0};
wstring wcsDevicePath = pSpDIDetailData->DevicePath;
vDevicePath.push_back(wcsDevicePath);
/*if( SetupDiRemoveDevice(hDeviceInfo, &devinfo_data) )
{
printf("success %d\n",GetLastError());
break;
}
else
{
printf("failZZZZZ%d\n",GetLastError());
}*/
if (NULL != pSpDIDetailData)
{
HeapFree(GetProcessHeap(), 0, pSpDIDetailData);
pSpDIDetailData = NULL;
}
dwIndex++;
}
if (!SetupDiDestroyDeviceInfoList(hDeviceInfo))
{
OutputDebugStringW(L"SetupDiDestroyDeviceInfoList Error");
}
bRes = TRUE;
Exit:
return bRes;
}
HANDLE GetPrintHandle(WCHAR *pPrintName, DWORD dwDesiredAccess = PRINTER_ALL_ACCESS)
{
if (NULL == pPrintName)
{
return NULL;
}
HANDLE hPrinter = NULL;
PRINTER_DEFAULTS pd;
ZeroMemory(&pd, sizeof(pd));
pd.DesiredAccess = dwDesiredAccess;
if (!::OpenPrinter(pPrintName, &hPrinter, &pd))
{
return NULL;
}
return hPrinter;
}
void GetPrint()
{
DWORD dwNeeded = 0;
DWORD dwReturned = 0;
TCHAR wcDefaultPrint[MAX_PATH] = {0};
DWORD dwDefaultPrintLen = MAX_PATH;
PCS_PRINT_INFO sPrintInfo = {0};
// 获取本地打印机的个数
(void)EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL,
2, NULL, 0, &dwNeeded, &dwReturned);
LPBYTE lpBuffer = new(std::nothrow) BYTE[dwNeeded];
if (!lpBuffer )
{
}
ZeroMemory(lpBuffer,dwNeeded);
if (!EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS,NULL,
2, lpBuffer, dwNeeded, &dwNeeded, &dwReturned))
{
}
PRINTER_INFO_2* pPrintInfo2 = (PRINTER_INFO_2*)lpBuffer;
printf("dwReturn = %d\n",dwReturned);
HANDLE hPrinter = NULL;
SP_DEVINFO_DATA devinfo_data = {0};
ZeroMemory(&devinfo_data,sizeof(devinfo_data));
devinfo_data.cbSize = sizeof(devinfo_data);
for (UINT nIndex = 0; nIndex < dwReturned; nIndex++)
{
printf("%ls\n",pPrintInfo2->pPrinterName);
//if (_tcsicmp(pPrintInfo2->pPrinterName,L"EPSON WF-M1030 Series") == 0)
if (TRUE)
{
WCHAR wcHardIdKey[MAX_PATH] = {0}; // HardId路径
DWORD dwType = REG_SZ;
WCHAR wszHardId[MAX_PATH] = { 0 };
DWORD dwDataLen = sizeof(wszHardId);
WCHAR szSubKey[MAX_PATH] = L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print";
HKEY key = NULL;
_snwprintf_s(wcHardIdKey, _countof(wcHardIdKey)-1, _TRUNCATE, _T("%s\\Printers\\%s\\PnPData"), szSubKey,pPrintInfo2->pPrinterName);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, wcHardIdKey,
0L, KEY_READ, &key) == ERROR_SUCCESS) {
if (RegQueryValueEx(key, L"HardwareID", NULL, &dwType, (LPBYTE)wszHardId, &dwDataLen) ==
ERROR_SUCCESS) {
//key_found = true;
}
RegCloseKey(key);
}
else
{
printf("err = %d\n",RegOpenKeyEx(HKEY_LOCAL_MACHINE, wcHardIdKey,0L, KEY_QUERY_VALUE, &key));
}
RemoveDevice(wszHardId);
}
pPrintInfo2++;
}
}
void GetDevicePropertiesCfgmgr32()
{
CONFIGRET cr = CR_SUCCESS;
PWSTR DeviceList = NULL;
ULONG DeviceListLength = 0;
PWSTR CurrentDevice;
DEVINST Devinst;
WCHAR DeviceDesc[2048];
DEVPROPTYPE PropertyType;
ULONG PropertySize;
DWORD Index = 0;
cr = CM_Get_Device_ID_List_Size(&DeviceListLength,
NULL,
CM_GETIDLIST_FILTER_PRESENT);
if (cr != CR_SUCCESS)
{
goto Exit;
}
DeviceList = (PWSTR)HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
DeviceListLength * sizeof(WCHAR));
if (DeviceList == NULL) {
goto Exit;
}
cr = CM_Get_Device_ID_List(L"{4d36e979-e325-11ce-bfc1-08002be10318}",
DeviceList,
16384,
CM_GETIDLIST_FILTER_CLASS);
if (cr != CR_SUCCESS)
{
goto Exit;
}
for (CurrentDevice = DeviceList;
*CurrentDevice;
CurrentDevice += wcslen(CurrentDevice) + 1)
{
// If the list of devices also includes non-present devices,
// CM_LOCATE_DEVNODE_PHANTOM should be used in place of
// CM_LOCATE_DEVNODE_NORMAL.
cr = CM_Locate_DevNode(&Devinst,
CurrentDevice,
CM_LOCATE_DEVNODE_NORMAL);
if (cr != CR_SUCCESS)
{
goto Exit;
}
// Query a property on the device. For example, the device description.
PropertySize = sizeof(DeviceDesc);
/*cr = CM_Get_DevNode_Property(Devinst,
&DEVPKEY_Device_DeviceDesc,
&PropertyType,
(PBYTE)DeviceDesc,
&PropertySize,
0);
*/
if (cr != CR_SUCCESS)
{
Index++;
continue;
}
/*if (PropertyType != DEVPROP_TYPE_STRING)
{
Index++;
continue;
}*/
Index++;
}
Exit:
if (DeviceList != NULL)
{
HeapFree(GetProcessHeap(),
0,
DeviceList);
}
return;
}
int _tmain(int argc, _TCHAR* argv[])
{
GetPrint();
/*vector<wstring> vDevicePath;
SearchDevice(vDevicePath);
vector<wstring>::iterator iter;
for (iter = vDevicePath.begin(); iter != vDevicePath.end(); ++iter)
{
wcout << (*iter).c_str() << endl;
}*/
system("pause");
return 0;
}