Source code of Windows XP (NT5)
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.

303 lines
10 KiB

  1. //
  2. // Using simplified database matching api
  3. //
  4. // Sample Pseudo-Code :-)
  5. //
  6. //
  7. /*
  8. ANNOTATED XML:
  9. =============
  10. <APP NAME="Evil Notepad Application" VENDOR="Impostors International LTD.">
  11. <HISTORY ALIAS="VadimB" DATE="11/29/00">
  12. <DESCRIPTION>
  13. Created this nifty sample database to demonstrate functionality
  14. of sdbapint.dll
  15. </DESCRIPTION>
  16. </HISTORY>
  17. <BUG NUMBER="300000" DATABASE="WHISTLER"/>
  18. <EXE NAME="Notepad.exe" SIZE="50960" PECHECKSUM="0xE8B4" CHECKSUM="0x02C54A1C" ID="{6E98D683-8A03-4F5B-9B33-FB7429F2C978}">
  19. <!-- VadimB: This is where the entry is,
  20. the following attributes could be used (on the left is the XML spelling)
  21. All of the attributes below are supported (including the version-related attributes)
  22. SIZE - file size
  23. PECHECKSUM - Checksum from the PE Header
  24. CHECKSUM - checksum as calculated by our tools (see xmlwiz)
  25. BIN_FILE_VERSION - Binary file version from Fixed Version Information resource
  26. BIN_PRODUCT_VERSION - binary product version from Fixed Version Information resource
  27. PRODUCT_VERSION - "ProductVersion" resource
  28. FILE_DESCRIPTION - "FileDescription" resource
  29. COMPANY_NAME - "CompanyName" resource
  30. PRODUCT_NAME - "ProductName" resource
  31. FILEVERSION - "FileVersion" resource
  32. ORIGINALFILENAME - "OriginalFilename" resource
  33. INTERNALNAME - "InternalName" resource
  34. LEGALCOPYRIGHT - "LegalCopyright" resource
  35. UPTO_BIN_PRODUCT_VERSION - up to the specified binary product version
  36. VERFILEDATEHI - maps to the Fixed Version Into dwFileDateMS
  37. VERFILEDATELO - maps to the Fixed Version Info dwFileDateLS
  38. VERFILETYPE - maps to the Fixed Version Info dwFileType
  39. MODULETYPE - specify module type - as one of WIN32, WIN16, DOS, etc -- ONLY WIN32 is supported
  40. S16BITDESCRIPTION - 16-bit Module's description - SHOULD NOT be used
  41. Initially when composing XML, the ID attribute could be omitted - it is generated automatically
  42. by shimdbc.exe and your xml file will be updated with the (generated) id
  43. -->
  44. <!-- VadimB: This is what you can do with DRIVER_POLICY tags:
  45. - more than one tag allowed
  46. - NAME attribute should be unique within the scope of each EXE tag above
  47. - VALUETYPE must be present, the following value types are supported:
  48. STRING - maps to REG_SZ
  49. DWORD - maps to REG_DWORD
  50. QWORD - maps to REG_QWORD
  51. BINARY - maps to REG_BINARY
  52. - Values can be located within the scope of the tag -- or denoted as an attribute
  53. VALUE
  54. -->
  55. <!-- in the entry below - Policy1 defines a DWORD value 0x12345 -->
  56. <DRIVER_POLICY NAME="Policy1" VALUETYPE="DWORD" VALUE="0x12345"/>
  57. <!-- entry below is a string which will be picked up from the body of the tag "This is my string" -->
  58. <DRIVER_POLICY NAME="Policy2" VALUETYPE="STRING">
  59. This is my string
  60. </DRIVER_POLICY>
  61. <!-- entry below provides for 5 bytes of binary data -->
  62. <DRIVER_POLICY NAME="Policy3" VALUETYPE="BINARY" VALUE="1 2 3 4 5"/>
  63. <!-- entry below does not have any value associated with it -->
  64. <DRIVER_POLICY NAME="Policy4"/>
  65. <!-- entry below is a ULONGLONG -->
  66. <DRIVER_POLICY NAME="Policy5" VALUETYPE="QWORD" VALUE="0x1234567812345678"/>
  67. <DRIVER_POLICY NAME="Policy6" VALUETYPE ="STRING" VALUE="Testing in-line string">
  68. <!--
  69. this is a comment, so please ignore me
  70. -->
  71. </DRIVER_POLICY>
  72. </EXE>
  73. </APP>
  74. Using SdbQueryDriverInformation :
  75. Note the change
  76. DWORD
  77. SdbQueryDriverInformation(
  78. IN HSDB hSDB, // Database handle
  79. IN TAGREF trExe, // matching entry
  80. IN LPCWSTR lpszPolicyName, // Policy name which is to be retrieved
  81. OUT LPDWORD lpdwDataType, // Optional, receives information on the target data type
  82. OUT LPVOID lpBuffer, // buffer to fill with data
  83. IN OUT LPDWORD lpdwBufferSize // buffer size
  84. );
  85. Notes:
  86. 1. If lpszPolicyName == NULL then the function will attempt to copy all the available policy names
  87. into buffer pointed to by lpBuffer. If lpBuffer is NULL, lpdwBufferSize will receive the required size.
  88. Possible return values:
  89. ERROR_INSUFFICIENT_BUFFER - when lpBuffer == NULL or the size of the buffer is insufficient to hold the
  90. list of policy names
  91. ERROR_INVALID_PARAMETER - one of the parameters was invalid
  92. ERROR_INTERNAL_DB_CORRUPTION - the database is unusable
  93. ERROR_SUCCESS
  94. 2. If lpszPolicyName != NULL then the function will attempt to find the data for this policy. If lpBuffer is
  95. specified, lpdwBufferSize should point to the buffer size. lpdwBUfferSize could be NULL -- an attempt to
  96. copy data into lpBuffer will be made (under try/except).
  97. Possible return values:
  98. ERROR_NOT_FOUND - policy could not be found
  99. ERROR_INTERNAL_DB_CORRUPTION - the database is unusable
  100. ERROR_INSUFFICIENT_BUFFER - lpdwBufferSize will be updated with the correct size of the buffer
  101. ERROR_INVALID_DATA - lpBuffer parameter is invalid
  102. ERROR_SUCCESS
  103. */
  104. #include "shimdb.h"
  105. // ....
  106. //
  107. // it is assumed that the database has been mapped into memory
  108. // pDatabase is the pointer to the beginning of the database image
  109. // dwDatabaseSize is the size of the database image
  110. //
  111. // Also assumed:
  112. // lpszDriverPath - fully qualified name of the driver file that we're checking
  113. //
  114. {
  115. HSDB hSDB;
  116. TAGREF trDriver;
  117. SDBDRIVERINFORMATION DriverInfo;
  118. BOOL bSuccess;
  119. DWORD Status;
  120. DWORD dwDataSize;
  121. DWORD dwDataType;
  122. LPVOID pBuffer;
  123. DWORD dwData;
  124. //
  125. //
  126. hSDB = SdbInitDatabaseInMemory(pDatabase, dwDatabaseSize);
  127. if (NULL == hSDB) {
  128. //
  129. // something is terribly wrong -- database has failed to initialize
  130. //
  131. }
  132. //
  133. // Match exe
  134. //
  135. trDriver = SdbGetDatabaseMatch(hSDB, lpszDriverPath);
  136. if (TAGREF_NULL == trDriver) {
  137. //
  138. // there is no match in the database for this file
  139. //
  140. }
  141. else {
  142. //
  143. // we have a match, trExe is the "token" that references the match
  144. // now we shall read the relevant info
  145. bSuccess = SdbReadDriverInformation(hSDB, trDriver, &DriverInfo);
  146. if (!bSuccess) {
  147. //
  148. // for one reason or the other, this entry is corrupted
  149. //
  150. }
  151. //
  152. // DriverInfo contains the following:
  153. //
  154. // GUID guidID; // guid ID for this entry - ID attribute in XML
  155. // DWORD dwFlags; // registry flags for this exe - see below
  156. //
  157. // Each EXE can have flags in AppCompatibility section of the registry associated with it,
  158. // we should check whether this entry is disabled via a registry flag
  159. //
  160. if (DriverInfo.dwFlags & SHIMREG_DISABLE_DRIVER) {
  161. //
  162. // don't apply this entry, it has been de-activated through the registry
  163. //
  164. }
  165. //
  166. // Query for information - step 1 -- obtain all the available policies for this driver
  167. //
  168. Status = SdbQueryDriverInformation(hSDB,
  169. trDriver,
  170. NULL, // policy name
  171. &dwDataType, // pointer to the data type
  172. NULL, // pointer to the buffer
  173. &dwDataSize);
  174. // expected return value:
  175. // ERROR_INSUFFICIENT_BUFFER
  176. // dwDataSize will contain the required buffer size in bytes
  177. //
  178. // assuming that we allocated dwDataSize bytes, pBuffer is the buffer pointer
  179. //
  180. Status = SdbQueryDriverInformation(hSDB,
  181. trDriver,
  182. NULL,
  183. &dwDataType,
  184. pBuffer,
  185. &dwDataSize);
  186. //
  187. // expected return value:
  188. // ERROR_SUCCESS
  189. // dwDataSize will contain the number of bytes written into the buffer
  190. // dwDataType will contain REG_MULTI_SZ
  191. // pBuffer will receive the following data (unicode strings, assuming xml above was used)
  192. // Policy1\0Policy2\0Policy3.... \0\0
  193. //
  194. Status = SdbQueryDriverInformation(hSDB,
  195. trDriver,
  196. L"Policy1",
  197. &dwDataType,
  198. NULL,
  199. &dwDataSize);
  200. //
  201. // Expected return value:
  202. // ERROR_INSUFFICIENT_BUFFER
  203. // dwDataSize will contain 4
  204. // dwDataType will contain REG_DWORD
  205. //
  206. dwDataSize = sizeof(dwData);
  207. Status = SdbQueryDriverInformation(hSDB,
  208. trDriver,
  209. L"Policy1",
  210. &dwDataType,
  211. &dwData,
  212. &dwDataSize);
  213. // expected return value:
  214. // ERROR_SUCCESS
  215. // dwData will have a value of 0x12345
  216. //
  217. }
  218. //
  219. // After all the work is done - release the database
  220. //
  221. //
  222. SdbReleaseDatabase(hSDB);
  223. }