internal MenuItemInfo(HMENU hMenu, uint idx) { using var strmem = new SafeHGlobalHandle(512); var mii = new MENUITEMINFO { cbSize = (uint)Marshal.SizeOf(typeof(MENUITEMINFO)), fMask = MenuItemInfoMask.MIIM_ID | MenuItemInfoMask.MIIM_SUBMENU | MenuItemInfoMask.MIIM_FTYPE | MenuItemInfoMask.MIIM_STRING | MenuItemInfoMask.MIIM_STATE | MenuItemInfoMask.MIIM_BITMAP, fType = MenuItemType.MFT_STRING, dwTypeData = (IntPtr)strmem, cch = strmem.Size / (uint)StringHelper.GetCharSize() }; Win32Error.ThrowLastErrorIfFalse(GetMenuItemInfo(hMenu, idx, true, ref mii)); Id = mii.wID; Text = mii.fType.IsFlagSet(MenuItemType.MFT_SEPARATOR) ? "-" : mii.fType.IsFlagSet(MenuItemType.MFT_STRING) ? strmem.ToString(-1, CharSet.Auto) : ""; Type = mii.fType; State = mii.fState; BitmapHandle = mii.hbmpItem; SubMenus = GetMenuItems(mii.hSubMenu); }
public void RegCreateDeleteOpenKeyExTest() { const string regKey = "Software"; Assert.That(RegOpenKeyEx(HKEY.HKEY_CURRENT_USER, regKey, RegOpenOptions.REG_OPTION_NON_VOLATILE, REGSAM.KEY_ALL_ACCESS, out var hPKey), ResultIs.Successful); using (hPKey) { Assert.That(RegCreateKeyEx(hPKey, tmpRegKey, samDesired: REGSAM.KEY_ALL_ACCESS, phkResult: out var hKey, lpdwDisposition: out _), ResultIs.Successful); try { using (hKey) { using (var pSD = new SafePSECURITY_DESCRIPTOR(256)) { Assert.That(SetSecurityDescriptorOwner(pSD, SafePSID.Current, false), ResultIs.Successful); Assert.That(RegSetKeySecurity(hKey, SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION, pSD), ResultIs.Successful); using (var pSD2 = new SafePSECURITY_DESCRIPTOR(256)) { var sdsz = (uint)pSD2.Size; Assert.That(RegGetKeySecurity(hKey, SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION, pSD2, ref sdsz), ResultIs.Successful); } } using (var mem = new SafeHGlobalHandle(1024)) { var memSz = (uint)mem.Size; const int val = 255; ulong ulongVal = val; var byteVal = BitConverter.GetBytes((uint)val); var strVal = val.ToString(); Assert.That(RegSetKeyValue(hPKey, tmpRegKey, "V1", REG_VALUE_TYPE.REG_QWORD, new PinnedObject(ulongVal), 8), ResultIs.Successful); Assert.That(RegGetValue(hPKey, tmpRegKey, "V1", RRF.RRF_RT_QWORD, out _, mem, ref memSz), ResultIs.Successful); Assert.That(mem.ToStructure <ulong>(), Is.EqualTo(ulongVal)); Assert.That(RegSetKeyValue(hPKey, tmpRegKey, "V2", REG_VALUE_TYPE.REG_DWORD, byteVal, (uint)byteVal.Length), ResultIs.Successful); memSz = mem.Size; Assert.That(RegGetValue(hPKey, tmpRegKey, "V2", RRF.RRF_RT_DWORD, out _, mem, ref memSz), ResultIs.Successful); Assert.That(mem.ToStructure <uint>(), Is.EqualTo((uint)val)); Assert.That(RegSetKeyValue(hPKey, tmpRegKey, "V3", REG_VALUE_TYPE.REG_SZ, strVal, (uint)StringHelper.GetByteCount(strVal)), ResultIs.Successful); memSz = mem.Size; Assert.That(RegGetValue(hPKey, tmpRegKey, "V3", RRF.RRF_RT_REG_SZ, out _, mem, ref memSz), ResultIs.Successful); Assert.That(mem.ToString(-1, CharSet.Auto), Is.EqualTo(strVal)); memSz = mem.Size; Assert.That(RegQueryMultipleValues(hKey, new[] { new VALENT("V1"), new VALENT("V2"), new VALENT("V3") }, 3, mem, ref memSz), ResultIs.Successful); Assert.That(RegDeleteKeyValue(hPKey, tmpRegKey, "V1"), ResultIs.Successful); Assert.That(RegDeleteKeyValue(hPKey, tmpRegKey, "V2"), ResultIs.Successful); Assert.That(RegDeleteKeyValue(hPKey, tmpRegKey, "V3"), ResultIs.Successful); Assert.That(RegSetValue(hKey, null, REG_VALUE_TYPE.REG_SZ, strVal), ResultIs.Successful); var imemSz = (int)mem.Size; Assert.That(RegQueryValue(hKey, null, mem, ref imemSz), ResultIs.Successful); Assert.That(mem.ToString(-1, CharSet.Auto), Is.EqualTo(strVal)); Assert.That(RegDeleteValue(hKey, null), ResultIs.Successful); Assert.That(RegSetValueEx(hKey, "V1", 0, REG_VALUE_TYPE.REG_QWORD, new PinnedObject(ulongVal), 8), ResultIs.Successful); memSz = mem.Size; Assert.That(RegQueryValueEx(hKey, "V1", default, out _, mem, ref memSz), ResultIs.Successful);