Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
4.3 KiB

  1. #include "hwdev.h"
  2. #include "setupapi.h"
  3. #include "dtctreg.h"
  4. #include "reg.h"
  5. #include "namellst.h"
  6. #include "sfstr.h"
  7. #include "str.h"
  8. #include "cmmn.h"
  9. #include "misc.h"
  10. #include "dbg.h"
  11. #define ARRAYSIZE(a) (sizeof((a))/sizeof((a)[0]))
  12. ///////////////////////////////////////////////////////////////////////////////
  13. //
  14. HRESULT CHWDeviceInst::Init(DEVINST devinst)
  15. {
  16. _devinst = devinst;
  17. return S_OK;
  18. }
  19. HRESULT CHWDeviceInst::InitInterfaceGUID(const GUID* pguidInterface)
  20. {
  21. _guidInterface = *pguidInterface;
  22. return S_OK;
  23. }
  24. HRESULT CHWDeviceInst::GetDeviceInstance(DEVINST* pdevinst)
  25. {
  26. HRESULT hr;
  27. if (_devinst)
  28. {
  29. *pdevinst = _devinst;
  30. hr = S_OK;
  31. }
  32. else
  33. {
  34. *pdevinst = 0;
  35. hr = S_FALSE;
  36. }
  37. return hr;
  38. }
  39. HRESULT CHWDeviceInst::GetPnpID(LPWSTR pszPnpID, DWORD cchPnpID)
  40. {
  41. HRESULT hr = _InitPnpInfo();
  42. if (SUCCEEDED(hr) && (S_FALSE != hr))
  43. {
  44. hr = SafeStrCpyN(pszPnpID, _szPnpID, cchPnpID);
  45. }
  46. else
  47. {
  48. hr = S_FALSE;
  49. }
  50. return hr;
  51. }
  52. HRESULT CHWDeviceInst::GetInterfaceGUID(GUID* pguidInterface)
  53. {
  54. ASSERT(guidInvalid != _guidInterface);
  55. *pguidInterface = _guidInterface;
  56. return S_OK;
  57. }
  58. HRESULT CHWDeviceInst::IsRemovableDevice(BOOL* pfRemovable)
  59. {
  60. return _DeviceInstIsRemovable(_devinst, pfRemovable);
  61. }
  62. HRESULT CHWDeviceInst::ShouldAutoplayOnSpecialInterface(
  63. const GUID* pguidInterface, BOOL* pfShouldAutoplay)
  64. {
  65. WCHAR szGUID[MAX_GUIDSTRING];
  66. HRESULT hr = _StringFromGUID(pguidInterface, szGUID,
  67. ARRAYSIZE(szGUID));
  68. *pfShouldAutoplay = FALSE;
  69. if (SUCCEEDED(hr))
  70. {
  71. WCHAR szGUIDFromReg[MAX_GUIDSTRING];
  72. DWORD dwType;
  73. hr = _GetDevicePropertyGeneric(this,
  74. TEXT("AutoplayOnSpecialInterface"), FALSE, &dwType, (PBYTE)szGUIDFromReg,
  75. sizeof(szGUIDFromReg));
  76. if (SUCCEEDED(hr) && (S_FALSE != hr))
  77. {
  78. if (REG_SZ == dwType)
  79. {
  80. if (!lstrcmpi(szGUIDFromReg, szGUID))
  81. {
  82. *pfShouldAutoplay = TRUE;
  83. }
  84. }
  85. }
  86. if (*pfShouldAutoplay)
  87. {
  88. DIAGNOSTIC((TEXT("[0314]Autoplay on Special Interface %s -> Autoplay!"), szGUID));
  89. }
  90. else
  91. {
  92. DIAGNOSTIC((TEXT("[0315]*NO* Autoplay on Special Interface %s -> No Autoplay!"), szGUID));
  93. }
  94. }
  95. return hr;
  96. }
  97. ///////////////////////////////////////////////////////////////////////////////
  98. //
  99. CHWDeviceInst::CHWDeviceInst() : _devinst(0), _guidInterface(guidInvalid)
  100. {}
  101. CHWDeviceInst::~CHWDeviceInst()
  102. {}
  103. ///////////////////////////////////////////////////////////////////////////////
  104. //
  105. HRESULT CHWDeviceInst::_InitPnpInfo()
  106. {
  107. HRESULT hres;
  108. // This require the _devinst to be set
  109. if (0 != _devinst)
  110. {
  111. hres = _InitPnpID();
  112. if (FAILED(hres))
  113. {
  114. // Probably not a removable device
  115. hres = S_FALSE;
  116. }
  117. }
  118. else
  119. {
  120. hres = S_FALSE;
  121. }
  122. return hres;
  123. }
  124. HRESULT _FindInstID(LPWSTR pszPnpID, DWORD* pcch)
  125. {
  126. DWORD cToFind = 2;
  127. LPWSTR psz = pszPnpID;
  128. *pcch = 0;
  129. while (*psz && cToFind)
  130. {
  131. if ((TEXT('\\') == *psz))
  132. {
  133. --cToFind;
  134. }
  135. if (cToFind)
  136. {
  137. ++psz;
  138. }
  139. }
  140. if (*psz)
  141. {
  142. *pcch = (DWORD)(psz - pszPnpID);
  143. }
  144. return S_OK;
  145. }
  146. HRESULT _GetPnpIDHelper(DEVINST devinst, LPWSTR pszPnpID, DWORD cchPnpID)
  147. {
  148. HRESULT hres = S_FALSE;
  149. HMACHINE hMachine = NULL;
  150. CONFIGRET cr = CM_Get_Device_ID_Ex(devinst, pszPnpID,
  151. cchPnpID, 0, hMachine);
  152. if (CR_SUCCESS == cr)
  153. {
  154. hres = S_OK;
  155. }
  156. return hres;
  157. }
  158. HRESULT CHWDeviceInst::_InitPnpID()
  159. {
  160. HRESULT hres = _GetPnpIDHelper(_devinst, _szPnpID, ARRAYSIZE(_szPnpID));
  161. if (SUCCEEDED(hres) && (S_FALSE != hres))
  162. {
  163. DWORD cchInstIDOffset;
  164. hres = _FindInstID(_szPnpID, &cchInstIDOffset);
  165. if (SUCCEEDED(hres))
  166. {
  167. *(_szPnpID + cchInstIDOffset) = 0;
  168. }
  169. }
  170. return hres;
  171. }