|
| 1 | +#include <windows.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <direct.h> |
| 4 | +#define GetCurrentDir _getcwd |
| 5 | + |
| 6 | +#include <iostream> |
| 7 | + |
| 8 | +#define MAX_REG_SIZE 0x7FFF |
| 9 | +void waitForUser(void); |
| 10 | + |
| 11 | +int main () { |
| 12 | + HKEY hkey; |
| 13 | + DWORD dwDisposition; |
| 14 | + DWORD path_len = MAX_REG_SIZE; |
| 15 | + DWORD path_type = REG_SZ; |
| 16 | + DWORD len; |
| 17 | + |
| 18 | + std::wstring path_str; |
| 19 | + std::string::size_type found_len; |
| 20 | + std::string::size_type found; |
| 21 | + |
| 22 | + wchar_t toolchain_path_w[FILENAME_MAX]; |
| 23 | + wchar_t toolchain_bin_w[FILENAME_MAX]; |
| 24 | + wchar_t prev_path[MAX_REG_SIZE]; |
| 25 | + |
| 26 | + if (!GetCurrentDirectoryW(sizeof(toolchain_path_w), toolchain_path_w)) { |
| 27 | + goto err; |
| 28 | + } |
| 29 | + |
| 30 | + toolchain_path_w[lstrlenW(toolchain_path_w)] = '\0'; |
| 31 | + |
| 32 | + wcscpy(toolchain_bin_w, toolchain_path_w); |
| 33 | + wcscat(toolchain_bin_w, L"\\bin"); |
| 34 | + printf("CEDEV Varible: %ls\n", toolchain_path_w); |
| 35 | + printf("PATH Variable: %ls\n", toolchain_bin_w); |
| 36 | + |
| 37 | + // Get the current key value if it exists |
| 38 | + if (RegOpenKeyW(HKEY_CURRENT_USER, L"Environment", &hkey) != ERROR_SUCCESS) { |
| 39 | + goto err; |
| 40 | + } |
| 41 | + |
| 42 | + RegQueryValueExW(hkey, L"CEDEV", NULL, &path_type, (LPBYTE)&prev_path, &path_len); |
| 43 | + RegCloseKey(hkey); |
| 44 | + |
| 45 | + // open the user enviornment key to add the CEDEV variable |
| 46 | + if (RegCreateKeyExW(HKEY_CURRENT_USER, L"Environment", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey, &dwDisposition) != ERROR_SUCCESS) { |
| 47 | + goto err; |
| 48 | + } |
| 49 | + |
| 50 | + len = lstrlenW(toolchain_path_w) * sizeof(wchar_t) + 1; |
| 51 | + if (RegSetValueExW(hkey, L"CEDEV", 0, REG_SZ, (LPBYTE)toolchain_path_w, len) != ERROR_SUCCESS) { |
| 52 | + goto err; |
| 53 | + } |
| 54 | + RegCloseKey(hkey); |
| 55 | + |
| 56 | + // open the system path enviornment key to add the CEdev\bin path |
| 57 | + if (RegOpenKeyW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", &hkey) != ERROR_SUCCESS) { |
| 58 | + goto err; |
| 59 | + } |
| 60 | + |
| 61 | + wchar_t path[MAX_REG_SIZE]; |
| 62 | + path_len = MAX_REG_SIZE; |
| 63 | + path_type = REG_SZ; |
| 64 | + RegQueryValueExW(hkey, L"Path", NULL, &path_type, (LPBYTE)&path, &path_len); |
| 65 | + |
| 66 | + path_str.assign(path); |
| 67 | + |
| 68 | + // delete the old path if there is one |
| 69 | + if (lstrlenW(prev_path)) { |
| 70 | + // organize the new path |
| 71 | + wcscat(prev_path, L"\\bin"); |
| 72 | + |
| 73 | + printf("\nRemoving previous location...\n"); |
| 74 | + found_len = lstrlenW(prev_path); |
| 75 | + found = path_str.find(prev_path); |
| 76 | + |
| 77 | + // remove the old path if needed |
| 78 | + if (found != std::string::npos) { |
| 79 | + if(path_str.length() != found + found_len && path_str.at(found + found_len) == ';') { |
| 80 | + found_len++; |
| 81 | + } |
| 82 | + if (found && path_str.at(found-1) == ';') { |
| 83 | + found--; |
| 84 | + found_len++; |
| 85 | + } |
| 86 | + path_str.erase(found, found_len); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // append the new path |
| 91 | + path_str.append(L";"); |
| 92 | + path_str.append(toolchain_bin_w); |
| 93 | + |
| 94 | + len = path_str.length() * sizeof(wchar_t) + 1; |
| 95 | + if (RegSetValueExW(hkey, L"Path", 0, REG_SZ, (LPBYTE)path_str.c_str(), len) != ERROR_SUCCESS) { |
| 96 | + goto err; |
| 97 | + } |
| 98 | + |
| 99 | + RegCloseKey(hkey); |
| 100 | + std::cout << std::endl << "Success!"; |
| 101 | + waitForUser(); |
| 102 | + return 0; |
| 103 | + |
| 104 | +err: |
| 105 | + printf("Errors were encountered. Please check rights or set up manually.\n"); |
| 106 | + waitForUser(); |
| 107 | + return errno; |
| 108 | +} |
| 109 | + |
| 110 | +void waitForUser(void) { |
| 111 | + std::cout << std::endl << "Press [Enter] to exit..."; |
| 112 | + std::cin.ignore(); |
| 113 | +} |
0 commit comments