mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +00:00
Fix microsoft_craziness.h
This commit is contained in:
+90
-97
@@ -398,6 +398,7 @@ bool find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result *res
|
|||||||
// If all this COM object instantiation, enumeration, and querying doesn't give us
|
// If all this COM object instantiation, enumeration, and querying doesn't give us
|
||||||
// a useful result, we drop back to the registry-checking method.
|
// a useful result, we drop back to the registry-checking method.
|
||||||
|
|
||||||
|
|
||||||
auto rc = CoInitialize(NULL);
|
auto rc = CoInitialize(NULL);
|
||||||
// "Subsequent valid calls return false." So ignore false.
|
// "Subsequent valid calls return false." So ignore false.
|
||||||
if (rc != S_OK) return false;
|
if (rc != S_OK) return false;
|
||||||
@@ -408,123 +409,123 @@ bool find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result *res
|
|||||||
ISetupConfiguration *config = NULL;
|
ISetupConfiguration *config = NULL;
|
||||||
HRESULT hr = 0;
|
HRESULT hr = 0;
|
||||||
hr = CoCreateInstance(CLSID_SetupConfiguration, NULL, CLSCTX_INPROC_SERVER, my_uid, (void **)&config);
|
hr = CoCreateInstance(CLSID_SetupConfiguration, NULL, CLSCTX_INPROC_SERVER, my_uid, (void **)&config);
|
||||||
if (hr != 0) return false;
|
if (hr == 0) {
|
||||||
defer (config->Release());
|
defer (config->Release());
|
||||||
|
|
||||||
IEnumSetupInstances *instances = NULL;
|
IEnumSetupInstances *instances = NULL;
|
||||||
hr = config->EnumInstances(&instances);
|
hr = config->EnumInstances(&instances);
|
||||||
if (hr != 0) return false;
|
if (hr != 0) return false;
|
||||||
if (!instances) return false;
|
if (!instances) return false;
|
||||||
defer (instances->Release());
|
defer (instances->Release());
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
ULONG found = 0;
|
||||||
|
ISetupInstance *instance = NULL;
|
||||||
|
auto hr = instances->Next(1, &instance, &found);
|
||||||
|
if (hr != S_OK) break;
|
||||||
|
|
||||||
for (;;) {
|
defer (instance->Release());
|
||||||
ULONG found = 0;
|
|
||||||
ISetupInstance *instance = NULL;
|
|
||||||
auto hr = instances->Next(1, &instance, &found);
|
|
||||||
if (hr != S_OK) break;
|
|
||||||
|
|
||||||
defer (instance->Release());
|
BSTR bstr_inst_path;
|
||||||
|
hr = instance->GetInstallationPath(&bstr_inst_path);
|
||||||
|
if (hr != S_OK) continue;
|
||||||
|
defer (SysFreeString(bstr_inst_path));
|
||||||
|
|
||||||
BSTR bstr_inst_path;
|
auto tools_filename = concat(bstr_inst_path, L"\\VC\\Auxiliary\\Build\\Microsoft.VCToolsVersion.default.txt");
|
||||||
hr = instance->GetInstallationPath(&bstr_inst_path);
|
defer (free(tools_filename));
|
||||||
if (hr != S_OK) continue;
|
|
||||||
defer (SysFreeString(bstr_inst_path));
|
|
||||||
|
|
||||||
auto tools_filename = concat(bstr_inst_path, L"\\VC\\Auxiliary\\Build\\Microsoft.VCToolsVersion.default.txt");
|
FILE *f = nullptr;
|
||||||
defer (free(tools_filename));
|
auto open_result = _wfopen_s(&f, tools_filename, L"rt");
|
||||||
|
if (open_result != 0) continue;
|
||||||
|
if (!f) continue;
|
||||||
|
defer (fclose(f));
|
||||||
|
|
||||||
FILE *f = nullptr;
|
LARGE_INTEGER tools_file_size;
|
||||||
auto open_result = _wfopen_s(&f, tools_filename, L"rt");
|
auto file_handle = (HANDLE)_get_osfhandle(_fileno(f));
|
||||||
if (open_result != 0) continue;
|
BOOL success = GetFileSizeEx(file_handle, &tools_file_size);
|
||||||
if (!f) continue;
|
if (!success) continue;
|
||||||
defer (fclose(f));
|
|
||||||
|
|
||||||
LARGE_INTEGER tools_file_size;
|
auto version_bytes = (tools_file_size.QuadPart + 1) * 2; // Warning: This multiplication by 2 presumes there is no variable-length encoding in the wchars (wacky characters in the file could betray this expectation).
|
||||||
auto file_handle = (HANDLE)_get_osfhandle(_fileno(f));
|
if (version_bytes > 0x7FFFFFFF) continue; // Avoid overflow.
|
||||||
BOOL success = GetFileSizeEx(file_handle, &tools_file_size);
|
|
||||||
if (!success) continue;
|
|
||||||
|
|
||||||
auto version_bytes = (tools_file_size.QuadPart + 1) * 2; // Warning: This multiplication by 2 presumes there is no variable-length encoding in the wchars (wacky characters in the file could betray this expectation).
|
wchar_t *version = (wchar_t *)malloc(version_bytes);
|
||||||
if (version_bytes > 0x7FFFFFFF) continue; // Avoid overflow.
|
defer (free(version));
|
||||||
|
|
||||||
wchar_t *version = (wchar_t *)malloc(version_bytes);
|
auto read_result = fgetws(version, (int)version_bytes, f);
|
||||||
defer (free(version));
|
if (!read_result) continue;
|
||||||
|
|
||||||
auto read_result = fgetws(version, (int)version_bytes, f);
|
auto version_tail = wcschr(version, '\n');
|
||||||
if (!read_result) continue;
|
if (version_tail) *version_tail = 0; // Stomp the data, because nobody cares about it.
|
||||||
|
|
||||||
auto version_tail = wcschr(version, '\n');
|
auto library_path = concat(bstr_inst_path, L"\\VC\\Tools\\MSVC\\", version, L"\\lib\\x64\\");
|
||||||
if (version_tail) *version_tail = 0; // Stomp the data, because nobody cares about it.
|
auto library_file = concat(library_path, L"vcruntime.lib"); // @Speed: Could have library_path point to this string, with a smaller count, to save on memory flailing!
|
||||||
|
|
||||||
auto library_path = concat(bstr_inst_path, L"\\VC\\Tools\\MSVC\\", version, L"\\lib\\x64\\");
|
if (os_file_exists(library_file)) {
|
||||||
auto library_file = concat(library_path, L"vcruntime.lib"); // @Speed: Could have library_path point to this string, with a smaller count, to save on memory flailing!
|
auto link_exe_path = concat(bstr_inst_path, L"\\VC\\Tools\\MSVC\\", version, L"\\bin\\Hostx64\\x64\\");
|
||||||
|
result->vs_exe_path = link_exe_path;
|
||||||
|
result->vs_library_path = library_path;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (os_file_exists(library_file)) {
|
/*
|
||||||
auto link_exe_path = concat(bstr_inst_path, L"\\VC\\Tools\\MSVC\\", version, L"\\bin\\Hostx64\\x64\\");
|
Ryan Saunderson said:
|
||||||
result->vs_exe_path = link_exe_path;
|
"Clang uses the 'SetupInstance->GetInstallationVersion' / ISetupHelper->ParseVersion to find the newest version
|
||||||
result->vs_library_path = library_path;
|
and then reads the tools file to define the tools path - which is definitely better than what i did."
|
||||||
return true;
|
|
||||||
|
So... @Incomplete: Should probably pick the newest version...
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Ryan Saunderson said:
|
|
||||||
"Clang uses the 'SetupInstance->GetInstallationVersion' / ISetupHelper->ParseVersion to find the newest version
|
|
||||||
and then reads the tools file to define the tools path - which is definitely better than what i did."
|
|
||||||
|
|
||||||
So... @Incomplete: Should probably pick the newest version...
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we get here, we didn't find Visual Studio 2017. Try earlier versions.
|
// If we get here, we didn't find Visual Studio 2017. Try earlier versions.
|
||||||
|
{
|
||||||
|
HKEY vs7_key;
|
||||||
|
rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7", 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY, &vs7_key);
|
||||||
|
if (rc != S_OK) return false;
|
||||||
|
defer (RegCloseKey(vs7_key));
|
||||||
|
|
||||||
HKEY vs7_key;
|
// Hardcoded search for 4 prior Visual Studio versions. Is there something better to do here?
|
||||||
rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7", 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY, &vs7_key);
|
wchar_t *versions[] = { L"14.0", L"13.0", L"12.0", L"11.0", L"10.0", L"9.0", };
|
||||||
|
const int NUM_VERSIONS = sizeof(versions) / sizeof(versions[0]);
|
||||||
|
|
||||||
if (rc != S_OK) return false;
|
for (int i = 0; i < NUM_VERSIONS; i++) {
|
||||||
defer (RegCloseKey(vs7_key));
|
wchar_t *v = versions[i];
|
||||||
|
|
||||||
// Hardcoded search for 4 prior Visual Studio versions. Is there something better to do here?
|
DWORD dw_type;
|
||||||
wchar_t *versions[] = { L"14.0", L"12.0", L"11.0", L"10.0" };
|
DWORD cb_data;
|
||||||
const int NUM_VERSIONS = sizeof(versions) / sizeof(versions[0]);
|
|
||||||
|
|
||||||
for (int i = 0; i < NUM_VERSIONS; i++) {
|
auto rc = RegQueryValueExW(vs7_key, v, NULL, &dw_type, NULL, &cb_data);
|
||||||
auto v = versions[i];
|
if ((rc == ERROR_FILE_NOT_FOUND) || (dw_type != REG_SZ)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
DWORD dw_type;
|
auto buffer = (wchar_t *)malloc(cb_data);
|
||||||
DWORD cb_data;
|
if (!buffer) return false;
|
||||||
|
defer (free(buffer));
|
||||||
|
|
||||||
auto rc = RegQueryValueExW(vs7_key, v, NULL, &dw_type, NULL, &cb_data);
|
rc = RegQueryValueExW(vs7_key, v, NULL, NULL, (LPBYTE)buffer, &cb_data);
|
||||||
if ((rc == ERROR_FILE_NOT_FOUND) || (dw_type != REG_SZ)) {
|
if (rc != 0) continue;
|
||||||
continue;
|
|
||||||
|
// @Robustness: Do the zero-termination thing suggested in the RegQueryValue docs?
|
||||||
|
|
||||||
|
auto lib_path = concat(buffer, L"VC\\Lib\\amd64\\");
|
||||||
|
|
||||||
|
// Check to see whether a vcruntime.lib actually exists here.
|
||||||
|
auto vcruntime_filename = concat(lib_path, L"vcruntime.lib");
|
||||||
|
defer (free(vcruntime_filename));
|
||||||
|
|
||||||
|
if (os_file_exists(vcruntime_filename)) {
|
||||||
|
result->vs_exe_path = concat(buffer, L"VC\\bin\\");
|
||||||
|
result->vs_library_path = lib_path;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(lib_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto buffer = (wchar_t *)malloc(cb_data);
|
// If we get here, we failed to find anything.
|
||||||
if (!buffer) return false;
|
|
||||||
defer (free(buffer));
|
|
||||||
|
|
||||||
rc = RegQueryValueExW(vs7_key, v, NULL, NULL, (LPBYTE)buffer, &cb_data);
|
|
||||||
if (rc != 0) continue;
|
|
||||||
|
|
||||||
// @Robustness: Do the zero-termination thing suggested in the RegQueryValue docs?
|
|
||||||
|
|
||||||
auto lib_path = concat(buffer, L"VC\\Lib\\amd64");
|
|
||||||
|
|
||||||
// Check to see whether a vcruntime.lib actually exists here.
|
|
||||||
auto vcruntime_filename = concat(lib_path, L"\\vcruntime.lib");
|
|
||||||
defer (free(vcruntime_filename));
|
|
||||||
|
|
||||||
if (os_file_exists(vcruntime_filename)) {
|
|
||||||
result->vs_exe_path = concat(buffer, L"VC\\bin");
|
|
||||||
result->vs_library_path = lib_path;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(lib_path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we get here, we failed to find anything.
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -568,13 +569,5 @@ Find_Result_Utf8 find_visual_studio_and_windows_sdk_utf8() {
|
|||||||
r.vs_exe_path = mc_wstring_to_string(result.vs_exe_path);
|
r.vs_exe_path = mc_wstring_to_string(result.vs_exe_path);
|
||||||
r.vs_library_path = mc_wstring_to_string(result.vs_library_path);
|
r.vs_library_path = mc_wstring_to_string(result.vs_library_path);
|
||||||
|
|
||||||
// gb_printf("%.*s\n", LIT(r.windows_sdk_root));
|
|
||||||
// gb_printf("%.*s\n", LIT(r.windows_sdk_um_library_path));
|
|
||||||
// gb_printf("%.*s\n", LIT(r.windows_sdk_ucrt_library_path));
|
|
||||||
// gb_printf("%.*s\n", LIT(r.vs_exe_path));
|
|
||||||
// gb_printf("%.*s\n", LIT(r.vs_library_path));
|
|
||||||
|
|
||||||
// gb_exit(1);
|
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user