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.

279 lines
9.1 KiB

  1. /*****************************************************************************
  2. * (C) COPYRIGHT MICROSOFT CORPORATION, 2002
  3. *
  4. * AUTHOR: ByronC
  5. *
  6. * DATE: 4/14/2002
  7. *
  8. * @doc INTERNAL
  9. *
  10. * @module WiaDeviceKey.cpp - Implmenentation for <c WiaDeviceKey> |
  11. *
  12. * This file contains the implementation for the <c WiaDeviceKey> class.
  13. *
  14. *****************************************************************************/
  15. #include "precomp.h"
  16. /*****************************************************************************
  17. * @doc INTERNAL
  18. *
  19. * @mfunc | WiaDeviceKey | WiaDeviceKey |
  20. *
  21. * We initialize all member variables. In general, this sets the values to 0,
  22. * except:
  23. * <nl><md WiaDeviceKey::m_ulSig> is set to be WiaDeviceKey_INIT_SIG.
  24. * <nl><md WiaDeviceKey::m_cRef> is set to be 1.
  25. *
  26. *****************************************************************************/
  27. WiaDeviceKey::WiaDeviceKey(const CSimpleStringWide &cswDeviceID) :
  28. m_ulSig(WiaDeviceKey_INIT_SIG),
  29. m_cRef(1),
  30. m_cswDeviceID(cswDeviceID)
  31. {
  32. }
  33. /*****************************************************************************
  34. * @doc INTERNAL
  35. *
  36. * @mfunc | WiaDeviceKey | ~WiaDeviceKey |
  37. *
  38. * Do any cleanup that is not already done.
  39. *
  40. * Also:
  41. * <nl><md WiaDeviceKey::m_ulSig> is set to be WiaDeviceKey_DEL_SIG.
  42. *
  43. *****************************************************************************/
  44. WiaDeviceKey::~WiaDeviceKey()
  45. {
  46. m_ulSig = WiaDeviceKey_DEL_SIG;
  47. m_cRef = 0;
  48. }
  49. /*****************************************************************************
  50. * @doc INTERNAL
  51. *
  52. * @mfunc ULONG | WiaDeviceKey | AddRef |
  53. *
  54. * Increments this object's ref count. We should always AddRef when handing
  55. * out a pointer to this object.
  56. *
  57. * @rvalue Count |
  58. * The reference count after the count has been incremented.
  59. *****************************************************************************/
  60. ULONG __stdcall WiaDeviceKey::AddRef()
  61. {
  62. InterlockedIncrement((long*) &m_cRef);
  63. return m_cRef;
  64. }
  65. /*****************************************************************************
  66. * @doc INTERNAL
  67. *
  68. * @mfunc ULONG | WiaDeviceKey | Release |
  69. *
  70. * Decrement this object's ref count. We should always Release when finished
  71. * with a pointer to this object.
  72. *
  73. * @rvalue Count |
  74. * The reference count after the count has been decremented.
  75. *****************************************************************************/
  76. ULONG __stdcall WiaDeviceKey::Release()
  77. {
  78. ULONG ulRefCount = m_cRef - 1;
  79. if (InterlockedDecrement((long*) &m_cRef) == 0)
  80. {
  81. delete this;
  82. return 0;
  83. }
  84. return ulRefCount;
  85. }
  86. /*****************************************************************************
  87. * @doc INTERNAL
  88. *
  89. * @mfunc CSimpleStringWIde | WiaDeviceKey | getDeviceKeyPath |
  90. *
  91. * This method retuns the device key path relative to HKLM.
  92. *
  93. * @rvalue CSimpleStringWIde |
  94. * A string representing the registry path to the device key
  95. * relative to HKLM.
  96. *****************************************************************************/
  97. CSimpleStringWide WiaDeviceKey::getDeviceKeyPath()
  98. {
  99. //
  100. // Start looking uner our devnode device keys
  101. //
  102. m_cswRootPath = IMG_DEVNODE_CLASS_REGPATH;
  103. CSimpleReg csrDevNodeDeviceRoot(HKEY_LOCAL_MACHINE, m_cswRootPath, false, KEY_READ);
  104. bool bDeviceNotFound = csrDevNodeDeviceRoot.EnumKeys(WiaDeviceKey::ProcessDeviceKeys, (LPARAM)this);
  105. if (bDeviceNotFound)
  106. {
  107. m_cswDeviceKeyPath = L"";
  108. }
  109. else
  110. {
  111. //
  112. // m_cswDeviceKeyPath now contains the path to the Device key
  113. //
  114. }
  115. return m_cswDeviceKeyPath;
  116. }
  117. /*****************************************************************************
  118. * @doc INTERNAL
  119. *
  120. * @mfunc bool | WiaDeviceKey | ProcessDeviceKeys |
  121. *
  122. * This method is called on each sub-key under the class key as part of an
  123. * enumeration to find the key corresponding to a specific deviceID (since
  124. * they're not necessarily the same).
  125. *
  126. * On return, we set the <md WiaDeviceKey::m_cswDeviceKeyPath>
  127. * member.
  128. *
  129. * @parm CKeyEnumInfo& | enumInfo |
  130. * Indicates the current sub-key we're on.
  131. *
  132. * @rvalue false |
  133. * Indicates we can stop with the enumeration. We found the correct
  134. * device.
  135. * @rvalue true |
  136. * Indicates we should continue with the enumeration.
  137. *****************************************************************************/
  138. bool WiaDeviceKey::ProcessDeviceKeys(
  139. CSimpleReg::CKeyEnumInfo &enumInfo)
  140. {
  141. bool bContinueEnumeration = TRUE;
  142. //
  143. // Check that we have a This pointer
  144. //
  145. WiaDeviceKey *This = (WiaDeviceKey*)enumInfo.lParam;
  146. if (This)
  147. {
  148. //
  149. // Open this sub-key.
  150. //
  151. CSimpleReg csrDeviceSubKey(enumInfo.hkRoot, enumInfo.strName);
  152. if (csrDeviceSubKey.OK())
  153. {
  154. //
  155. // Check whether this is the one we want.
  156. //
  157. CSimpleStringWide cswDeviceID = csrDeviceSubKey.Query(DEVICE_ID_VALUE_NAME, L"");
  158. if (cswDeviceID.CompareNoCase(This->m_cswDeviceID) == 0)
  159. {
  160. CSimpleString cswSlash = L"\\";
  161. This->m_cswDeviceKeyPath = This->m_cswRootPath + cswSlash + enumInfo.strName;
  162. bContinueEnumeration = FALSE;
  163. }
  164. }
  165. }
  166. return bContinueEnumeration;
  167. }
  168. /*****************************************************************************
  169. * @doc INTERNAL
  170. *
  171. * @mfunc CSimpleStringWide | WiaDeviecKey | getDeviceEventKeyPath |
  172. *
  173. * Returns the path to the device event registry key relative to HKLM
  174. *
  175. * @parm const GUID& | guidEvent |
  176. * Specifies the event to look up.
  177. *
  178. * @rvalue S_OK |
  179. * The method succeeded.
  180. *****************************************************************************/
  181. CSimpleStringWide WiaDeviceKey::getDeviceEventKeyPath(
  182. const GUID &guidEvent)
  183. {
  184. CSimpleStringWide cswEventPath;
  185. CSimpleStringWide cswDeviceKey = getDeviceKeyPath();
  186. if (cswDeviceKey.Length() > 0)
  187. {
  188. //
  189. // Save the parameters in member fields so we can search on them during
  190. // reg key enumeration. Enumeration is done via procedure callbacks,
  191. // in which we pass (this) as a parameter.
  192. //
  193. WCHAR wszGuid[40];
  194. if (StringFromGUID2(guidEvent, wszGuid, sizeof(wszGuid)/sizeof(wszGuid[0])))
  195. {
  196. wszGuid[(sizeof(wszGuid)/sizeof(wszGuid[0])) - 1] = L'\0';
  197. m_cswEventGuidString = wszGuid;
  198. }
  199. m_cswRootPath = cswDeviceKey + EVENT_STR;
  200. CSimpleReg csrDeviceEventKey(HKEY_LOCAL_MACHINE, m_cswRootPath, false, KEY_READ);
  201. bool bEventNotFound = csrDeviceEventKey.EnumKeys(WiaDeviceKey::ProcessEventSubKey,
  202. (LPARAM) this);
  203. if (bEventNotFound)
  204. {
  205. cswEventPath = L"";
  206. }
  207. else
  208. {
  209. cswEventPath = m_cswRootPath;
  210. }
  211. }
  212. return cswEventPath;
  213. }
  214. /*****************************************************************************
  215. * @doc INTERNAL
  216. *
  217. * @mfunc bool | WiaDeviceKey | ProcessEventSubKey |
  218. *
  219. * This method is called on each sub-key as part of an enumeration of all
  220. * the event sub-keys. The enumeration will stop if we return false from
  221. * this method.
  222. *
  223. * If successful, the <md WiaDeviceKey::m_cswRootPath> will contain the
  224. * path to this event key.
  225. *
  226. * @parm CKeyEnumInfo& | enumInfo |
  227. * Indicates the current sub-key we're on.
  228. *
  229. * @rvalue false |
  230. * Indicates we can stop with the enumeration. We found the correct
  231. * event key.
  232. * @rvalue true |
  233. * Indicates we should continue with the enumeration.
  234. *****************************************************************************/
  235. bool WiaDeviceKey::ProcessEventSubKey(
  236. CSimpleReg::CKeyEnumInfo &enumInfo)
  237. {
  238. bool bContinueEnumeration = true;
  239. //
  240. // Check that we have a This pointer
  241. //
  242. WiaDeviceKey *This = (WiaDeviceKey*)enumInfo.lParam;
  243. if (This)
  244. {
  245. //
  246. // Open this sub-key. We're looking for a sub-key which contains a GUID entry
  247. // matching m_cswEventGuidString.
  248. //
  249. CSimpleReg csrEventSubKey(enumInfo.hkRoot, enumInfo.strName);
  250. CSimpleStringWide cswGuidValue = csrEventSubKey.Query(GUID_VALUE_NAME, L"{00000000-0000-0000-0000-000000000000}");
  251. if (cswGuidValue.CompareNoCase(This->m_cswEventGuidString) == 0)
  252. {
  253. //
  254. // We found the key we're looking for. Construct the path to this key.
  255. //
  256. This->m_cswRootPath += L"\\";
  257. This->m_cswRootPath += enumInfo.strName;
  258. bContinueEnumeration = false;
  259. }
  260. }
  261. return bContinueEnumeration;
  262. }