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.

521 lines
12 KiB

  1. #include "deskcmmn.h"
  2. #include <regstr.h>
  3. #include <ccstock.h>
  4. LPTSTR SubStrEnd(LPTSTR pszTarget, LPTSTR pszScan)
  5. {
  6. int i;
  7. for (i = 0; pszScan[i] != TEXT('\0') && pszTarget[i] != TEXT('\0') &&
  8. CharUpperChar(pszScan[i]) == CharUpperChar(pszTarget[i]); i++);
  9. if (pszTarget[i] == TEXT('\0'))
  10. {
  11. // we found the substring
  12. return pszScan + i;
  13. }
  14. return pszScan;
  15. }
  16. BOOL GetDeviceRegKey(LPCTSTR pstrDeviceKey, HKEY* phKey)
  17. {
  18. if(lstrlen(pstrDeviceKey) >= MAX_PATH)
  19. return FALSE;
  20. BOOL bRet = FALSE;
  21. // copy to local string
  22. TCHAR szBuffer[MAX_PATH];
  23. StringCchCopy(szBuffer, ARRAYSIZE(szBuffer), pstrDeviceKey);
  24. //
  25. // At this point, szBuffer has something like:
  26. // \REGISTRY\Machine\System\ControlSet001\Services\Jazzg300\Device0
  27. //
  28. // To use the Win32 registry calls, we have to strip off the \REGISTRY
  29. // and convert \Machine to HKEY_LOCAL_MACHINE
  30. //
  31. LPTSTR pszRegistryPath = SubStrEnd(SZ_REGISTRYMACHINE, szBuffer);
  32. if(pszRegistryPath)
  33. {
  34. // Open the registry key
  35. bRet = (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  36. pszRegistryPath,
  37. 0,
  38. KEY_QUERY_VALUE,
  39. phKey) == ERROR_SUCCESS);
  40. }
  41. return bRet;
  42. }
  43. int GetDisplayCPLPreference(LPCTSTR szRegVal)
  44. {
  45. int val = -1;
  46. HKEY hk;
  47. if (RegOpenKeyEx(HKEY_CURRENT_USER,
  48. REGSTR_PATH_CONTROLSFOLDER_DISPLAY,
  49. 0,
  50. KEY_READ,
  51. &hk) == ERROR_SUCCESS)
  52. {
  53. TCHAR sz[64];
  54. DWORD cb = sizeof(sz);
  55. *sz = 0;
  56. if ((RegQueryValueEx(hk, szRegVal, NULL, NULL,
  57. (LPBYTE)sz, &cb) == ERROR_SUCCESS) && *sz)
  58. {
  59. val = (int)MyStrToLong(sz);
  60. }
  61. RegCloseKey(hk);
  62. }
  63. if (val == -1 && RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  64. REGSTR_PATH_CONTROLSFOLDER_DISPLAY,
  65. 0,
  66. KEY_READ,
  67. &hk) == ERROR_SUCCESS)
  68. {
  69. TCHAR sz[64];
  70. DWORD cb = sizeof(sz);
  71. *sz = 0;
  72. if ((RegQueryValueEx(hk, szRegVal, NULL, NULL,
  73. (LPBYTE)sz, &cb) == ERROR_SUCCESS) && *sz)
  74. {
  75. val = (int)MyStrToLong(sz);
  76. }
  77. RegCloseKey(hk);
  78. }
  79. return val;
  80. }
  81. int GetDynaCDSPreference()
  82. {
  83. int iRegVal = GetDisplayCPLPreference(REGSTR_VAL_DYNASETTINGSCHANGE);
  84. if (iRegVal == -1)
  85. iRegVal = DCDSF_DYNA; // Apply dynamically
  86. return iRegVal;
  87. }
  88. void SetDisplayCPLPreference(LPCTSTR szRegVal, int val)
  89. {
  90. HKEY hk;
  91. if (RegCreateKeyEx(HKEY_CURRENT_USER,
  92. REGSTR_PATH_CONTROLSFOLDER_DISPLAY,
  93. 0,
  94. TEXT(""),
  95. 0,
  96. KEY_WRITE,
  97. NULL,
  98. &hk,
  99. NULL) == ERROR_SUCCESS)
  100. {
  101. TCHAR sz[64];
  102. StringCchPrintf(sz, ARRAYSIZE(sz), TEXT("%d"), val);
  103. RegSetValueEx(hk, szRegVal, NULL, REG_SZ,
  104. (LPBYTE)sz, (lstrlen(sz) + 1) * sizeof(TCHAR));
  105. RegCloseKey(hk);
  106. }
  107. }
  108. LONG WINAPI MyStrToLong(LPCTSTR sz)
  109. {
  110. long l=0;
  111. while (*sz >= TEXT('0') && *sz <= TEXT('9'))
  112. l = l*10 + (*sz++ - TEXT('0'));
  113. return l;
  114. }
  115. BOOL
  116. AllocAndReadInterfaceName(
  117. IN LPTSTR pDeviceKey,
  118. OUT LPWSTR* ppInterfaceName
  119. )
  120. /*
  121. Note: If this function retuns success, the caller is responsible
  122. to free the memory pointed by *ppInterfaceName
  123. */
  124. {
  125. BOOL bSuccess = FALSE;
  126. LPTSTR pszPath = NULL;
  127. HKEY hkDevice = 0;
  128. HKEY hkVolatileSettings = 0;
  129. pszPath = SubStrEnd(SZ_REGISTRYMACHINE, pDeviceKey);
  130. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  131. pszPath,
  132. 0,
  133. KEY_READ,
  134. &hkDevice) != ERROR_SUCCESS)
  135. {
  136. goto Cleanup;
  137. }
  138. if (RegOpenKeyEx(hkDevice,
  139. SZ_VOLATILE_SETTINGS,
  140. 0,
  141. KEY_READ,
  142. &hkVolatileSettings) != ERROR_SUCCESS)
  143. {
  144. goto Cleanup;
  145. }
  146. bSuccess = AllocAndReadValue(hkVolatileSettings,
  147. SZ_DISPLAY_ADAPTER_INTERFACE_NAME,
  148. ppInterfaceName);
  149. Cleanup:
  150. if (hkVolatileSettings) {
  151. RegCloseKey(hkVolatileSettings);
  152. }
  153. if (hkDevice) {
  154. RegCloseKey(hkDevice);
  155. }
  156. return bSuccess;
  157. }
  158. BOOL
  159. AllocAndReadInstanceID(
  160. IN LPTSTR pDeviceKey,
  161. OUT LPWSTR* ppInstanceID
  162. )
  163. /*
  164. Note: If this function retuns success, the caller is responsible
  165. to free the memory pointed by *ppInstanceID
  166. */
  167. {
  168. LPTSTR pDeviceKeyCopy = NULL, pDeviceKeyCopy2 = NULL;
  169. LPTSTR pTemp = NULL, pX = NULL;
  170. BOOL bSuccess = FALSE;
  171. HKEY hkEnum = 0;
  172. HKEY hkService = 0;
  173. HKEY hkCommon = 0;
  174. DWORD Count = 0;
  175. DWORD cb = 0, len = 0;
  176. //
  177. // Make a copy of pDeviceKey
  178. //
  179. len = max (256, (lstrlen(pDeviceKey) + 6) * sizeof(TCHAR));
  180. pDeviceKeyCopy2 = pDeviceKeyCopy = (LPTSTR)LocalAlloc(LPTR, len);
  181. if (pDeviceKeyCopy == NULL)
  182. {
  183. goto Cleanup;
  184. }
  185. StringCbCopy(pDeviceKeyCopy, len, pDeviceKey);
  186. pTemp = SubStrEnd(SZ_REGISTRYMACHINE, pDeviceKeyCopy);
  187. pDeviceKeyCopy = pTemp;
  188. //
  189. // Open the service key
  190. //
  191. pTemp = pDeviceKeyCopy + lstrlen(pDeviceKeyCopy);
  192. while ((pTemp != pDeviceKeyCopy) && (*pTemp != TEXT('\\')))
  193. {
  194. pTemp--;
  195. }
  196. if (pTemp == pDeviceKeyCopy)
  197. {
  198. goto Cleanup;
  199. }
  200. pX = SubStrEnd(SZ_DEVICE, pTemp);
  201. if (pX == pTemp)
  202. {
  203. //
  204. // The new key is used: CCS\Control\Video\[GUID]\000X
  205. //
  206. *pTemp = UNICODE_NULL;
  207. StringCbCat(pDeviceKeyCopy, len, SZ_COMMON_SUBKEY);
  208. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  209. pDeviceKeyCopy,
  210. 0,
  211. KEY_READ,
  212. &hkCommon) != ERROR_SUCCESS)
  213. {
  214. goto Cleanup;
  215. }
  216. pDeviceKeyCopy = pDeviceKeyCopy2;
  217. ZeroMemory(pDeviceKeyCopy, len);
  218. StringCbCopy(pDeviceKeyCopy, len, SZ_SERVICES_PATH);
  219. cb = len - (lstrlen(pDeviceKeyCopy) + 1) * sizeof(TCHAR);
  220. if (RegQueryValueEx(hkCommon,
  221. SZ_SERVICE,
  222. NULL,
  223. NULL,
  224. (LPBYTE)(pDeviceKeyCopy + lstrlen(pDeviceKeyCopy)),
  225. &cb) != ERROR_SUCCESS)
  226. {
  227. goto Cleanup;
  228. }
  229. }
  230. else
  231. {
  232. //
  233. // The old key is used: CCS\Services\[SrvName]\DeviceX
  234. //
  235. *pTemp = UNICODE_NULL;
  236. }
  237. //
  238. // Open the ServiceName key
  239. //
  240. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  241. pDeviceKeyCopy,
  242. 0,
  243. KEY_READ,
  244. &hkService) != ERROR_SUCCESS)
  245. {
  246. goto Cleanup;
  247. }
  248. //
  249. // Open the "Enum" key under the devicename
  250. //
  251. if (RegOpenKeyEx(hkService,
  252. SZ_ENUM,
  253. 0,
  254. KEY_READ,
  255. &hkEnum) != ERROR_SUCCESS)
  256. {
  257. goto Cleanup;
  258. }
  259. cb = sizeof(Count);
  260. if ((RegQueryValueEx(hkEnum,
  261. SZ_VU_COUNT,
  262. NULL,
  263. NULL,
  264. (LPBYTE)&Count,
  265. &cb) != ERROR_SUCCESS) ||
  266. (Count != 1))
  267. {
  268. //
  269. // Igonore the case when there are at least 2 devices.
  270. //
  271. goto Cleanup;
  272. }
  273. bSuccess = AllocAndReadValue(hkEnum, TEXT("0"), ppInstanceID);
  274. Cleanup:
  275. if (hkEnum != 0)
  276. {
  277. RegCloseKey(hkEnum);
  278. }
  279. if (hkService != 0)
  280. {
  281. RegCloseKey(hkService);
  282. }
  283. if (hkCommon != 0)
  284. {
  285. RegCloseKey(hkCommon);
  286. }
  287. if (pDeviceKeyCopy2 != NULL)
  288. {
  289. LocalFree(pDeviceKeyCopy2);
  290. }
  291. return bSuccess;
  292. }
  293. BOOL
  294. AllocAndReadValue(
  295. IN HKEY hkKey,
  296. IN LPTSTR pValueName,
  297. OUT LPWSTR* ppwValueData
  298. )
  299. /*
  300. Note: If this function retuns success, the caller is responsible
  301. to free the memory pointed by *ppwValueData
  302. */
  303. {
  304. LPWSTR pwValueData = NULL;
  305. DWORD AllocUnit = 64;
  306. DWORD cBytes = 0;
  307. BOOL bSuccess = FALSE;
  308. LONG Error = ERROR_SUCCESS;
  309. while (!bSuccess)
  310. {
  311. AllocUnit *= 2;
  312. cBytes = AllocUnit * sizeof(WCHAR);
  313. pwValueData = (LPWSTR)(LocalAlloc(LPTR, cBytes));
  314. if (pwValueData == NULL)
  315. break;
  316. Error = RegQueryValueEx(hkKey,
  317. pValueName,
  318. NULL,
  319. NULL,
  320. (LPBYTE)pwValueData,
  321. &cBytes);
  322. bSuccess = (Error == ERROR_SUCCESS);
  323. if (!bSuccess)
  324. {
  325. LocalFree(pwValueData);
  326. pwValueData = NULL;
  327. if (Error != ERROR_MORE_DATA)
  328. break;
  329. }
  330. }
  331. if (bSuccess)
  332. {
  333. *ppwValueData = pwValueData;
  334. }
  335. return bSuccess;
  336. }
  337. VOID
  338. DeskAESnapshot(
  339. HKEY hkExtensions,
  340. PAPPEXT* ppAppExtList
  341. )
  342. {
  343. HKEY hkSubkey = 0;
  344. DWORD index = 0;
  345. DWORD ulSize = MAX_PATH;
  346. APPEXT AppExtTemp;
  347. PAPPEXT pAppExtBefore = NULL;
  348. PAPPEXT pAppExtTemp = NULL;
  349. ulSize = ARRAYSIZE(AppExtTemp.szKeyName);
  350. while (RegEnumKeyEx(hkExtensions,
  351. index,
  352. AppExtTemp.szKeyName,
  353. &ulSize,
  354. NULL,
  355. NULL,
  356. NULL,
  357. NULL) == ERROR_SUCCESS)
  358. {
  359. if (RegOpenKeyEx(hkExtensions,
  360. AppExtTemp.szKeyName,
  361. 0,
  362. KEY_READ,
  363. &hkSubkey) == ERROR_SUCCESS)
  364. {
  365. ulSize = sizeof(AppExtTemp.szDefaultValue);
  366. if ((RegQueryValueEx(hkSubkey,
  367. NULL,
  368. 0,
  369. NULL,
  370. (PBYTE)AppExtTemp.szDefaultValue,
  371. &ulSize) == ERROR_SUCCESS) &&
  372. (AppExtTemp.szDefaultValue[0] != TEXT('\0')))
  373. {
  374. PAPPEXT pAppExt = (PAPPEXT)LocalAlloc(LPTR, sizeof(APPEXT));
  375. if (pAppExt != NULL)
  376. {
  377. *pAppExt = AppExtTemp;
  378. pAppExtBefore = pAppExtTemp = *ppAppExtList;
  379. while((pAppExtTemp != NULL) &&
  380. (lstrcmpi(pAppExtTemp->szDefaultValue,
  381. pAppExt->szDefaultValue) < 0))
  382. {
  383. pAppExtBefore = pAppExtTemp;
  384. pAppExtTemp = pAppExtTemp->pNext;
  385. }
  386. if (pAppExtBefore != pAppExtTemp)
  387. {
  388. pAppExt->pNext = pAppExtBefore->pNext;
  389. pAppExtBefore->pNext = pAppExt;
  390. }
  391. else
  392. {
  393. pAppExt->pNext = *ppAppExtList;
  394. *ppAppExtList = pAppExt;
  395. }
  396. }
  397. }
  398. RegCloseKey(hkSubkey);
  399. }
  400. ulSize = ARRAYSIZE(AppExtTemp.szKeyName);
  401. index++;
  402. }
  403. }
  404. VOID
  405. DeskAECleanup(
  406. PAPPEXT pAppExt
  407. )
  408. {
  409. PAPPEXT pAppExtTemp;
  410. while (pAppExt)
  411. {
  412. pAppExtTemp = pAppExt->pNext;
  413. LocalFree(pAppExt);
  414. pAppExt = pAppExtTemp;
  415. }
  416. }