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.

561 lines
14 KiB

  1. //*********************************************************************
  2. //* Microsoft Windows **
  3. //* Copyright(c) Microsoft Corp., 1993 **
  4. //*********************************************************************
  5. #include "admincfg.h"
  6. #include <stdlib.h>
  7. // global small buffer for general use
  8. TCHAR szSmallBuf[SMALLBUF];
  9. BOOL EnableDlgItem(HWND hDlg,UINT uID,BOOL fEnable)
  10. {
  11. return EnableWindow(GetDlgItem(hDlg,uID),fEnable);
  12. }
  13. BOOL IsDlgItemEnabled(HWND hDlg,UINT uID)
  14. {
  15. return IsWindowEnabled(GetDlgItem(hDlg,uID));
  16. }
  17. int MsgBox(HWND hWnd,UINT nResource,UINT uIcon,UINT uButtons)
  18. {
  19. TCHAR szMsgBuf[REGBUFLEN];
  20. LoadSz(IDS_APPNAME,szSmallBuf,ARRAYSIZE(szSmallBuf));
  21. LoadSz(nResource,szMsgBuf,ARRAYSIZE(szMsgBuf));
  22. MessageBeep(uIcon);
  23. return (MessageBox(hWnd,szMsgBuf,szSmallBuf,uIcon | uButtons));
  24. }
  25. int MsgBoxSz(HWND hWnd,LPTSTR szText,UINT uIcon,UINT uButtons)
  26. {
  27. LoadSz(IDS_APPNAME,szSmallBuf,ARRAYSIZE(szSmallBuf));
  28. MessageBeep(uIcon);
  29. return (MessageBox(hWnd,szText,szSmallBuf,uIcon | uButtons));
  30. }
  31. int MsgBoxParam(HWND hWnd,UINT nResource,TCHAR * szReplaceText,UINT uIcon,
  32. UINT uButtons)
  33. {
  34. #if 1
  35. TCHAR szFormat[REGBUFLEN];
  36. LPTSTR lpMsgBuf;
  37. INT iResult;
  38. lpMsgBuf = (LPTSTR) LocalAlloc (LPTR, (lstrlen(szReplaceText) + 1 + REGBUFLEN) * sizeof(TCHAR));
  39. if (!lpMsgBuf)
  40. {
  41. return 0;
  42. }
  43. LoadSz(nResource,szFormat,ARRAYSIZE(szFormat));
  44. wsprintf(lpMsgBuf,szFormat,szReplaceText);
  45. iResult = MsgBoxSz(hWnd,lpMsgBuf,uIcon,uButtons);
  46. LocalFree (lpMsgBuf);
  47. return iResult;
  48. #else
  49. //
  50. // szReplaceText can be REGBUFLEN+1 + sizeof(szFormat) (also REGBUFLEN),
  51. // so szMsgBuf must be sizeof(szReplaceText) + sizeof(szFormat).
  52. //
  53. TCHAR szMsgBuf[(REGBUFLEN+1) * 2],szFormat[REGBUFLEN];
  54. LoadSz(nResource,szFormat,ARRAYSIZE(szFormat));
  55. wsprintf(szMsgBuf,szFormat,szReplaceText);
  56. return (MsgBoxSz(hWnd,szMsgBuf,uIcon,uButtons));
  57. #endif
  58. }
  59. LONG AddListboxItem(HWND hDlg,int idControl,TCHAR * szItem)
  60. {
  61. return (LONG)(SendDlgItemMessage(hDlg,idControl,LB_ADDSTRING,0,
  62. (LPARAM) szItem));
  63. }
  64. LONG GetListboxItemText(HWND hDlg,int idControl,UINT nIndex,TCHAR * szText)
  65. {
  66. return (LONG)(SendDlgItemMessage(hDlg,idControl,LB_GETTEXT,(WPARAM) nIndex,
  67. (LPARAM) szText));
  68. }
  69. LONG SetListboxItemData(HWND hDlg,int idControl,UINT nIndex,LPARAM dwData)
  70. {
  71. return (LONG)(SendDlgItemMessage(hDlg,idControl,LB_SETITEMDATA,(WPARAM) nIndex,
  72. dwData));
  73. }
  74. LONG GetListboxItemData(HWND hDlg,int idControl,UINT nIndex)
  75. {
  76. return (LONG)(SendDlgItemMessage(hDlg,idControl,LB_GETITEMDATA,(WPARAM) nIndex,
  77. 0L));
  78. }
  79. LONG SetListboxSelection(HWND hDlg,int idControl,UINT nIndex)
  80. {
  81. return (LONG)(SendDlgItemMessage(hDlg,idControl,LB_SETCURSEL,(WPARAM) nIndex,
  82. 0L));
  83. }
  84. LONG GetListboxSelection(HWND hDlg,int idControl)
  85. {
  86. return (LONG)(SendDlgItemMessage(hDlg,idControl,LB_GETCURSEL,0,
  87. 0L));
  88. }
  89. TCHAR * ResizeBuffer(TCHAR *pBuf,HGLOBAL hBuf,DWORD dwNeeded,DWORD * pdwCurSize)
  90. {
  91. TCHAR * pNew;
  92. if (dwNeeded <= *pdwCurSize) return pBuf; // nothing to do
  93. *pdwCurSize = dwNeeded;
  94. GlobalUnlock(hBuf);
  95. if (!GlobalReAlloc(hBuf,dwNeeded * sizeof(TCHAR),GHND))
  96. return NULL;
  97. if (!(pNew = (TCHAR *) GlobalLock(hBuf))) return NULL;
  98. return pNew;
  99. }
  100. LPTSTR LoadSz(UINT idString,LPTSTR lpszBuf,UINT cbBuf)
  101. {
  102. // Clear the buffer and load the string
  103. if ( lpszBuf )
  104. {
  105. *lpszBuf = TEXT('\0');
  106. LoadString( ghInst, idString, lpszBuf, cbBuf );
  107. }
  108. return lpszBuf;
  109. }
  110. DWORD RoundToDWord(DWORD dwSize)
  111. {
  112. DWORD dwResult = dwSize;
  113. if ( dwResult % sizeof(DWORD)) dwResult += (sizeof(DWORD) -
  114. dwResult % sizeof(DWORD));
  115. return dwResult;
  116. }
  117. /*******************************************************************
  118. NAME: MyRegDeleteKey
  119. SYNOPSIS: This does what RegDeleteKey should do, which is delete
  120. keys that have subkeys. Unfortunately, NT jumped off
  121. the empire state building and we have to jump off after
  122. them, so RegDeleteKey will (one day, I'm told) puke on
  123. keys that have subkeys. This fcn enums them and recursively
  124. deletes the leaf keys.
  125. NOTES: This algorithm goes against the spirit of what's mentioned
  126. in the SDK, which is "don't mess with a key that you're enuming",
  127. since it deletes keys during an enum. However, MarcW the
  128. registry guru says that this is the way to do it. The alternative,
  129. I suppose, is to allocate buffers and enum all the subkeys into
  130. the buffer, close the enum and go back through the buffer
  131. elminating the subkeys. But since you have to do this recursively,
  132. you would wind up allocating a lot of buffers.
  133. ********************************************************************/
  134. LONG MyRegDeleteKey(HKEY hkey,LPTSTR pszSubkey)
  135. {
  136. TCHAR szSubkeyName[MAX_PATH+1];
  137. HKEY hkeySubkey;
  138. UINT uRet;
  139. uRet = RegOpenKey(hkey,pszSubkey,&hkeySubkey);
  140. if (uRet != ERROR_SUCCESS)
  141. return uRet;
  142. // best algorithm according to marcw: keep deleting zeroth subkey till
  143. // there are no more
  144. while (RegEnumKey(hkeySubkey,0,szSubkeyName,ARRAYSIZE(szSubkeyName))
  145. == ERROR_SUCCESS && (uRet == ERROR_SUCCESS)) {
  146. uRet=MyRegDeleteKey(hkeySubkey,szSubkeyName);
  147. }
  148. RegCloseKey(hkeySubkey);
  149. if (uRet != ERROR_SUCCESS)
  150. return uRet;
  151. return RegDeleteKey(hkey,pszSubkey);
  152. }
  153. //*************************************************************
  154. //
  155. // MyRegLoadKey()
  156. //
  157. // Purpose: Loads a hive into the registry
  158. //
  159. // Parameters: hKey - Key to load the hive into
  160. // lpSubKey - Subkey name
  161. // lpFile - hive filename
  162. //
  163. // Return: ERROR_SUCCESS if successful
  164. // Error number if not
  165. //
  166. // Comments:
  167. //
  168. // History: Date Author Comment
  169. // 6/22/95 ericflo Created
  170. //
  171. //*************************************************************
  172. LONG MyRegLoadKey(HKEY hKey, LPCTSTR lpSubKey, LPTSTR lpFile)
  173. {
  174. HANDLE hToken;
  175. LUID luid;
  176. DWORD dwSize = 1024;
  177. PTOKEN_PRIVILEGES lpPrevPrivilages;
  178. TOKEN_PRIVILEGES tp;
  179. LONG error;
  180. if (g_bWinnt) {
  181. //
  182. // Allocate space for the old privileges
  183. //
  184. lpPrevPrivilages = GlobalAlloc(GPTR, dwSize);
  185. if (!lpPrevPrivilages) {
  186. return GetLastError();
  187. }
  188. if (!OpenProcessToken( GetCurrentProcess(),
  189. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
  190. return GetLastError();
  191. }
  192. LookupPrivilegeValue( NULL, SE_RESTORE_NAME, &luid );
  193. tp.PrivilegeCount = 1;
  194. tp.Privileges[0].Luid = luid;
  195. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  196. if (!AdjustTokenPrivileges( hToken, FALSE, &tp,
  197. dwSize, lpPrevPrivilages, &dwSize )) {
  198. if (GetLastError() == ERROR_MORE_DATA) {
  199. PTOKEN_PRIVILEGES lpTemp;
  200. lpTemp = GlobalReAlloc(lpPrevPrivilages, dwSize, GMEM_MOVEABLE);
  201. if (!lpTemp) {
  202. GlobalFree (lpPrevPrivilages);
  203. return GetLastError();
  204. }
  205. lpPrevPrivilages = lpTemp;
  206. if (!AdjustTokenPrivileges( hToken, FALSE, &tp,
  207. dwSize, lpPrevPrivilages, &dwSize )) {
  208. return GetLastError();
  209. }
  210. } else {
  211. return GetLastError();
  212. }
  213. }
  214. }
  215. //
  216. // Load the hive
  217. //
  218. error = RegLoadKey(hKey, lpSubKey, lpFile);
  219. if (g_bWinnt) {
  220. AdjustTokenPrivileges( hToken, FALSE, lpPrevPrivilages,
  221. 0, NULL, NULL );
  222. CloseHandle (hToken);
  223. }
  224. return error;
  225. }
  226. //*************************************************************
  227. //
  228. // MyRegUnLoadKey()
  229. //
  230. // Purpose: Unloads a registry key
  231. //
  232. // Parameters: hKey - Registry handle
  233. // lpSubKey - Subkey to be unloaded
  234. //
  235. //
  236. // Return: ERROR_SUCCESS if successful
  237. // Error code if not
  238. //
  239. // Comments:
  240. //
  241. // History: Date Author Comment
  242. // 6/19/95 ericflo Ported
  243. //
  244. //*************************************************************
  245. LONG MyRegUnLoadKey(HKEY hKey, LPCTSTR lpSubKey)
  246. {
  247. HANDLE hToken;
  248. LUID luid;
  249. DWORD dwSize = 1024;
  250. PTOKEN_PRIVILEGES lpPrevPrivilages;
  251. TOKEN_PRIVILEGES tp;
  252. LONG error;
  253. if (g_bWinnt) {
  254. //
  255. // Allocate space for the old privileges
  256. //
  257. lpPrevPrivilages = GlobalAlloc(GPTR, dwSize);
  258. if (!lpPrevPrivilages) {
  259. return GetLastError();
  260. }
  261. if (!OpenProcessToken( GetCurrentProcess(),
  262. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
  263. return GetLastError();
  264. }
  265. LookupPrivilegeValue( NULL, SE_RESTORE_NAME, &luid );
  266. tp.PrivilegeCount = 1;
  267. tp.Privileges[0].Luid = luid;
  268. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  269. if (!AdjustTokenPrivileges( hToken, FALSE, &tp,
  270. dwSize, lpPrevPrivilages, &dwSize )) {
  271. if (GetLastError() == ERROR_MORE_DATA) {
  272. PTOKEN_PRIVILEGES lpTemp;
  273. lpTemp = GlobalReAlloc(lpPrevPrivilages, dwSize, GMEM_MOVEABLE);
  274. if (!lpTemp) {
  275. GlobalFree (lpPrevPrivilages);
  276. return GetLastError();
  277. }
  278. lpPrevPrivilages = lpTemp;
  279. if (!AdjustTokenPrivileges( hToken, FALSE, &tp,
  280. dwSize, lpPrevPrivilages, &dwSize )) {
  281. return GetLastError();
  282. }
  283. } else {
  284. return GetLastError();
  285. }
  286. }
  287. }
  288. //
  289. // Unload the hive
  290. //
  291. error = RegUnLoadKey(hKey, lpSubKey);
  292. if (g_bWinnt) {
  293. AdjustTokenPrivileges( hToken, FALSE, lpPrevPrivilages,
  294. 0, NULL, NULL );
  295. CloseHandle (hToken);
  296. }
  297. return error;
  298. }
  299. //*************************************************************
  300. //
  301. // MyRegSaveKey()
  302. //
  303. // Purpose: Saves a registry key
  304. //
  305. // Parameters: hKey - Registry handle
  306. // lpSubKey - Subkey to be unloaded
  307. //
  308. //
  309. // Return: ERROR_SUCCESS if successful
  310. // Error number if not
  311. //
  312. // Comments:
  313. //
  314. // History: Date Author Comment
  315. // 6/19/95 ericflo Ported
  316. //
  317. //*************************************************************
  318. LONG MyRegSaveKey(HKEY hKey, LPCTSTR lpSubKey)
  319. {
  320. HANDLE hToken;
  321. LUID luid;
  322. DWORD dwSize = 1024;
  323. PTOKEN_PRIVILEGES lpPrevPrivilages;
  324. TOKEN_PRIVILEGES tp;
  325. LONG error;
  326. if (g_bWinnt) {
  327. //
  328. // Allocate space for the old privileges
  329. //
  330. lpPrevPrivilages = GlobalAlloc(GPTR, dwSize);
  331. if (!lpPrevPrivilages) {
  332. return GetLastError();
  333. }
  334. if (!OpenProcessToken( GetCurrentProcess(),
  335. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
  336. return GetLastError();
  337. }
  338. LookupPrivilegeValue( NULL, SE_BACKUP_NAME, &luid );
  339. tp.PrivilegeCount = 1;
  340. tp.Privileges[0].Luid = luid;
  341. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  342. if (!AdjustTokenPrivileges( hToken, FALSE, &tp,
  343. dwSize, lpPrevPrivilages, &dwSize )) {
  344. if (GetLastError() == ERROR_MORE_DATA) {
  345. PTOKEN_PRIVILEGES lpTemp;
  346. lpTemp = GlobalReAlloc(lpPrevPrivilages, dwSize, GMEM_MOVEABLE);
  347. if (!lpTemp) {
  348. GlobalFree (lpPrevPrivilages);
  349. return GetLastError();
  350. }
  351. lpPrevPrivilages = lpTemp;
  352. if (!AdjustTokenPrivileges( hToken, FALSE, &tp,
  353. dwSize, lpPrevPrivilages, &dwSize )) {
  354. return GetLastError();
  355. }
  356. } else {
  357. return GetLastError();
  358. }
  359. }
  360. }
  361. //
  362. // Save the hive
  363. //
  364. error = RegSaveKey(hKey, lpSubKey, NULL);
  365. if (g_bWinnt) {
  366. AdjustTokenPrivileges( hToken, FALSE, lpPrevPrivilages,
  367. 0, NULL, NULL );
  368. CloseHandle (hToken);
  369. }
  370. return error;
  371. }
  372. /*******************************************************************
  373. NAME: StringToNum
  374. SYNOPSIS: Converts string value to numeric value
  375. NOTES: Calls atoi() to do conversion, but first checks
  376. for non-numeric characters
  377. EXIT: Returns TRUE if successful, FALSE if invalid
  378. (non-numeric) characters
  379. ********************************************************************/
  380. BOOL StringToNum(TCHAR *pszStr,UINT * pnVal)
  381. {
  382. TCHAR *pTst = pszStr;
  383. if (!pszStr) return FALSE;
  384. // verify that all characters are numbers
  385. while (*pTst) {
  386. if (!(*pTst >= TEXT('0') && *pTst <= TEXT('9'))) {
  387. if (*pTst != TEXT('-'))
  388. return FALSE;
  389. }
  390. pTst = CharNext(pTst);
  391. }
  392. *pnVal = atoi(pszStr);
  393. return TRUE;
  394. }
  395. DWORD ListView_GetItemParm( HWND hwnd, int i )
  396. {
  397. LV_ITEM lvi;
  398. lvi.mask = LVIF_PARAM;
  399. lvi.iItem = i;
  400. lvi.iSubItem = 0;
  401. lvi.state = (UINT)-1;
  402. lvi.lParam = 0L;
  403. ListView_GetItem ( hwnd, &lvi );
  404. return (DWORD)lvi.lParam;
  405. }
  406. VOID DisplayStandardError(HWND hwndOwner,TCHAR * pszParam,UINT uMsgID,UINT uErr)
  407. {
  408. TCHAR szError[256],szMsg[512+MAX_PATH+1],szFmt[256];
  409. if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,
  410. uErr,0,szError,ARRAYSIZE(szError),NULL)) {
  411. // if getting system text fails, make a string a la
  412. // "error <n> occurred"
  413. LoadSz(IDS_ERRFORMAT,szFmt,ARRAYSIZE(szFmt));
  414. wsprintf(szError,szFmt,uErr);
  415. }
  416. LoadSz(uMsgID,szFmt,ARRAYSIZE(szFmt));
  417. if (pszParam) {
  418. wsprintf(szMsg,szFmt,pszParam,szError);
  419. } else {
  420. wsprintf(szMsg,szFmt,szError);
  421. }
  422. MsgBoxSz(hwndOwner,szMsg,MB_ICONEXCLAMATION,MB_OK);
  423. }
  424. #ifdef DEBUG
  425. CHAR szDebugOut[255];
  426. #endif