PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/publish/iefix/RegisterOperate.cpp

https://bitbucket.org/knoss/pcmanager/
C++ | 512 lines | 430 code | 74 blank | 8 comment | 145 complexity | 3d66715c6cceec504228c67d547e9f65 MD5 | raw file
Possible License(s): LGPL-2.0
  1. #include "StdAfx.h"
  2. #include "RegisterOperate.h"
  3. using namespace RegisterOperate;
  4. //#include "modifyprivilege/AccessHelper.h"//修改注册表权限
  5. #define STR_BEGIN
  6. #define STR_CLSID_GETFILE_REGPATH TEXT("CLSID\\%s\\InprocServer32")
  7. #define STR_END
  8. CRegisterOperate::CRegisterOperate(void)
  9. {
  10. }
  11. CRegisterOperate::~CRegisterOperate(void)
  12. {
  13. }
  14. int CRegisterOperate::GetRegValue(HKEY hKey, LPCTSTR pszSubPath, LPCTSTR pszName, DWORD& dwValue)
  15. {
  16. int nRet = -1;
  17. HKEY hOpenKey = NULL;
  18. hOpenKey = _OpenKey(hKey, pszSubPath);
  19. if (NULL != hOpenKey)
  20. {
  21. DWORD dwType = REG_DWORD;
  22. DWORD dwSize = sizeof(DWORD);
  23. if (ERROR_SUCCESS == RegQueryValueEx(hOpenKey, pszName, 0,&dwType , (LPBYTE)&dwValue, &dwSize))
  24. nRet = 0;
  25. }
  26. _CloseKey(hOpenKey);
  27. return nRet;
  28. }
  29. int CRegisterOperate::GetRegValue(HKEY hKey, LPCTSTR pszSubPath, LPCTSTR pszName, LPTSTR pszValue, DWORD cbSize)
  30. {
  31. int nRet = -1;
  32. HKEY hOpenKey = NULL;
  33. hOpenKey = _OpenKey(hKey, pszSubPath);
  34. if (NULL != hOpenKey)
  35. {
  36. DWORD dwType = REG_SZ;
  37. DWORD dwSize = 0;
  38. // SHGetValue(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Internet Explorer\\Main"), TEXT("Start Page"), &dwType, (LPVOID)pszValue, &cbSize);
  39. if (ERROR_SUCCESS != RegQueryValueEx(hOpenKey, pszName, 0, &dwType , (LPBYTE)pszValue, &dwSize))
  40. {
  41. if (REG_MULTI_SZ == dwType)
  42. {//多字节解析
  43. if (dwSize > cbSize)
  44. {
  45. TCHAR* pszBuf = new TCHAR[dwSize+1];
  46. ZeroMemory(pszBuf, dwSize+1);
  47. nRet = _GetValue_Mul_SZ(hOpenKey, pszName, pszBuf, dwType, dwSize);
  48. SAFE_DELETE_PTR(pszBuf);
  49. }
  50. else
  51. {
  52. nRet = _GetValue_Mul_SZ(hOpenKey, pszName, pszValue, dwType, cbSize);
  53. }
  54. }
  55. else
  56. {//解析非多字节字符
  57. nRet = _GetValue(hOpenKey, pszName, pszValue, dwType, cbSize, dwSize);
  58. }
  59. // nRet = 0;
  60. }
  61. }
  62. _CloseKey(hOpenKey);
  63. return nRet;
  64. }
  65. HKEY CRegisterOperate::_OpenKey(HKEY hKey, LPCTSTR pszSubPath)
  66. {
  67. HKEY hRetkey = NULL;
  68. if (ERROR_SUCCESS == RegOpenKeyEx(hKey, pszSubPath, 0, KEY_READ|KEY_WRITE, &hRetkey))
  69. {
  70. return hRetkey;
  71. }
  72. else
  73. {
  74. CAccessHepler addAccess;
  75. addAccess.AddRegAccess(hKey, pszSubPath, KEY_READ|KEY_WRITE, TRUE);//提权
  76. if (ERROR_SUCCESS == RegOpenKeyEx(hKey, pszSubPath, 0, KEY_READ|KEY_WRITE, &hRetkey))
  77. {
  78. return hRetkey;
  79. }
  80. }
  81. int nError = GetLastError();
  82. return hRetkey;
  83. }
  84. void CRegisterOperate::_CloseKey(HKEY hKey)
  85. {
  86. if (NULL != hKey)
  87. RegCloseKey(hKey);
  88. hKey = NULL;
  89. }
  90. int CRegisterOperate::_GetValue(HKEY hKey, LPCTSTR pszName, LPTSTR pszValue, DWORD dwType, DWORD cbSize, DWORD cbSize2)
  91. {
  92. int nRet = -1;
  93. if (cbSize <= cbSize2)
  94. {
  95. TCHAR* pszBuf = new TCHAR[cbSize2+1];
  96. ZeroMemory(pszBuf, cbSize2+1);
  97. if (ERROR_SUCCESS == RegQueryValueEx(hKey, pszName, 0, &dwType, (LPBYTE)pszBuf, &cbSize2))
  98. {
  99. StringCbCopyN(pszValue, cbSize, pszBuf, cbSize-1);
  100. nRet = 0;
  101. }
  102. SAFE_DELETE_PTR(pszBuf);
  103. }
  104. else
  105. {
  106. if (ERROR_SUCCESS == RegQueryValueEx(hKey, pszName, 0, &dwType, (LPBYTE)pszValue, &cbSize))
  107. {
  108. nRet = 0;
  109. }
  110. }
  111. return nRet;
  112. }
  113. int CRegisterOperate::_GetValue_Mul_SZ(HKEY hKey, LPCTSTR pszName, LPTSTR pszValue, DWORD dwType, DWORD cbSize)
  114. {
  115. int nRet = -1;
  116. CAtlArray<CString> arrItemStr;
  117. DWORD dwSize = cbSize;
  118. if (ERROR_SUCCESS == RegQueryValueEx(hKey, pszName, 0, &dwType, (LPBYTE)pszValue, &dwSize))
  119. {
  120. _ParseMulSzBuf(pszValue, arrItemStr);
  121. ZeroMemory(pszValue, cbSize);
  122. nRet = 0;
  123. }
  124. if (0 == nRet)
  125. {//这里解析多字节字符成功,那么把解析成功的字符合并成字符,中间用空格隔开
  126. size_t tCount = arrItemStr.GetCount();
  127. CString strInfo;
  128. for (size_t i = 0; i < tCount; i++)
  129. {
  130. strInfo += arrItemStr[i];
  131. strInfo += TEXT(" ");
  132. }
  133. StringCbCopyN(pszValue, cbSize, strInfo, cbSize);
  134. }
  135. return nRet;
  136. }
  137. int CRegisterOperate::_ParseMulSzBuf(LPCTSTR pszBuf, CAtlArray<CString>& arrStr)
  138. {
  139. int nRet = -1;
  140. arrStr.RemoveAll();
  141. if (NULL == pszBuf)
  142. return nRet;
  143. while (0 != _tcslen(pszBuf))
  144. {
  145. TCHAR pszValue[MAX_PATH] = {0};
  146. size_t nValue = (_tcslen(pszBuf)+1)*sizeof(TCHAR);
  147. StringCbCopyN(pszValue, sizeof(pszValue), pszBuf, nValue);
  148. nValue /= sizeof(TCHAR);
  149. pszBuf += nValue;
  150. arrStr.Add(CString(pszValue));
  151. }
  152. nRet = 0;
  153. return nRet;
  154. }
  155. int CRegisterOperate::GetRegValue(HKEY hKey, LPCTSTR pszSubPath, LPCTSTR pszName, CAtlArray<CString>& arrString)
  156. {
  157. int nRet = -1;
  158. HKEY hOpenKey = NULL;
  159. hOpenKey = _OpenKey(hKey, pszSubPath);
  160. DWORD dwSize = 0;
  161. DWORD dwType = REG_MULTI_SZ;
  162. if (NULL != hOpenKey)
  163. {
  164. //先通过调用它来获得需要的缓冲区的长度
  165. RegQueryValueEx(hOpenKey, pszName, 0,&dwType , NULL, &dwSize);
  166. if (dwSize > 0)
  167. {
  168. TCHAR* pszValue = new TCHAR[dwSize+1];
  169. ZeroMemory(pszValue, dwSize+1);
  170. if (ERROR_SUCCESS == RegQueryValueEx(hOpenKey, pszName, 0, &dwType, (LPBYTE)pszValue, &dwSize))
  171. {
  172. nRet = _ParseMulSzBuf(pszValue, arrString);
  173. }
  174. SAFE_DELETE_PTR(pszValue);
  175. }
  176. }
  177. _CloseKey(hOpenKey);
  178. return nRet;
  179. }
  180. DWORD CRegisterOperate::GetRegType(HKEY hKey, LPCTSTR pszSubPath, LPCTSTR pszName)
  181. {
  182. DWORD dwType = 0;
  183. HKEY hOpenKey = NULL;
  184. hOpenKey = _OpenKey(hKey, pszSubPath);
  185. if (NULL != hOpenKey)
  186. {
  187. DWORD dwSize = 0;
  188. if (ERROR_SUCCESS != RegQueryValueEx(hOpenKey, pszName, 0,&dwType , NULL, &dwSize))
  189. {
  190. _CloseKey(hOpenKey);
  191. return dwType;
  192. }
  193. }
  194. _CloseKey(hOpenKey);
  195. return dwType;
  196. }
  197. int CRegisterOperate::EnumSubKey(HKEY hKey, LPCTSTR pszSubPath, CAtlArray<CString>& arrKey)
  198. {
  199. int nRet = -1;
  200. if (NULL == pszSubPath)
  201. return nRet;
  202. HKEY hOpenKey = NULL;
  203. arrKey.RemoveAll();
  204. hOpenKey = _OpenKey(hKey, pszSubPath);
  205. if (NULL == hOpenKey)
  206. return nRet;
  207. DWORD dwIndex = 0;
  208. TCHAR pszName[FILEPATH_MAX_LEN] = {0};
  209. DWORD dwRetSize = 0;
  210. do
  211. {
  212. ZEROMEM(pszName);
  213. dwRetSize = sizeof(pszName);
  214. if (ERROR_SUCCESS != RegEnumKeyEx(hOpenKey, dwIndex, pszName, &dwRetSize, 0, NULL, 0, 0))
  215. dwRetSize = 0;
  216. if (dwRetSize > 0)
  217. {
  218. arrKey.Add(CString(pszName));
  219. ++dwIndex;
  220. }
  221. } while(dwRetSize);
  222. nRet = 0;
  223. _CloseKey(hOpenKey);
  224. return nRet;
  225. }
  226. int CRegisterOperate::EnumCLSIDAndFile(HKEY hKEY, LPCTSTR pszSubPath, CAtlArray<CLSID_STRUCT>& arrClsid)
  227. {
  228. int nRet = -1;
  229. CAtlArray<CString> arrSubKey;
  230. //这里枚举出所有的CLSID
  231. if (EnumSubKey(hKEY, pszSubPath, arrSubKey) != 0)
  232. return nRet;
  233. size_t tCount = arrSubKey.GetCount();
  234. CLSID_STRUCT clsidStruct;
  235. TCHAR _pszFullSubPath[REGPATH_MAX_LEN] = {0};
  236. for (size_t t = 0; t < tCount; t++)
  237. {
  238. ZEROMEM(&clsidStruct);
  239. clsidStruct.cbSize = sizeof(clsidStruct);
  240. STRPRINF_STR(clsidStruct._pszCLSID, arrSubKey[t]);//获取CLSID
  241. StringCbPrintf(clsidStruct._pszFullRegPath, sizeof(clsidStruct._pszFullRegPath),
  242. TEXT("%s\\%s"), pszSubPath, clsidStruct._pszCLSID);//拼接出全注册表路径
  243. //拼接文件路径所在的注册表路径,HKEY_CLASSES_ROOT\CLSID\{0000002F-0000-0000-C000-000000000046}\InprocServer32
  244. STRPRINF(clsidStruct._pszFileRegPath, STR_CLSID_GETFILE_REGPATH, clsidStruct._pszCLSID);
  245. GetRegValue(HKEY_CLASSES_ROOT, clsidStruct._pszFileRegPath, TEXT(""), clsidStruct._pszFileFullPath, sizeof(clsidStruct._pszFileFullPath));//获取文件路径
  246. //--增加到列表中
  247. arrClsid.Add(clsidStruct);
  248. }
  249. nRet = 0;
  250. return nRet;
  251. }
  252. int CRegisterOperate::SetRegValue(HKEY hKey, LPCTSTR pszSubPath, LPCTSTR pszName, LPCTSTR pszValue, DWORD cbSize)
  253. {
  254. int nRet = -1;
  255. if (NULL == hKey || NULL == pszSubPath || NULL == pszName || NULL == pszValue)
  256. return nRet;
  257. HKEY hOpenKey = NULL;
  258. hOpenKey = _OpenKey(hKey, pszSubPath);
  259. if (NULL == hOpenKey)
  260. return nRet;
  261. if (ERROR_SUCCESS == RegSetValueEx(hOpenKey, pszName, 0, REG_SZ, (LPBYTE)pszValue, cbSize))
  262. {
  263. nRet = 0;
  264. }
  265. else
  266. {
  267. CAccessHepler().AddRegAccess(hKey, pszSubPath, KEY_ALL_ACCESS);
  268. if (ERROR_SUCCESS == RegSetValueEx(hOpenKey, pszName, 0, REG_SZ, (LPBYTE)pszValue, cbSize))
  269. nRet = 0;
  270. }
  271. _CloseKey(hOpenKey);
  272. return nRet;
  273. }
  274. int CRegisterOperate::SetRegValue(HKEY hKey, LPCTSTR pszSubPath, LPCTSTR pszName, DWORD dwValue)
  275. {
  276. int nRet = -1;
  277. if (NULL == hKey || NULL == pszSubPath || NULL == pszName)
  278. return nRet;
  279. HKEY hOpenKey = NULL;
  280. hOpenKey = _OpenKey(hKey, pszSubPath);
  281. if (NULL == hOpenKey)
  282. return nRet;
  283. if (ERROR_SUCCESS == RegSetValueEx(hOpenKey, pszName, 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(DWORD)))
  284. {
  285. nRet = 0;
  286. }else
  287. {
  288. CAccessHepler().AddRegAccess(hKey, pszSubPath, KEY_ALL_ACCESS);
  289. if (ERROR_SUCCESS == RegSetValueEx(hOpenKey, pszName, 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(DWORD)))
  290. nRet = 0;
  291. }
  292. _CloseKey(hOpenKey);
  293. return nRet;
  294. }
  295. int CRegisterOperate::DeleteRegValue(HKEY hKey, LPCTSTR pszSubPath, LPCTSTR pszName)
  296. {
  297. int nRet = -1;
  298. if (NULL == hKey || NULL == pszSubPath || NULL == pszName)
  299. return nRet;
  300. HKEY hOpenKey = NULL;
  301. hOpenKey = _OpenKey(hKey, pszSubPath);
  302. if (NULL == hOpenKey)
  303. return nRet;
  304. if (ERROR_SUCCESS == RegDeleteValue(hOpenKey, pszName))
  305. {
  306. nRet = 0;
  307. }
  308. else
  309. {
  310. CAccessHepler().AddRegAccess(hKey, pszSubPath, KEY_ALL_ACCESS);
  311. if (ERROR_SUCCESS == RegDeleteValue(hOpenKey, pszName))
  312. nRet = 0;
  313. }
  314. _CloseKey(hOpenKey);
  315. return nRet;
  316. }
  317. int CRegisterOperate::DeleteAnKey(HKEY hKey, LPCTSTR pszSubPath)
  318. {
  319. int nRet = -1;
  320. if (NULL == hKey || NULL == pszSubPath)
  321. return nRet;
  322. HKEY hOpenKey = NULL;
  323. hOpenKey = _OpenKey(hKey, NULL);
  324. if (NULL == hOpenKey)
  325. return nRet;
  326. #if 0
  327. //Requires Windows Vista or Windows XP Professional x64 Edition.
  328. if (ERROR_SUCCESS == RegDeleteKeyEx(hOpenKey, pszSubPath,KEY_WOW64_32KEY|KEY_WOW64_64KEY, 0))
  329. nRet;
  330. #else
  331. if (ERROR_SUCCESS == SHDeleteKey(hKey, pszSubPath))
  332. nRet = 0;
  333. else
  334. {
  335. CAccessHepler().AddRegAccess(hKey, pszSubPath, KEY_ALL_ACCESS);
  336. if (ERROR_SUCCESS == SHDeleteKey(hKey, pszSubPath))
  337. nRet = 0;
  338. }
  339. #endif
  340. _CloseKey(hOpenKey);
  341. return nRet ;
  342. }
  343. int CRegisterOperate::KeyIsExist(HKEY hKey, LPCTSTR pszSubPath)
  344. {
  345. int nRet = -1;
  346. HKEY hOpenKey = _OpenKey(hKey, pszSubPath);
  347. if (NULL != hOpenKey)
  348. nRet = 0;
  349. _CloseKey(hOpenKey);
  350. return nRet;
  351. }
  352. int CRegisterOperate::EnumRegValue(HKEY hKey, LPCTSTR pszSubPath, CAtlArray<CString>& arrRegValue)
  353. {
  354. int nRet = -1;
  355. if (NULL == hKey || NULL == pszSubPath)
  356. return nRet;
  357. HKEY hOpenKey = _OpenKey(hKey, pszSubPath);
  358. if (NULL == hOpenKey)
  359. return nRet;
  360. arrRegValue.RemoveAll();
  361. int nIndex = 0;
  362. TCHAR pszRegValue[REGPATH_MAX_LEN] = {0};
  363. DWORD dwSize = sizeof(pszRegValue);
  364. DWORD dwType = REG_SZ;
  365. while(ERROR_SUCCESS == RegEnumValue(hOpenKey, nIndex, pszRegValue, &dwSize, 0, &dwType ,NULL, NULL))
  366. {
  367. arrRegValue.Add(pszRegValue);
  368. ++nIndex;
  369. dwSize = sizeof(pszRegValue);
  370. }
  371. nRet = 0;
  372. _CloseKey(hOpenKey);
  373. return nRet;
  374. }
  375. int CRegisterOperate::CreateAnKey(HKEY hKey, LPCTSTR pszSubPath, HKEY &hNewKey)
  376. {
  377. int nRet = -1;
  378. if (NULL == hKey || NULL == pszSubPath)
  379. return nRet;
  380. if (ERROR_SUCCESS == RegCreateKeyEx(hKey,pszSubPath, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE,NULL, &hNewKey,NULL))
  381. {
  382. nRet = 0;
  383. }
  384. else
  385. {
  386. CAccessHepler().AddRegAccess(hKey, pszSubPath, KEY_ALL_ACCESS);
  387. if (ERROR_SUCCESS == RegCreateKeyEx(hKey,pszSubPath, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE,NULL, &hNewKey,NULL))
  388. nRet = 0;
  389. }
  390. return nRet;
  391. }
  392. int CRegisterOperate::SetRegValue2(HKEY hKey, LPCTSTR pszSubPath, LPCTSTR pszName, LPCTSTR pszValue, DWORD cbSize)
  393. {
  394. int nRet = -1;
  395. if (NULL == hKey || NULL == pszSubPath || NULL == pszName || NULL == pszValue)
  396. return nRet;
  397. HKEY hOpenKey = NULL;
  398. hOpenKey = _OpenKey(hKey, pszSubPath);
  399. if (NULL == hOpenKey)
  400. return nRet;
  401. if (ERROR_SUCCESS == RegSetValueEx(hOpenKey, pszName, 0, REG_EXPAND_SZ, (LPBYTE)pszValue, cbSize))
  402. {
  403. nRet = 0;
  404. }
  405. _CloseKey(hOpenKey);
  406. return nRet;
  407. }
  408. int CRegisterOperate::GetBinaryRegValue(HKEY hKey, LPCTSTR pszSubPath, LPCTSTR pszName, LPBYTE pByteValue, DWORD cbSize)
  409. {
  410. int nRet = -1;
  411. if (NULL == hKey || NULL == pszSubPath)
  412. return nRet;
  413. HKEY hOpenKey = _OpenKey(hKey, pszSubPath);
  414. if (NULL == hOpenKey)
  415. return nRet;
  416. DWORD dwType = REG_BINARY;
  417. if (ERROR_SUCCESS == RegQueryValueEx(hOpenKey, pszName, 0, &dwType, pByteValue, &cbSize))
  418. {
  419. nRet = 0;
  420. }
  421. _CloseKey(hOpenKey);
  422. return nRet;
  423. }
  424. int CRegisterOperate::SetBinaryRegValue(HKEY hKey, LPCTSTR pszSubPath, LPCTSTR pszName, LPBYTE pByteValue, DWORD cbSize)
  425. {
  426. int nRet = -1;
  427. if (NULL == hKey || NULL == pszSubPath)
  428. return nRet;
  429. HKEY hOpenKey = _OpenKey(hKey, pszSubPath);
  430. if (NULL == hOpenKey)
  431. return nRet;
  432. if (ERROR_SUCCESS == RegSetValueEx(hOpenKey, pszName, 0, REG_BINARY, pByteValue, cbSize))
  433. {
  434. nRet = 0;
  435. }
  436. _CloseKey(hOpenKey);
  437. return nRet;
  438. }