1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| INT GetJavaHomePathFromRegedit(wchar_t* retwstr) { wchar_t str[MAX_PATH + 1]; INT flag = GetStrFromRegedit(L"SOFTWARE\\JavaSoft\\Java Runtime Environment\\", L"CurrentVersion", str); wprintf(L"%ws\n", str);
WCHAR keyjp[MAX_PATH + 1] = L"\0"; swprintf_s(keyjp, MAX_PATH, L"%ws%ws", L"SOFTWARE\\JavaSoft\\Java Runtime Environment\\", str); wprintf(L"%ws\n", keyjp); flag = GetStrFromRegedit(keyjp, L"JavaHome", str); wprintf(L"%ws\n", str); swprintf_s(retwstr, MAX_PATH, L"%ws%ws", str, L"\\bin\\java.exe"); return TRUE; }
INT GetStrFromRegedit(const wchar_t* hkey, const wchar_t* childkey, wchar_t* str) { HKEY retKey; BYTE retKeyVal[MAX_PATH + 1] = { 0 }; DWORD nSize = MAX_PATH + 1; LONG ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, hkey, 0, KEY_QUERY_VALUE | KEY_READ, &retKey); if (ret != ERROR_SUCCESS) { return -1; } ret = RegQueryValueEx(retKey, childkey, NULL, NULL, (LPBYTE)retKeyVal, &nSize); RegCloseKey(retKey); if (ret != ERROR_SUCCESS) { return -2; } int sum = 0; wchar_t TEMPSTR[MAX_PATH + 1] = L"\0"; for (int i = 0; i < int(nSize); i += 1) { if (retKeyVal[i + 1] == 0) { TEMPSTR[i] = retKeyVal[i]; if (retKeyVal[i] == 0) break; } } for (int i = 0; i < int(nSize); i += 1) { if (retKeyVal[i + 1] == 0) { *(str + sum) = retKeyVal[i]; sum++; if (retKeyVal[i] == 0) break; } } return TRUE; }
|