Esempio n. 1
0
 internal static extern bool SetServiceStatus(IntPtr hServiceStatus, ref structenums.SERVICE_STATUS lpServiceStatus);
Esempio n. 2
0
 internal static extern int RegOpenKey(structenums.baseKey hKey, [MarshalAs(UnmanagedType.LPStr)]string subKey, ref UIntPtr phkResult);
Esempio n. 3
0
 internal static extern int RegDeleteTree(structenums.baseKey hKey, [MarshalAs(UnmanagedType.LPStr)]string subKey);
Esempio n. 4
0
 internal static extern int RtlGetVersion(ref structenums.OSVERSIONINFOW version);
Esempio n. 5
0
 internal static extern int NetUserSetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername, string username, int level, ref structenums.USER_INFO_4 buf, out Int32 parm_err);
Esempio n. 6
0
        public static UIntPtr RegistryOpenKey(structenums.baseKey RootKey, string SubKey)
        {
            UIntPtr hKey = UIntPtr.Zero;
            int Result = SafeNativeMethods.RegOpenKey(RootKey, SubKey, ref hKey);
            if (Result != 0)
            {
                Console.WriteLine("RegOpenKey error:{0}", LastError(Result));
            }

            return hKey;
        }
Esempio n. 7
0
        public static Boolean RegistryDeleteTree(structenums.baseKey RootKey, string SubKey)
        {
            int Result = SafeNativeMethods.RegDeleteTree(RootKey, SubKey);
            if (Result != 0)
            {
                Console.WriteLine("CopyKey error:{0}", LastError(Result));
                return false;
            }

            return true;
        }
Esempio n. 8
0
        /// <summary>
        /// load a regfile at hklm or hku
        /// </summary>
        /// <param name="where">hklm or hku</param>
        /// <param name="name">The name of the key to be created under hKey. This subkey is where the registration information from the file will be loaded</param>
        /// <param name="file"></param>
        /// <returns></returns>
        public static Boolean RegistryLoad(structenums.RegistryLocation where, string name, string file)
        {
            if (where != structenums.RegistryLocation.HKEY_LOCAL_MACHINE && where != structenums.RegistryLocation.HKEY_USERS)
            {
                LibraryLogging.Error("unable to load regfile at {0}", where);
                return false;
            }

            if (!SafeNativeMethods.RegistryLoadPrivilegeSet)
            {
                if (!RegistryLoadSetPrivilege())
                    return false;
            }

            int ret = SafeNativeMethods.RegLoadKey((uint)Enum.Parse(typeof(structenums.baseKey), where.ToString()), name, file);
            if (ret != 0)
            {
                LibraryLogging.Error("Unable to load regfile {0} error:{1} {2}", file, ret, LastError(ret));
                return false;
            }
            return true;
        }
Esempio n. 9
0
        /// <summary>
        /// unload regfile from regkey
        /// </summary>
        /// <param name="where">hklm or hku</param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Boolean RegistryUnLoad(structenums.RegistryLocation where, string name)
        {
            if (!SafeNativeMethods.RegistryLoadPrivilegeSet)
            {
                if (!RegistryLoadSetPrivilege())
                    return false;
            }

            int ret = SafeNativeMethods.RegUnLoadKey((uint)Enum.Parse(typeof(structenums.baseKey), where.ToString()), name);
            if (ret != 0)
            {
                LibraryLogging.Error("Unable to unload regkey {0} error:{1} {2}", name, ret, LastError(ret));
                return false;
            }
            return true;
        }
Esempio n. 10
0
        /// <summary>
        /// returns local user settings as a ref of an USER_INFO_4 struct
        /// based on http://social.msdn.microsoft.com/forums/en-us/csharpgeneral/thread/B70B79D9-971F-4D6F-8462-97FC126DE0AD
        /// </summary>
        /// <param name="username"></param>
        /// <param name="domain"></param>
        /// <param name="userinfo4">a ref of an USER_INFO_4 struct</param>
        /// <returns>bool true 4 success</returns>
        public static Boolean UserGet(string username, string domain, ref structenums.USER_INFO_4 userinfo4)
        {
            IntPtr bufPtr;

            int lngReturn = SafeNativeMethods.NetUserGetInfo((String.IsNullOrEmpty(domain)) ? null : domain, username, 4, out bufPtr);
            if (lngReturn == 0)
            {
                try
                {
                    userinfo4 = (structenums.USER_INFO_4)Marshal.PtrToStructure(bufPtr, typeof(structenums.USER_INFO_4));
                }
                catch (Exception ex)
                {
                    LibraryLogging.Error("UserGet Marshal.PtrToStructure error:{0}", ex.ToString());
                    return false;
                }
            }
            else if (lngReturn == 2221)
            {
                // user does not exist
            }
            else
            {
                string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).ToString();
                LibraryLogging.Error("NetUserGetInfo error:{0} {1}", lngReturn, errorMessage);
            }

            SafeNativeMethods.NetApiBufferFree(bufPtr);

            if (lngReturn == 0)
            {
                return true;
            }

            return false;
        }
