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.

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