TNB Library
TnbUsbHidPathName.h
[詳解]
1#pragma once
11#include "TnbStr.h"
12#include "TnbRegistryAccessor.h"
13#include "TnbWindowsVersion.h"
14
15#ifndef _TnbDOXYGEN //Document作成用シンボル
16
17#pragma pack(push,4)
18
19// HIDUSAGE.H より
20#ifndef __HIDUSAGE_H__
21
22 typedef USHORT USAGE, *PUSAGE;
23
24#endif // __HIDUSAGE_H__
25
26// HIDPI.H より
27#ifndef __HIDPI_H__
28
29 typedef enum _HIDP_REPORT_TYPE
30 {
31 HidP_Input,
32 HidP_Output,
33 HidP_Feature
34 } HIDP_REPORT_TYPE;
35
36 typedef struct _HIDP_CAPS
37 {
38 USAGE Usage;
39 USAGE UsagePage;
40 USHORT InputReportByteLength;
41 USHORT OutputReportByteLength;
42 USHORT FeatureReportByteLength;
43 USHORT Reserved[17];
44
45 USHORT NumberLinkCollectionNodes;
46
47 USHORT NumberInputButtonCaps;
48 USHORT NumberInputValueCaps;
49 USHORT NumberInputDataIndices;
50
51 USHORT NumberOutputButtonCaps;
52 USHORT NumberOutputValueCaps;
53 USHORT NumberOutputDataIndices;
54
55 USHORT NumberFeatureButtonCaps;
56 USHORT NumberFeatureValueCaps;
57 USHORT NumberFeatureDataIndices;
58 } HIDP_CAPS, *PHIDP_CAPS;
59
60 typedef struct _HIDP_LINK_COLLECTION_NODE
61 {
62 USAGE LinkUsage;
63 USAGE LinkUsagePage;
64 USHORT Parent;
65 USHORT NumberOfChildren;
66 USHORT NextSibling;
67 USHORT FirstChild;
68 ULONG CollectionType: 8; // As defined in 6.2.2.6 of HID spec
69 ULONG IsAlias : 1; // This link node is an allias of the next link node.
70 ULONG Reserved: 23;
71 PVOID UserContext; // The user can hang his coat here.
72 } HIDP_LINK_COLLECTION_NODE, *PHIDP_LINK_COLLECTION_NODE;
73
74 typedef struct _HIDP_BUTTON_CAPS
75 {
76 USAGE UsagePage;
77 UCHAR ReportID;
78 BOOLEAN IsAlias;
79
80 USHORT BitField;
81 USHORT LinkCollection; // A unique internal index pointer
82
83 USAGE LinkUsage;
84 USAGE LinkUsagePage;
85
86 BOOLEAN IsRange;
87 BOOLEAN IsStringRange;
88 BOOLEAN IsDesignatorRange;
89 BOOLEAN IsAbsolute;
90
91 ULONG Reserved[10];
92 union {
93 struct {
94 USAGE UsageMin, UsageMax;
95 USHORT StringMin, StringMax;
96 USHORT DesignatorMin, DesignatorMax;
97 USHORT DataIndexMin, DataIndexMax;
98 } Range;
99 struct {
100 USAGE Usage, Reserved1;
101 USHORT StringIndex, Reserved2;
102 USHORT DesignatorIndex, Reserved3;
103 USHORT DataIndex, Reserved4;
104 } NotRange;
105 };
106
107 } HIDP_BUTTON_CAPS, *PHIDP_BUTTON_CAPS;
108
109 typedef struct _HIDP_VALUE_CAPS
110 {
111 USAGE UsagePage;
112 UCHAR ReportID;
113 BOOLEAN IsAlias;
114
115 USHORT BitField;
116 USHORT LinkCollection; // A unique internal index pointer
117
118 USAGE LinkUsage;
119 USAGE LinkUsagePage;
120
121 BOOLEAN IsRange;
122 BOOLEAN IsStringRange;
123 BOOLEAN IsDesignatorRange;
124 BOOLEAN IsAbsolute;
125
126 BOOLEAN HasNull; // Does this channel have a null report union
127 UCHAR Reserved;
128 USHORT BitSize; // How many bits are devoted to this value?
129
130 USHORT ReportCount; // See Note below. Usually set to 1.
131 USHORT Reserved2[5];
132
133 ULONG UnitsExp;
134 ULONG Units;
135
136 LONG LogicalMin, LogicalMax;
137 LONG PhysicalMin, PhysicalMax;
138
139 union {
140 struct {
141 USAGE UsageMin, UsageMax;
142 USHORT StringMin, StringMax;
143 USHORT DesignatorMin, DesignatorMax;
144 USHORT DataIndexMin, DataIndexMax;
145 } Range;
146
147 struct {
148 USAGE Usage, Reserved1;
149 USHORT StringIndex, Reserved2;
150 USHORT DesignatorIndex, Reserved3;
151 USHORT DataIndex, Reserved4;
152 } NotRange;
153 };
154 } HIDP_VALUE_CAPS, *PHIDP_VALUE_CAPS;
155
156 typedef PUCHAR PHIDP_REPORT_DESCRIPTOR;
157 typedef struct _HIDP_PREPARSED_DATA * PHIDP_PREPARSED_DATA;
158 #ifndef FACILITY_HID_ERROR_CODE
159 #define FACILITY_HID_ERROR_CODE 0x11
160 #endif
161 #define HIDP_ERROR_CODES(SEV, CODE) \
162 ((NTSTATUS) (((SEV) << 28) | (FACILITY_HID_ERROR_CODE << 16) | (CODE)))
163 #define HIDP_STATUS_SUCCESS (HIDP_ERROR_CODES(0x0,0))
164
165#endif // __HIDPI_H__
166
167//HIDSDI.h より
168#ifndef _HIDSDI_H
169
170 typedef struct _HIDD_ATTRIBUTES
171 {
172 ULONG Size; // = sizeof (struct _HIDD_ATTRIBUTES)
173 // Vendor ids of this hid device
174 USHORT VendorID;
175 USHORT ProductID;
176 USHORT VersionNumber;
177 // Additional fields will be added to the end of this structure.
178 } HIDD_ATTRIBUTES, *PHIDD_ATTRIBUTES;
179 typedef LONG NTSTATUS;
180
181#endif // _HIDSDI_H
182
183#pragma pack(pop)
184
185#endif // _TnbDOXYGEN
186
187
188
189//TNB Library
190namespace TNB
191{
192
193
194
211{
212public:
213
215 CUsbHidApi(void) : m_module(NULL)
216 {
217 }
218
224 CUsbHidApi(const CUsbHidApi& other) : m_module(NULL)
225 {
226 }
227
230 {
231 if ( m_module != NULL )
232 {
233 ::FreeLibrary(m_module);
234 m_module = NULL;
235 }
236 }
237
245 {
246 return *this;
247 }
248
256 BOOLEAN HidD_GetAttributes(HANDLE hidDeviceObject, PHIDD_ATTRIBUTES pAttributes) const
257 {
258 BOOLEAN (__stdcall *P)(HANDLE hidDeviceObject, PHIDD_ATTRIBUTES pAttributes) = 0;
259 return Func(P, "HidD_GetAttributes")(hidDeviceObject, pAttributes);
260 }
261
270 BOOLEAN HidD_GetPreparsedData(HANDLE hidDeviceObject, PHIDP_PREPARSED_DATA* ppPreparsedData) const
271 {
272 BOOLEAN (__stdcall *P)(HANDLE hidDeviceObject, PHIDP_PREPARSED_DATA* ppPreparsedData) = 0;
273 return Func(P, "HidD_GetPreparsedData")(hidDeviceObject, ppPreparsedData);
274 }
275
283 BOOLEAN HidD_FreePreparsedData(PHIDP_PREPARSED_DATA pPreparsedData) const
284 {
285 BOOLEAN (__stdcall *P)(PHIDP_PREPARSED_DATA pPreparsedData) = 0;
286 return Func(P, "HidD_FreePreparsedData")(pPreparsedData);
287 }
288
297 BOOLEAN HidD_SetFeature(HANDLE hidDeviceObject,PVOID pReportBuffer, ULONG reportBufferLength)
298 {
299 BOOLEAN (__stdcall *P)(HANDLE hidDeviceObject,PVOID pReportBuffer, ULONG reportBufferLength) = 0;
300 return Func(P, "HidD_SetFeature")(hidDeviceObject, pReportBuffer, reportBufferLength);
301 }
302
311 BOOLEAN HidD_GetFeature(HANDLE hidDeviceObject, PVOID pReportBuffer, ULONG reportBufferLength) const
312 {
313 BOOLEAN (__stdcall *P)(HANDLE hidDeviceObject, PVOID pReportBuffer, ULONG reportBufferLength) = 0;
314 return Func(P, "HidD_GetFeature")(hidDeviceObject, pReportBuffer, reportBufferLength);
315 }
316
325 BOOLEAN HidD_SetOutputReport(HANDLE hidDeviceObject, PVOID pReportBuffer, ULONG reportBufferLength)
326 {
327 BOOLEAN (__stdcall *P)(HANDLE hidDeviceObject, PVOID pReportBuffer, ULONG reportBufferLength) = 0;
328 return Func(P, "HidD_SetOutputReport")(hidDeviceObject, pReportBuffer, reportBufferLength);
329 }
330
339 BOOLEAN HidD_GetInputReport(HANDLE hidDeviceObject, PVOID pReportBuffer, ULONG reportBufferLength) const
340 {
341 BOOLEAN (__stdcall *P)(HANDLE hidDeviceObject, PVOID pReportBuffer, ULONG reportBufferLength) = 0;
342 return Func(P, "HidD_GetInputReport")(hidDeviceObject, pReportBuffer, reportBufferLength);
343 }
344
349 void HidD_GetHidGuid(LPGUID pHidGuid) const
350 {
351 void (__stdcall *P)(LPGUID pHidGuid) = NULL;
352 Func(P, "HidD_GetHidGuid")(pHidGuid);
353 }
354
363 BOOLEAN HidD_GetManufacturerString(HANDLE hidDeviceObject, PVOID pBuffer, ULONG bufferLength) const
364 {
365 BOOLEAN (__stdcall *P)(HANDLE hidDeviceObject, PVOID pBuffer, ULONG bufferLength) = 0;
366 return Func(P, "HidD_GetManufacturerString")(hidDeviceObject, pBuffer, bufferLength);
367 }
368
377 BOOLEAN HidD_GetProductString(HANDLE hidDeviceObject, PVOID pBuffer, ULONG bufferLength) const
378 {
379 BOOLEAN (__stdcall *P)(HANDLE hidDeviceObject, PVOID pBuffer, ULONG bufferLength) = 0;
380 return Func(P, "HidD_GetProductString")(hidDeviceObject, pBuffer, bufferLength);
381 }
382
391 BOOLEAN HidD_GetSerialNumberString(HANDLE hidDeviceObject, PVOID pBuffer, ULONG bufferLength) const
392 {
393 BOOLEAN (__stdcall *P)(HANDLE hidDeviceObject, PVOID pBuffer, ULONG bufferLength) = 0;
394 return Func(P, "HidD_GetSerialNumberString")(hidDeviceObject, pBuffer, bufferLength);
395 }
396
406 BOOLEAN HidD_GetIndexedString(HANDLE hidDeviceObject, ULONG stringIndex, PVOID pBuffer, ULONG bufferLength) const
407 {
408 BOOLEAN (__stdcall *P)(HANDLE hidDeviceObject, ULONG stringIndex, PVOID pBuffer, ULONG bufferLength) = 0;
409 return Func(P, "HidD_GetIndexedString")(hidDeviceObject, stringIndex, pBuffer, bufferLength);
410 }
411
419 BOOLEAN HidD_GetNumInputBuffers(HANDLE hidDeviceObject, PULONG pNumberBuffers) const
420 {
421 BOOLEAN (__stdcall *P)(HANDLE hidDeviceObject, PULONG pNumberBuffers) = 0;
422 return Func(P, "HidD_GetNumInputBuffers")(hidDeviceObject, pNumberBuffers);
423 }
424
432 BOOLEAN HidD_SetNumInputBuffers(HANDLE hidDeviceObject, ULONG numberBuffers)
433 {
434 BOOLEAN (__stdcall *P)(HANDLE hidDeviceObject, ULONG numberBuffers) = 0;
435 return Func(P, "HidD_SetNumInputBuffers")(hidDeviceObject, numberBuffers);
436 }
437
444 BOOLEAN HidD_FlushQueue(HANDLE hidDeviceObject)
445 {
446 BOOLEAN (__stdcall *P)(HANDLE hidDeviceObject) = 0;
447 return Func(P, "HidD_FlushQueue")(hidDeviceObject);
448 }
449
457 NTSTATUS HidP_GetCaps(PHIDP_PREPARSED_DATA pPreparsedData, PHIDP_CAPS pCapabilities) const
458 {
459 NTSTATUS (__stdcall *P)(PHIDP_PREPARSED_DATA pPreparsedData, PHIDP_CAPS pCapabilities) = 0;
460 return Func(P, "HidP_GetCaps")(pPreparsedData, pCapabilities);
461 }
462
471 NTSTATUS HidP_GetLinkCollectionNodes(PHIDP_LINK_COLLECTION_NODE pLinkCollectionNodes, PULONG pLinkCollectionNodesLength, PHIDP_PREPARSED_DATA pPreparsedData)
472 {
473 NTSTATUS (__stdcall *P)(PHIDP_LINK_COLLECTION_NODE pLinkCollectionNodes, PULONG pLinkCollectionNodesLength, PHIDP_PREPARSED_DATA pPreparsedData) = 0;
474 return Func(P, "HidP_GetLinkCollectionNodes")(pLinkCollectionNodes, pLinkCollectionNodesLength, pPreparsedData);
475 }
476
489 NTSTATUS HidP_GetSpecificButtonCaps(HIDP_REPORT_TYPE reportType, USAGE usagePage, USHORT linkCollection, USAGE usage, PHIDP_BUTTON_CAPS pButtonCaps, PUSHORT pButtonCapsLength, PHIDP_PREPARSED_DATA pPreparsedData)
490 {
491 NTSTATUS (__stdcall *P)(HIDP_REPORT_TYPE reportType, USAGE usagePage, USHORT linkCollection, USAGE usage, PHIDP_BUTTON_CAPS pButtonCaps, PUSHORT pButtonCapsLength, PHIDP_PREPARSED_DATA pPreparsedData) = 0;
492 return Func(P, "HidP_GetSpecificButtonCaps")(reportType, usagePage, linkCollection, usage, pButtonCaps, pButtonCapsLength, pPreparsedData);
493 }
494
504 NTSTATUS HidP_GetButtonCaps(HIDP_REPORT_TYPE reportType, PHIDP_BUTTON_CAPS pButtonCaps, PUSHORT pButtonCapsLength, PHIDP_PREPARSED_DATA pPreparsedData)
505 {
506 return HidP_GetSpecificButtonCaps(reportType, 0, 0, 0, pButtonCaps, pButtonCapsLength, pPreparsedData);
507 }
508
521 NTSTATUS HidP_GetSpecificValueCaps(HIDP_REPORT_TYPE reportType, USAGE usagePage, USHORT linkCollection, USAGE usage, PHIDP_VALUE_CAPS pValueCaps, PUSHORT pValueCapsLength, PHIDP_PREPARSED_DATA pPreparsedData)
522 {
523 NTSTATUS (__stdcall *P)(HIDP_REPORT_TYPE reportType, USAGE usagePage, USHORT linkCollection, USAGE usage, PHIDP_VALUE_CAPS pValueCaps, PUSHORT pValueCapsLength, PHIDP_PREPARSED_DATA pPreparsedData) = 0;
524 return Func(P, "HidP_GetSpecificValueCaps")(reportType, usagePage, linkCollection, usage, pValueCaps, pValueCapsLength, pPreparsedData);
525 }
526
536 NTSTATUS HidP_GetValueCaps(HIDP_REPORT_TYPE reportType, PHIDP_VALUE_CAPS pValueCaps, PUSHORT pValueCapsLength, PHIDP_PREPARSED_DATA pPreparsedData)
537 {
538 return HidP_GetSpecificValueCaps(reportType, 0, 0, 0, pValueCaps, pValueCapsLength, pPreparsedData);
539 }
540
541private:
542
543 // EXPORT関数コール
544 template<typename TYP>
545 TYP Func(TYP typ, LPCSTR lpsz) const
546 {
547 if ( m_module == NULL )
548 {
549 m_module = ::LoadLibrary(_T("hid.dll"));
550 ASSERT( m_module != NULL );
551 }
552 ForceSet(typ, ::GetProcAddress(m_module, lpsz));
553 ASSERT1( typ != NULL, "CUsbHidApi::Func", "[%s]が見つかりませんでした", lpsz );
554 return typ;
555 }
556
557 mutable HMODULE m_module;
558};
559
560
561
585{
586public:
587
592 CUsbHidPathName(void) : m_vendorId(0), m_productId(0), m_pathName()
593 {
594 Zero(m_hidpCaps);
595 }
596
605 CUsbHidPathName(WORD wVid, WORD wPid, LPCTSTR lpszPathName, const HIDP_CAPS& cap)
606 : m_vendorId(wVid), m_productId(wPid), m_pathName(lpszPathName), m_hidpCaps(cap)
607 {
608 }
609
615 bool IsEmpty(void) const
616 {
617 return m_pathName.IsEmpty();
618// return m_vendorId != 0;
619 }
620
625 void Empty(void)
626 {
627 m_vendorId = 0;
628 m_productId = 0;
629 m_pathName.Empty();
630 m_regPathHid.Empty();
631 m_regPathUsb.Empty();
632 Zero(m_hidpCaps);
633 }
634
644 DWORD SetPathName(LPCTSTR lpszPathName)
645 {
646 Empty();
647 DWORD shareMode = (FILE_SHARE_READ | FILE_SHARE_WRITE);
648 HANDLE h = ::CreateFile(lpszPathName, GENERIC_WRITE, shareMode, NULL, OPEN_EXISTING, 0, 0);
649 if ( h == INVALID_HANDLE_VALUE )
650 {
651 return _GetLastError("CreateFile");
652 }
653 DWORD dwResult = ERROR_SUCCESS;
654 HIDD_ATTRIBUTES attr;
655 attr.Size = sizeof(HIDD_ATTRIBUTES);
656 // Vendor IDとProduct IDを取得
657 if ( ! HidD_GetAttributes(h, &attr) )
658 {
659 dwResult = _GetLastError("HidD_GetAttributes");
660 }
661 else
662 {
663 PHIDP_PREPARSED_DATA pPreparsedData;
664 if ( ! HidD_GetPreparsedData(h, &pPreparsedData) )
665 {
666 dwResult = _GetLastError("HidD_GetPreparsedData");
667 }
668 else
669 {
670 HidP_GetCaps(pPreparsedData, &m_hidpCaps);
671 HidD_FreePreparsedData(pPreparsedData);
672 m_vendorId = attr.VendorID;
673 m_productId = attr.ProductID;
674 m_pathName = lpszPathName;
675 m_regPathHid.Empty();
676 m_regPathUsb.Empty();
677// TRACE3("desc=[%s], loca=[%s], mfg=[%s]\n", GetDeviceDescription(), GetLocationInformation(), GetManufacturer());
678 }
679 }
680 ::CloseHandle(h);
681 return dwResult;
682 }
683
691 {
692 _vn.Resize(m_hidpCaps.NumberLinkCollectionNodes);
693 DWORD shareMode = (FILE_SHARE_READ | FILE_SHARE_WRITE);
694 HANDLE h = ::CreateFile(m_pathName, GENERIC_WRITE, shareMode, NULL, OPEN_EXISTING, 0, 0);
695 if ( h == INVALID_HANDLE_VALUE )
696 {
697 _GetLastError("CreateFile");
698 return false;
699 }
700 bool r = false;
701 PHIDP_PREPARSED_DATA pPreparsedData;
702 if ( HidD_GetPreparsedData(h, &pPreparsedData) )
703 {
704 ULONG ul = static_cast<ULONG>(_vn.GetSize());
705 r = (HidP_GetLinkCollectionNodes(_vn.Ref(), &ul, pPreparsedData) == HIDP_STATUS_SUCCESS);
706 HidD_FreePreparsedData(pPreparsedData);
707 }
708 ::CloseHandle(h);
709 return r;
710 }
711
719 bool GetButtonCaps(CWorkMemT<HIDP_BUTTON_CAPS>& _vn, HIDP_REPORT_TYPE reportType)
720 {
721 switch ( reportType )
722 {
723 case HidP_Input:
724 _vn.Resize(m_hidpCaps.NumberInputButtonCaps);
725 break;
726 case HidP_Output:
727 _vn.Resize(m_hidpCaps.NumberOutputButtonCaps);
728 break;
729 case HidP_Feature:
730 default:
731 _vn.Resize(m_hidpCaps.NumberFeatureButtonCaps);
732 break;
733 }
734 DWORD shareMode = (FILE_SHARE_READ | FILE_SHARE_WRITE);
735 HANDLE h = ::CreateFile(m_pathName, GENERIC_WRITE, shareMode, NULL, OPEN_EXISTING, 0, 0);
736 if ( h == INVALID_HANDLE_VALUE )
737 {
738 _GetLastError("CreateFile");
739 return false;
740 }
741 bool r = false;
742 PHIDP_PREPARSED_DATA pPreparsedData;
743 if ( HidD_GetPreparsedData(h, &pPreparsedData) )
744 {
745 USHORT ul = static_cast<USHORT>(_vn.GetSize());
746 r = (HidP_GetButtonCaps(reportType, _vn.Ref(), &ul, pPreparsedData) == HIDP_STATUS_SUCCESS);
747 HidD_FreePreparsedData(pPreparsedData);
748 }
749 ::CloseHandle(h);
750 return r;
751 }
752
760 bool GetValueCaps(CWorkMemT<HIDP_VALUE_CAPS>& _vn, HIDP_REPORT_TYPE reportType)
761 {
762 switch ( reportType )
763 {
764 case HidP_Input:
765 _vn.Resize(m_hidpCaps.NumberInputValueCaps);
766 break;
767 case HidP_Output:
768 _vn.Resize(m_hidpCaps.NumberOutputValueCaps);
769 break;
770 case HidP_Feature:
771 default:
772 _vn.Resize(m_hidpCaps.NumberFeatureValueCaps);
773 break;
774 }
775 DWORD shareMode = (FILE_SHARE_READ | FILE_SHARE_WRITE);
776 HANDLE h = ::CreateFile(m_pathName, GENERIC_WRITE, shareMode, NULL, OPEN_EXISTING, 0, 0);
777 if ( h == INVALID_HANDLE_VALUE )
778 {
779 _GetLastError("CreateFile");
780 return false;
781 }
782 bool r = false;
783 PHIDP_PREPARSED_DATA pPreparsedData;
784 if ( HidD_GetPreparsedData(h, &pPreparsedData) )
785 {
786 USHORT ul = static_cast<USHORT>(_vn.GetSize());
787 r = (HidP_GetValueCaps(reportType, _vn.Ref(), &ul, pPreparsedData) == HIDP_STATUS_SUCCESS);
788 HidD_FreePreparsedData(pPreparsedData);
789 }
790 ::CloseHandle(h);
791 return r;
792 }
793
798 const HIDP_CAPS& GetCapability(void) const
799 {
800 return m_hidpCaps;
801 }
802
807 const CStr& GetPathName(void) const
808 {
809 return m_pathName;
810 }
811
816 WORD GetVendorId(void) const
817 {
818 return m_vendorId;
819 }
820
825 WORD GetProductId(void) const
826 {
827 return m_productId;
828 }
829
834 size_t GetInputReportByteLength(void) const
835 {
836 return m_hidpCaps.InputReportByteLength;
837 }
838
843 size_t GetOutputReportByteLength(void) const
844 {
845 return m_hidpCaps.OutputReportByteLength;
846 }
847
852 size_t GetFeatureReportByteLength(void) const
853 {
854 return m_hidpCaps.FeatureReportByteLength;
855 }
856
863 {
864 m_SearchRegPath();
865 if ( ! m_regPathHid.IsEmpty() )
866 {
867 CRegistryAccessor reg(HKEY_LOCAL_MACHINE, m_GetBaseRegPath(), KEY_READ);
868 return m_GetRegString(reg, m_regPathHid, _T("Mfg"));
869 }
870 return _T("");
871 }
872
879 {
880 m_SearchRegPath();
881 if ( ! m_regPathHid.IsEmpty() )
882 {
883 CRegistryAccessor reg(HKEY_LOCAL_MACHINE, m_GetBaseRegPath(), KEY_READ);
884 return m_GetRegString(reg, m_regPathHid, _T("DeviceDesc"));
885 }
886 return _T("");
887 }
888
895 {
896 m_SearchRegPath();
897 if ( ! m_regPathUsb.IsEmpty() )
898 {
899 CRegistryAccessor reg(HKEY_LOCAL_MACHINE, m_GetBaseRegPath(), KEY_READ);
900 return m_GetRegString(reg, m_regPathUsb, _T("LocationInformation"));
901 }
902 return _T("");
903 }
904
907 {
911 };
912
921 {
922 _ds.manufacturer.Empty();
923 _ds.product.Empty();
924 _ds.serialNumber.Empty();
925 HANDLE hDown = ::CreateFile(m_pathName, GENERIC_WRITE, (FILE_SHARE_READ | FILE_SHARE_WRITE), NULL, OPEN_EXISTING, 0, 0);
926 if ( hDown == INVALID_HANDLE_VALUE )
927 {
928 return false;
929 }
930 CUnicode s;
931 bool r = !! HidD_GetManufacturerString(hDown, s.GetBuffer(256), 256 * sizeof(WCHAR));
932 s.ReleaseBuffer();
933 if ( r )
934 {
935 _ds.manufacturer = s;
936 }
937 r = !! HidD_GetProductString(hDown, s.GetBuffer(256), 256 * sizeof(WCHAR));
938 s.ReleaseBuffer();
939 if ( r )
940 {
941 _ds.product = s;
942 }
943 r = !! HidD_GetSerialNumberString(hDown, s.GetBuffer(256), 256 * sizeof(WCHAR));
944 s.ReleaseBuffer();
945 if ( r )
946 {
947 _ds.serialNumber = s;
948 }
949 ::CloseHandle(hDown);
950 return true;
951 }
952
958 CStr ToString(void) const
959 {
960 CStr str;
961 USHORT vid = GetVendorId();
962 USHORT pid = GetProductId();
964 if ( GetManufacturerString(ds) )
965 {
966 if ( ds.manufacturer != _T("?") && ! ds.manufacturer.IsEmpty() )
967 {
968 str.Format(_T("VID=0x%04X, PID=0x%04X, [%s] - %s"), vid, pid, ds.product, ds.manufacturer);
969 return str;
970 }
971 }
973 if ( wv.GetMajorVersion() <= 5/*XP,2000*/ )
974 {
975 str.Format(_T("VID=0x%04X, PID=0x%04X [%s]"), vid, pid, GetLocationInformation());
976 }
977 else
978 {
979 str.Format(_T("VID=0x%04X, PID=0x%04X, [%s] - %s"), vid, pid, GetDeviceDescription(), GetManufacturer());
980 }
981 return str;
982 }
983
984private:
986 class CMyFinder : public CDeepAccessFinder
987 {
988 public:
989 CStr m_targetId;
990 CStr m_foundPath;
991 protected:
992 // セクション発見.
993 virtual EResult OnFindSection(LPCTSTR lpszSection, IAccessor::CSection& sec, int depth)
994 {
995 if ( depth >= 2 )
996 {
997 return Previous;
998 }
999 if ( m_targetId.IsEqual(sec.QueryString(_T("ParentIdPrefix"))) )
1000 {
1001 m_foundPath = sec.GetSectionName();
1002 return Stop;
1003 }
1004 return Next;
1005 }
1006 };
1008 bool m_SearchRegPath(void) const
1009 {
1010 if ( m_regPathHid.IsEmpty() || m_regPathUsb.IsEmpty() )
1011 {
1012 INDEX f1 = m_pathName.Find('#');
1013 INDEX f2 = m_pathName.Find('#', f1 + 1);
1014 INDEX f3 = m_pathName.Find('#', f2 + 1);
1015 if ( f1 != INVALID_INDEX && f2 != INVALID_INDEX && f3 != INVALID_INDEX )
1016 {
1017 CMyFinder ff;
1018 CStr vpid = m_pathName.Mid(f1 + 1, f2 - f1 - 1);
1019 CStr vpid2 = vpid;
1020 INDEX f = vpid.Find('&', 18);
1021 if ( f != INVALID_INDEX )
1022 {
1023 vpid2 = vpid.Left(f);
1024 }
1025 CStr key = m_pathName.Mid(f2 + 1, f3 - f2 - 1);
1026 ff.m_targetId = key.Left(key.GetLength() - 5);
1027 CRegistryAccessor reg(HKEY_LOCAL_MACHINE, m_GetBaseRegPath(), KEY_READ);
1028 ff.Execute(reg[_T("USB\\") + vpid2]);
1029 m_regPathUsb = ff.m_foundPath;
1030 CStrVector vs = reg.EnumSectionNames(_T("HID\\") + vpid);
1031 if ( ! vs.IsEmpty() )
1032 {
1033 m_regPathHid.Format(_T("HID\\%s\\%s"), vpid, vs[0]);
1034 }
1035 }
1036 }
1037 return (! m_regPathHid.IsEmpty() && ! m_regPathUsb.IsEmpty());
1038 }
1040 CStr m_GetRegString(CRegistryAccessor& reg, LPCTSTR lpszSection, LPCTSTR lpszKey) const
1041 {
1042 CStr s = reg[lpszSection].QueryString(lpszKey).Trim();
1043 INDEX f = s.Find(';');
1044 if ( f != INVALID_INDEX )
1045 {
1046 s = s.Mid(f + 1);
1047 }
1048 return s;
1049 }
1051 LPCTSTR m_GetBaseRegPath(void) const
1052 {
1053 return _T("System\\currentControlSet\\Enum");
1054 }
1055 WORD m_vendorId;
1056 WORD m_productId;
1057 CStr m_pathName;
1058 HIDP_CAPS m_hidpCaps;
1059 mutable CStr m_regPathHid;
1060 mutable CStr m_regPathUsb;
1061};
1062
1063
1064
1065}; // TNB
レジストリアクセス関係のヘッダ
文字列管理関係のヘッダ
ウィンドウズバージョン関係のヘッダ
ディープ検索クラス
Definition: TnbAccessor.h:1093
レジストリアクセスクラス
bool IsEmpty(void) const
[確認] 空チェック
Definition: TnbStr.h:528
CStrT Left(size_t iSize) const
[作成] 範囲取得.
Definition: TnbStr.h:801
void ReleaseBuffer(void)
[操作] 割り当てたバッファを開放.
Definition: TnbStr.h:954
size_t GetLength(void) const
[取得] 文字列長
Definition: TnbStr.h:518
INT_PTR Find(TYP t, INDEX iFromIndex=0) const
[確認] 検索.
Definition: TnbStr.h:540
bool IsEqual(const TYP *lpszSubject) const
[確認] 文字列比較
Definition: TnbStr.h:669
void Empty(void)
[削除] 空化
Definition: TnbStr.h:197
void Format(const TYP *lpszFormat,...)
[代入] 書式付き文字列代入.
Definition: TnbStr.h:359
CStrT Mid(INDEX iOffset, size_t iSize=INVALID_SIZE) const
[作成] 範囲取得.
Definition: TnbStr.h:766
TYP * GetBuffer(size_t iLength=0)
[操作] 書き込みバッファ要求.
Definition: TnbStr.h:914
hid.dll アクセスクラス
NTSTATUS HidP_GetButtonCaps(HIDP_REPORT_TYPE reportType, PHIDP_BUTTON_CAPS pButtonCaps, PUSHORT pButtonCapsLength, PHIDP_PREPARSED_DATA pPreparsedData)
[取得] ボタン性能取得.
NTSTATUS HidP_GetLinkCollectionNodes(PHIDP_LINK_COLLECTION_NODE pLinkCollectionNodes, PULONG pLinkCollectionNodesLength, PHIDP_PREPARSED_DATA pPreparsedData)
[取得] リンクコレクションノード取得.
BOOLEAN HidD_SetFeature(HANDLE hidDeviceObject, PVOID pReportBuffer, ULONG reportBufferLength)
[送信] 機能レポート送信
~CUsbHidApi(void)
デストラクタ
BOOLEAN HidD_GetAttributes(HANDLE hidDeviceObject, PHIDD_ATTRIBUTES pAttributes) const
[取得] 属性取得.
CUsbHidApi & operator=(const CUsbHidApi &other)
コピーオペレータ
BOOLEAN HidD_SetNumInputBuffers(HANDLE hidDeviceObject, ULONG numberBuffers)
[設定] 入力バッファ数設定.
BOOLEAN HidD_GetFeature(HANDLE hidDeviceObject, PVOID pReportBuffer, ULONG reportBufferLength) const
[受信] 機能レポート受信
BOOLEAN HidD_GetNumInputBuffers(HANDLE hidDeviceObject, PULONG pNumberBuffers) const
[取得] 入力バッファ数取得.
BOOLEAN HidD_SetOutputReport(HANDLE hidDeviceObject, PVOID pReportBuffer, ULONG reportBufferLength)
[送信] Outputレポート送信
NTSTATUS HidP_GetSpecificButtonCaps(HIDP_REPORT_TYPE reportType, USAGE usagePage, USHORT linkCollection, USAGE usage, PHIDP_BUTTON_CAPS pButtonCaps, PUSHORT pButtonCapsLength, PHIDP_PREPARSED_DATA pPreparsedData)
[取得] ボタン詳細性能取得.
CUsbHidApi(void)
コンストラクタ
NTSTATUS HidP_GetSpecificValueCaps(HIDP_REPORT_TYPE reportType, USAGE usagePage, USHORT linkCollection, USAGE usage, PHIDP_VALUE_CAPS pValueCaps, PUSHORT pValueCapsLength, PHIDP_PREPARSED_DATA pPreparsedData)
[取得] 値詳細性能取得.
void HidD_GetHidGuid(LPGUID pHidGuid) const
[取得] HID GUID 取得.
BOOLEAN HidD_GetProductString(HANDLE hidDeviceObject, PVOID pBuffer, ULONG bufferLength) const
[取得] Product 文字列取得
CUsbHidApi(const CUsbHidApi &other)
コピーコンストラクタ.
NTSTATUS HidP_GetCaps(PHIDP_PREPARSED_DATA pPreparsedData, PHIDP_CAPS pCapabilities) const
[取得] 能力取得.
BOOLEAN HidD_GetInputReport(HANDLE hidDeviceObject, PVOID pReportBuffer, ULONG reportBufferLength) const
[受信] Inputレポート受信
BOOLEAN HidD_GetManufacturerString(HANDLE hidDeviceObject, PVOID pBuffer, ULONG bufferLength) const
[取得] Manufacturer 文字列取得
BOOLEAN HidD_GetPreparsedData(HANDLE hidDeviceObject, PHIDP_PREPARSED_DATA *ppPreparsedData) const
[取得] PREPARSEDデータ取得.
BOOLEAN HidD_FlushQueue(HANDLE hidDeviceObject)
[処理] キューフラッシュ.
BOOLEAN HidD_GetIndexedString(HANDLE hidDeviceObject, ULONG stringIndex, PVOID pBuffer, ULONG bufferLength) const
[取得] INDEXED 文字列取得
BOOLEAN HidD_GetSerialNumberString(HANDLE hidDeviceObject, PVOID pBuffer, ULONG bufferLength) const
[取得] SerialNumber 文字列取得
NTSTATUS HidP_GetValueCaps(HIDP_REPORT_TYPE reportType, PHIDP_VALUE_CAPS pValueCaps, PUSHORT pValueCapsLength, PHIDP_PREPARSED_DATA pPreparsedData)
[取得] 値性能取得.
BOOLEAN HidD_FreePreparsedData(PHIDP_PREPARSED_DATA pPreparsedData) const
[開放] PREPARSEDデータ開放.
USB-HIDパスネーム情報管理クラス
CStr GetManufacturer(void) const
[取得] 製造業者取得.
CUsbHidPathName(WORD wVid, WORD wPid, LPCTSTR lpszPathName, const HIDP_CAPS &cap)
コンストラクタ
size_t GetOutputReportByteLength(void) const
[取得] Outputレポート長取得
CStr GetDeviceDescription(void) const
[取得] 説明取得.
bool IsEmpty(void) const
[確認] 情報有無確認
const HIDP_CAPS & GetCapability(void) const
[取得] Capability情報取得
bool GetLinkCollectionNodes(CWorkMemT< HIDP_LINK_COLLECTION_NODE > &_vn)
[設定] リンクコレクションノード情報取得.
bool GetManufacturerString(TDescriptionStrings &_ds) const
[取得] Description 文字列取得.
CUsbHidPathName(void)
コンストラクタ
const CStr & GetPathName(void) const
[取得] パスネーム取得
size_t GetInputReportByteLength(void) const
[取得] Inputレポート長取得
DWORD SetPathName(LPCTSTR lpszPathName)
[設定] パスネーム設定.
WORD GetProductId(void) const
[取得] プロダクトID取得
WORD GetVendorId(void) const
[取得] ベンダID取得
void Empty(void)
[設定] 情報なし
CStr GetLocationInformation(void) const
[取得] 場所情報取得.
CStr ToString(void) const
[取得] 文字列取得.
bool GetValueCaps(CWorkMemT< HIDP_VALUE_CAPS > &_vn, HIDP_REPORT_TYPE reportType)
[設定] 値性能情報取得.
bool GetButtonCaps(CWorkMemT< HIDP_BUTTON_CAPS > &_vn, HIDP_REPORT_TYPE reportType)
[設定] ボタン性能情報取得.
size_t GetFeatureReportByteLength(void) const
[取得] Featureレポート長取得
ウィンドウズバージョン情報
DWORD GetMajorVersion(void) const
[取得] メジャーバージョン取得.
ワークメモリテンプレート.
Definition: TnbDef.h:633
size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbDef.h:665
void Resize(size_t l)
[設定] サイズ再設定
Definition: TnbDef.h:672
const TYP * Ref(void) const
[取得] ポインタ取得
Definition: TnbDef.h:712
セクション情報アクセスクラス
Definition: TnbAccessor.h:294
CStr QueryString(LPCTSTR lpszKey, LPCTSTR lpszDefault=NULL) const
[取得] 文字列情報取得
Definition: TnbAccessor.h:506
CStr GetSectionName(void) const
[取得] セクション名取得
Definition: TnbAccessor.h:335
void Zero(V &value)
[設定] ゼロクリア.
Definition: TnbDef.h:399
TNB Library
Definition: TnbDoxyTitle.txt:2
bool IsEmpty(void) const
[確認] 要素の有無確認.