예제 #1
0
        public void SystemParametersInfoGetTest()
        {
            // Try get bool value
            var ptr = new SafeHGlobalHandle(4);

            Assert.That(SystemParametersInfo(SPI.SPI_GETCLIENTAREAANIMATION, 0, (IntPtr)ptr, 0));
            var bval1 = ptr.ToStructure <uint>() > 0;

            Assert.That(bval1, Is.True);

            // Try get generic bool value
            Assert.That(SystemParametersInfo(SPI.SPI_GETCLIENTAREAANIMATION, out bool bval2));
            Assert.That(bval2, Is.True);

            // Try get integral value
            ptr = new SafeHGlobalHandle(4);
            Assert.That(SystemParametersInfo(SPI.SPI_GETFOCUSBORDERHEIGHT, 0, (IntPtr)ptr, 0));
            var uval1 = ptr.ToStructure <uint>();

            Assert.That(uval1, Is.Not.Zero);

            // Try get generic integral value
            Assert.That(SystemParametersInfo <uint>(SPI.SPI_GETFOCUSBORDERHEIGHT, out var uval2));
            Assert.That(uval2, Is.EqualTo(uval1));

            // Try get struct value
            ptr = SafeHGlobalHandle.CreateFromStructure <RECT>();
            Assert.That(SystemParametersInfo(SPI.SPI_GETWORKAREA, 0, (IntPtr)ptr, 0));
            var rval1 = ptr.ToStructure <RECT>();

            Assert.That(rval1.IsEmpty, Is.False);

            // Try get generic struct value
            Assert.That(SystemParametersInfo(SPI.SPI_GETWORKAREA, out RECT rval2));
            Assert.That(rval2, Is.EqualTo(rval1));

            // Try get string value
            var sb = new System.Text.StringBuilder(Kernel32.MAX_PATH, Kernel32.MAX_PATH);

            Assert.That(SystemParametersInfo(SPI.SPI_GETDESKWALLPAPER, (uint)sb.Capacity, sb, 0));
            Assert.That(sb, Has.Length.GreaterThan(0));
        }
예제 #2
0
 public void GetSetDefaultCommConfigTest()
 {
     using (var mem = new SafeHGlobalHandle(2048))
     {
         var sz = (uint)mem.Size;
         Assert.That(GetDefaultCommConfig(pcCommPort, mem, ref sz), ResultIs.Successful);
         var cc = mem.ToStructure <COMMCONFIG>();
         cc.WriteValues();
         Assert.That(SetDefaultCommConfig(pcCommPort, mem, (uint)mem.Size), ResultIs.Successful);
     }
 }
예제 #3
0
        public void ChangeAndQueryServiceConfigTest()
        {
            var st = GetStartType();

            Assert.That(ChangeServiceConfig(hSvc, ServiceTypes.SERVICE_NO_CHANGE, ServiceStartType.SERVICE_DISABLED, ServiceErrorControlType.SERVICE_NO_CHANGE), ResultIs.Successful);
            Thread.Sleep(10000);

            Assert.That(GetStartType(), Is.EqualTo(ServiceStartType.SERVICE_DISABLED));
            Assert.That(ChangeServiceConfig(hSvc, ServiceTypes.SERVICE_NO_CHANGE, st, ServiceErrorControlType.SERVICE_NO_CHANGE), ResultIs.Successful);
            Assert.That(GetStartType(), Is.EqualTo(st));

            ServiceStartType GetStartType()
            {
                using (var info = new SafeHGlobalHandle(1024))
                {
                    Assert.That(QueryServiceConfig(hSvc, info, info.Size, out var _), ResultIs.Successful);
                    var qsc = info.ToStructure <QUERY_SERVICE_CONFIG>();
                    return(qsc.dwStartType);
                }
            }
        }
예제 #4
0
        public void ChangeAndQueryServiceConfigTest()
        {
            using (var sc = new System.ServiceProcess.ServiceController("Fax"))
            {
                using (var h = sc.ServiceHandle)
                {
                    var hSvc = h.DangerousGetHandle();

                    var st = GetStartType();
                    var b  = ChangeServiceConfig(hSvc, ServiceTypes.SERVICE_NO_CHANGE, ServiceStartType.SERVICE_DISABLED, ServiceErrorControlType.SERVICE_NO_CHANGE);
                    if (!b)
                    {
                        TestContext.WriteLine($"Err: {Win32Error.GetLastError()}");
                    }
                    Assert.That(b, Is.True);
                    Thread.Sleep(10000);

                    Assert.That(GetStartType(), Is.EqualTo(ServiceStartType.SERVICE_DISABLED));
                    b = ChangeServiceConfig(hSvc, ServiceTypes.SERVICE_NO_CHANGE, st, ServiceErrorControlType.SERVICE_NO_CHANGE);
                    if (!b)
                    {
                        TestContext.WriteLine($"Err: {Win32Error.GetLastError()}");
                    }
                    Assert.That(b, Is.True);
                    Assert.That(GetStartType(), Is.EqualTo(st));

                    ServiceStartType GetStartType()
                    {
                        using (var info = new SafeHGlobalHandle(1024))
                        {
                            Assert.That(QueryServiceConfig(hSvc, (IntPtr)info, (uint)info.Size, out var _), Is.True);
                            var qsc = info.ToStructure <QUERY_SERVICE_CONFIG>();
                            return(qsc.dwStartType);
                        }
                    }
                }
            }
        }
예제 #5
0
        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);