void UnregisterShellMenu(std::wstring opt, wchar_t* keyBaseName) { HKEY root = GetRootKey(opt); HKEY cmderKey; FAIL_ON_ERROR( RegCreateKeyEx(root, keyBaseName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &cmderKey, NULL)); #ifdef XP FAIL_ON_ERROR(SHDeleteKey(cmderKey, NULL)); FAIL_ON_ERROR(SHDeleteKey(root, SHELL_MENU_REGISTRY_PATH)); #else FAIL_ON_ERROR(RegDeleteTree(cmderKey, NULL)););
HKEY TRegistry::GetBaseKey(bool Relative) { HKEY Result = 0; if ((FCurrentKey == 0) || !Relative) { Result = GetRootKey(); } else { Result = FCurrentKey; } return Result; }
void TRegistry::SetRootKey(HKEY ARootKey) { if (FRootKey != ARootKey) { if (FCloseRootKey) { RegCloseKey(GetRootKey()); FCloseRootKey = false; } FRootKey = ARootKey; CloseKey(); } }
void RegisterShellMenu(std::wstring opt, wchar_t* keyBaseName) { // First, get the paths we will use wchar_t exePath[MAX_PATH] = { 0 }; wchar_t icoPath[MAX_PATH] = { 0 }; GetModuleFileName(NULL, exePath, sizeof(exePath)); wchar_t commandStr[MAX_PATH + 20] = { 0 }; swprintf_s(commandStr, L"\"%s\" \"%%V\"", exePath); // Now that we have `commandStr`, it's OK to change `exePath`... PathRemoveFileSpec(exePath); PathCombine(icoPath, exePath, L"icons\\cmder.ico"); // Now set the registry keys HKEY root = GetRootKey(opt); HKEY cmderKey; FAIL_ON_ERROR( RegCreateKeyEx(root, keyBaseName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &cmderKey, NULL)); FAIL_ON_ERROR(RegSetValue(cmderKey, L"", REG_SZ, L"Cmder Here", NULL)); FAIL_ON_ERROR(RegSetValueEx(cmderKey, L"NoWorkingDirectory", 0, REG_SZ, (BYTE *)L"", 2)); FAIL_ON_ERROR(RegSetValueEx(cmderKey, L"Icon", 0, REG_SZ, (BYTE *)icoPath, wcslen(icoPath) * sizeof(wchar_t))); HKEY command; FAIL_ON_ERROR( RegCreateKeyEx(cmderKey, L"command", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &command, NULL)); FAIL_ON_ERROR(RegSetValue(command, L"", REG_SZ, commandStr, NULL)); RegCloseKey(command); RegCloseKey(cmderKey); RegCloseKey(root); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // REGistry eXecution HRESULT REGX(PTSTR ptzCmd) { HKEY hRoot = GetRootKey(ptzCmd); if (!hRoot) { return E_INVALIDARG; } PTSTR ptzVal = UStrChr(ptzCmd, '='); if (ptzVal) { *ptzVal++ = 0; } PTSTR ptzName = UStrRChr(ptzCmd, '\\'); if (!ptzName) { return E_INVALIDARG; } else { *ptzName++ = 0; } HKEY hKey; HRESULT hResult = RegCreateKeyEx(hRoot, ptzCmd, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL); if (hResult != S_OK) { return hResult; } if (ptzVal) { if (*ptzName == '#') { DWORD dwData = UStrToInt(ptzVal); hResult = RegSetValueEx(hKey, ptzName + 1, 0, REG_DWORD, (PBYTE) &dwData, sizeof(DWORD)); } else if (*ptzName == '@') { UINT i = 0; PBYTE pbVal = (PBYTE) ptzVal; while (*ptzVal) { pbVal[i++] = (UChrToHex(ptzVal[0]) << 4) | UChrToHex(ptzVal[1]); while (*ptzVal && (*ptzVal++ != ',')); } hResult = RegSetValueEx(hKey, ptzName + 1, 0, REG_BINARY, pbVal, i); } else { hResult = RegSetValueEx(hKey, ptzName, 0, REG_SZ, (PBYTE) ptzVal, (UStrLen(ptzVal) + 1) * sizeof(TCHAR)); } } else { if (*ptzName == '-') { if (ptzName[1]) { hResult = RegDeleteValue(hKey, ptzName + 1); } else { RegCloseKey(hKey); return SHDeleteKey(hRoot, ptzCmd); } } else if (*ptzName == '#') { DWORD dwSize = sizeof(hResult); RegQueryValueEx(hKey, ptzName + 1, NULL, NULL, (PBYTE) &hResult, &dwSize); } else { g_tzXVar[0][0] = 0; DWORD dwSize = sizeof(g_tzXVar[0]); hResult = RegQueryValueEx(hKey, ptzName, NULL, NULL, (PBYTE) g_tzXVar[0], &dwSize); } } RegCloseKey(hKey); return hResult; }
HRESULT Import(PTSTR ptzScript, PCTSTR* pptzStop = NULL) { HKEY hKey = NULL; _SkipBlank(ptzScript); HRESULT hResult = S_OK; PTSTR ptzEnd = ptzScript; PTSTR ptzStart = ptzScript; while (*ptzScript) { switch (*ptzScript) { case '"': if (*ptzStart != '[') { ptzScript++; *ptzEnd++ = CC_SEP; continue; } break; case '\\': if (*ptzStart != '[') { ptzScript++; _SkipBlank(ptzScript); *ptzEnd++ = *ptzScript++; continue; } break; case '\r': case '\n': *ptzEnd = 0; if (*ptzStart == '[') { if (hKey) { RegCloseKey(hKey); hKey = NULL; } ptzScript[-1] = 0; if (*++ptzStart == '-') { HKEY hRoot = GetRootKey(++ptzStart); if (hRoot) { SHDeleteKey(hRoot, ptzStart); } } else { HKEY hRoot = GetRootKey(ptzStart); if (hRoot) { RegCreateKeyEx(hRoot, ptzStart, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL); } } } else if (hKey && ((*ptzStart == '@') || (*ptzStart == CC_SEP))) { PTSTR ptzVal; if (*ptzStart++ == '@') { ptzVal = UStrChr(ptzStart, '='); ptzStart = NULL; } else if (ptzVal = UStrChr(ptzStart, CC_SEP)) { *ptzVal++ = 0; ptzVal = UStrChr(ptzVal, '='); } if (ptzVal) { PTSTR ptzTemp; if (ptzTemp = UStrStrI(ptzVal, TEXT("DWORD:"))) { ptzVal = ptzTemp + 4; ptzVal[0] = '0'; ptzVal[1] = 'x'; DWORD dwData = UStrToInt(ptzVal); RegSetValueEx(hKey, ptzStart, 0, REG_DWORD, (PBYTE) &dwData, sizeof(DWORD)); } else if (ptzTemp = UStrStrI(ptzVal, TEXT("HEX"))) { DWORD dwType = REG_BINARY; if (ptzTemp[3] == '(') { ptzVal[2] = '0'; ptzVal[3] = 'x'; dwType = UStrToInt(ptzTemp + 2); } if (ptzVal = UStrChr(ptzTemp, ':')) { ptzVal++; UINT i = 0; PBYTE pbVal = (PBYTE) ptzVal; while (*ptzVal) { BYTE bVal = (UChrToHex(ptzVal[0]) << 4) | UChrToHex(ptzVal[1]); while (*ptzVal && (*ptzVal++ != ',')); pbVal[i++] = bVal; } RegSetValueEx(hKey, ptzStart, 0, dwType, pbVal, i); } } else if (ptzTemp = UStrStrI(ptzVal, TEXT("MULTI_SZ:"))) { if (ptzVal = UStrChr(ptzTemp, CC_SEP)) { ptzEnd = ++ptzVal; ptzTemp = ptzVal; while (*ptzTemp) { if (*ptzTemp == CC_SEP) { ptzTemp++; *ptzEnd++ = 0; while (*ptzTemp && (*ptzTemp++ != CC_SEP)); } else { *ptzEnd++ =*ptzTemp++; } } *ptzEnd++ = 0; RegSetValueEx(hKey, ptzStart, 0, REG_MULTI_SZ, (PBYTE) ptzVal, (DWORD) (ptzEnd - ptzVal) * sizeof(TCHAR)); } } else { DWORD dwType = UStrStrI(ptzVal, TEXT("MUI_SZ:")) ? REG_MUI_SZ : REG_SZ; if (ptzTemp = UStrChr(ptzVal, CC_SEP)) { ptzVal = ptzTemp + 1; if (ptzTemp = UStrChr(ptzVal, CC_SEP)) { *ptzTemp++ = 0; RegSetValueEx(hKey, ptzStart, 0, dwType, (PBYTE) ptzVal, (DWORD) (ptzTemp - ptzVal) * sizeof(TCHAR)); } } else if (ptzVal[1] == '-') { RegDeleteValue(hKey, ptzStart); } } } } else if (*ptzStart != ';') { hResult = ERROR_NOT_REGISTRY_FILE; if (pptzStop) { *ptzScript = '\n'; ptzScript = ptzStart; goto _Stop; } } ptzScript++; _SkipBlank(ptzScript); ptzStart = ptzEnd = ptzScript; continue; } *ptzEnd++ = *ptzScript++; } _Stop: if (pptzStop) { *pptzStop = ptzScript; } if (hKey) { RegCloseKey(hKey); } return hResult; }
/*********************************************************************** * registry_callback * * Called once for each AddReg and DelReg entry in a given section. */ static BOOL registry_callback (HINF hInf, PWCHAR Section, BOOL Delete) { WCHAR Buffer[MAX_INF_STRING_LENGTH]; PWCHAR ValuePtr; ULONG Flags; size_t Length; PINFCONTEXT Context = NULL; HKEY KeyHandle; BOOL Ok; Ok = InfHostFindFirstLine (hInf, Section, NULL, &Context) == 0; if (!Ok) return TRUE; /* Don't fail if the section isn't present */ for (;Ok; Ok = (InfHostFindNextLine (Context, Context) == 0)) { /* get root */ if (InfHostGetStringField (Context, 1, Buffer, MAX_INF_STRING_LENGTH, NULL) != 0) continue; if (!GetRootKey (Buffer)) continue; /* get key */ Length = strlenW (Buffer); if (InfHostGetStringField (Context, 2, Buffer + Length, MAX_INF_STRING_LENGTH - (ULONG)Length, NULL) != 0) *Buffer = 0; DPRINT("KeyName: <%S>\n", Buffer); if (Delete) { Flags = FLG_ADDREG_DELVAL; } else { /* get flags */ if (InfHostGetIntField (Context, 4, &Flags) != 0) Flags = 0; } DPRINT("Flags: 0x%x\n", Flags); if (Delete || (Flags & FLG_ADDREG_OVERWRITEONLY)) { if (RegOpenKeyW (NULL, Buffer, &KeyHandle) != ERROR_SUCCESS) { DPRINT("RegOpenKey(%S) failed\n", Buffer); continue; /* ignore if it doesn't exist */ } } else { if (RegCreateKeyW (NULL, Buffer, &KeyHandle) != ERROR_SUCCESS) { DPRINT("RegCreateKey(%S) failed\n", Buffer); continue; } } /* get value name */ if (InfHostGetStringField (Context, 3, Buffer, MAX_INF_STRING_LENGTH, NULL) == 0) { ValuePtr = Buffer; } else { ValuePtr = NULL; } /* and now do it */ if (!do_reg_operation (KeyHandle, ValuePtr, Context, Flags)) { return FALSE; } } InfHostFreeContext(Context); return TRUE; }
/*********************************************************************** * registry_callback * * Called once for each AddReg and DelReg entry in a given section. */ static BOOLEAN registry_callback (HINF hInf, PCWSTR Section, BOOLEAN Delete) { OBJECT_ATTRIBUTES ObjectAttributes; WCHAR Buffer[MAX_INF_STRING_LENGTH]; UNICODE_STRING Name; UNICODE_STRING Value; PUNICODE_STRING ValuePtr; NTSTATUS Status; UINT Flags; ULONG Length; INFCONTEXT Context; HANDLE KeyHandle; BOOLEAN Ok; Ok = SetupFindFirstLineW (hInf, Section, NULL, &Context); if (Ok) { for (;Ok; Ok = SetupFindNextLine (&Context, &Context)) { /* get root */ if (!SetupGetStringFieldW (&Context, 1, Buffer, MAX_INF_STRING_LENGTH, NULL)) continue; if (!GetRootKey (Buffer)) continue; /* get key */ Length = wcslen (Buffer); if (!SetupGetStringFieldW (&Context, 2, Buffer + Length, MAX_INF_STRING_LENGTH - Length, NULL)) *Buffer = 0; DPRINT("KeyName: <%S>\n", Buffer); /* get flags */ if (!SetupGetIntField (&Context, 4, (PINT)&Flags)) Flags = 0; DPRINT("Flags: %lx\n", Flags); #ifdef __ODYSSEY__ RtlInitUnicodeString (&Name, Buffer); InitializeObjectAttributes (&ObjectAttributes, &Name, OBJ_CASE_INSENSITIVE, NULL, NULL); if (Delete || (Flags & FLG_ADDREG_OVERWRITEONLY)) { Status = NtOpenKey (&KeyHandle, KEY_ALL_ACCESS, &ObjectAttributes); if (!NT_SUCCESS(Status)) { DPRINT("NtOpenKey(%wZ) failed (Status %lx)\n", &Name, Status); continue; /* ignore if it doesn't exist */ } } else { Status = CreateNestedKey (&KeyHandle, KEY_ALL_ACCESS, &ObjectAttributes); if (!NT_SUCCESS(Status)) { DPRINT("CreateNestedKey(%wZ) failed (Status %lx)\n", &Name, Status); continue; } } #else if (Delete || (Flags & FLG_ADDREG_OVERWRITEONLY)) { LONG rc = RegOpenKeyW(NULL, Buffer, &KeyHandle); if (rc != ERROR_SUCCESS) { DPRINT("RegOpenKeyW(%S) failed (error %lu)\n", Buffer, rc); continue; /* ignore if it doesn't exist */ } } else { LONG rc = RegCreateKeyW(NULL, Buffer, &KeyHandle); if (rc != ERROR_SUCCESS) { DPRINT("RegCreateKeyW(%S) failed (error %lu)\n", Buffer, rc); continue; } } #endif /* get value name */ if (SetupGetStringFieldW (&Context, 3, Buffer, MAX_INF_STRING_LENGTH, NULL)) { RtlInitUnicodeString (&Value, Buffer); ValuePtr = &Value; } else { ValuePtr = NULL; } /* and now do it */ if (!do_reg_operation (KeyHandle, ValuePtr, &Context, Flags)) { NtClose (KeyHandle); return FALSE; } #ifdef __ODYSSEY__ NtClose (KeyHandle); #endif } } return TRUE; }