Esempio n. 11
0
        /// <summary>
        /// get RegistryKey from structenums.RegistryLocation
        /// </summary>
        /// <param name="location"></param>
        /// <returns></returns>
        public static RegistryKey GetRegistryLocation(structenums.RegistryLocation location)
        {
            switch (location)
            {
                case structenums.RegistryLocation.HKEY_CLASSES_ROOT:
                    return Registry.ClassesRoot;

                case structenums.RegistryLocation.HKEY_CURRENT_USER:
                    return Registry.CurrentUser;

                case structenums.RegistryLocation.HKEY_LOCAL_MACHINE:
                    return Registry.LocalMachine;

                case structenums.RegistryLocation.HKEY_USERS:
                    return Registry.Users;

                case structenums.RegistryLocation.HKEY_CURRENT_CONFIG:
                    return Registry.CurrentConfig;

                default:
                    return null;
            }
        }
Esempio n. 12
0
 /// <summary>
 /// returns local user settings as a ref of an USER_INFO_4 struct
 /// based on http://social.msdn.microsoft.com/forums/en-us/csharpgeneral/thread/B70B79D9-971F-4D6F-8462-97FC126DE0AD
 /// </summary>
 /// <param name="username"></param>
 /// <param name="userinfo4">a ref of an USER_INFO_4 struct</param>
 /// <returns>bool true 4 success</returns>
 public static Boolean UserGet(string username, ref structenums.USER_INFO_4 userinfo4)
 {
     return UserGet(username, null, ref userinfo4);
 }
Esempio n. 13
0
        /// <summary>
        /// modify local user settings based on a USER_INFO_4 struct
        /// based on http://social.msdn.microsoft.com/forums/en-us/csharpgeneral/thread/B70B79D9-971F-4D6F-8462-97FC126DE0AD
        /// </summary>
        /// <param name="username"></param>
        /// <param name="userInfo4"></param>
        /// <returns></returns>
        public static Boolean UserMod(string username, structenums.USER_INFO_4 userInfo4)
        {
            Int32 ret = 1;

            SafeNativeMethods.NetUserSetInfo(null, username, 4, ref userInfo4, out ret);
            if (ret != 0) // If the call fails we get a non-zero value
            {
                LibraryLogging.Error("NetUserSetInfo error:{0} {1}", ret, LastError());
                return false;
            }

            return true;
        }
Esempio n. 14
0
        public static bool SetServiceStopped(IntPtr handle, ref structenums.SERVICE_STATUS myServiceStatus)
        {
            myServiceStatus.checkPoint++;
            myServiceStatus.currentState = (int)SafeNativeMethods.State.SERVICE_STOPPED;
            myServiceStatus.waitHint = 0;
            if (!SafeNativeMethods.SetServiceStatus(handle, ref myServiceStatus))
            {
                LibraryLogging.Error("SetServiceStatus error:{0}", LastError());
                return false;
            }

            return true;
        }
Esempio n. 15
0
        public static bool ShutdownPending(IntPtr handle, ref structenums.SERVICE_STATUS myServiceStatus, TimeSpan wait)
        {
            myServiceStatus.checkPoint++;
            myServiceStatus.currentState = (int)SafeNativeMethods.State.SERVICE_STOP_PENDING;
            myServiceStatus.waitHint = wait.Milliseconds;
            if (!SafeNativeMethods.SetServiceStatus(handle, ref myServiceStatus))
            {
                LibraryLogging.Error("SetServiceStatus error:{0}", LastError());
                return false;
            }

            return true;
        }
Esempio n. 16
0
        public static bool SetPreshutdownTimeout(SafeHandle handle, ref structenums.SERVICE_STATUS myServiceStatus)
        {
            SafeNativeMethods.SERVICE_PRESHUTDOWN_INFO spi = new SafeNativeMethods.SERVICE_PRESHUTDOWN_INFO();
            spi.dwPreshutdownTimeout = 3600 * 1000;
            IntPtr lpInfo = Marshal.AllocHGlobal(Marshal.SizeOf(spi));
            if (lpInfo == IntPtr.Zero)
            {
                string errorMessage = LastError();
                LibraryLogging.Error("Unable to allocate memory for service action, error: {0}", errorMessage);
                EventLog.WriteEntry("pGina", String.Format("Unable to allocate memory for service action\nerror:{0}", errorMessage), EventLogEntryType.Warning);

                return false;
            }
            else
            {
                Marshal.StructureToPtr(spi, lpInfo, false);
                if (!SafeNativeMethods.ChangeServiceConfig2(handle, (int)SafeNativeMethods.INFO_LEVEL.SERVICE_CONFIG_PRESHUTDOWN_INFO, lpInfo))
                {
                    string errorMessage = LastError();
                    LibraryLogging.Error("ChangeServiceConfig2 error: {0}", errorMessage);
                    EventLog.WriteEntry("pGina", String.Format("ChangeServiceConfig2\nThe service will be forced to stop during a shutdown within 3 minutes\nerror:{0}", errorMessage), EventLogEntryType.Warning);

                    return false;
                }
                Marshal.FreeHGlobal(lpInfo);
            }

            return true;
        }