获取Java路径
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;
}
|