Ejemplo n.º 1
0
        /// <summary>
        /// Get a DWORD Registry Value as string
        /// </summary>
        /// <param name="hDefKey">HKLM = 2147483650</param>
        /// <param name="sSubKeyName"></param>
        /// <param name="sValueName"></param>
        /// <param name="DefaultValue">return string if key or value does not exist</param>
        /// <returns></returns>
        public string GetDWord(UInt32 hDefKey, string sSubKeyName, string sValueName, string DefaultValue)
        {
            {
                String result = "";
                try
                {
                    WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
                    oProv.mScope.Path.NamespacePath = @"ROOT\default";

                    ManagementBaseObject inParams = oProv.GetClass("StdRegProv").GetMethodParameters("GetDWORDValue");
                    inParams["hDefKey"]     = hDefKey;
                    inParams["sSubKeyName"] = sSubKeyName;
                    inParams["sValueName"]  = sValueName;
                    ManagementBaseObject outParams = oProv.ExecuteMethod("StdRegProv", "GetDWORDValue", inParams);

                    if (outParams.GetPropertyValue("ReturnValue").ToString() == "0")
                    {
                        if (outParams.GetPropertyValue("uValue") != null)
                        {
                            result = outParams.GetPropertyValue("uValue").ToString();
                        }
                    }
                    return(result);
                }
                catch
                {
                    return(DefaultValue);
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Get an ArrayList of all Regisry Values
 /// </summary>
 /// <param name="hDefKey">2147483650 = HKLM</param>
 /// <param name="sSubKeyName"></param>
 /// <returns>RegistryValues</returns>
 public List <string> RegValuesList(UInt32 hDefKey, string sSubKeyName)
 {
     try
     {
         WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
         oProv.mScope.Path.NamespacePath = @"ROOT\default";
         ManagementBaseObject inParams = oProv.GetClass("StdRegProv").GetMethodParameters("EnumValues");
         inParams["hDefKey"]     = hDefKey;
         inParams["sSubKeyName"] = sSubKeyName;
         ManagementBaseObject outParams = oProv.ExecuteMethod("StdRegProv", "EnumValues", inParams);
         List <string>        result    = new List <string>();
         if (outParams.GetPropertyValue("ReturnValue").ToString() == "0")
         {
             if (outParams.GetPropertyValue("sNames") != null)
             {
                 result.AddRange(outParams.GetPropertyValue("sNames") as String[]);
             }
         }
         return(result);
     }
     catch
     {
         throw;
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Create a new process on a remote system. For security reasons this method cannot be used to start an interactive process remotely.
        /// </summary>
        /// <param name="CommandLine"></param>
        /// <param name="CurrentDirectory"></param>
        /// <param name="ProcessStartupInformation">A Win32_ProcessStartup Object <see href="http://msdn2.microsoft.com/en-us/library/aa394375.aspx"/></param>
        /// <returns>ProcessID</returns>
        public int StartProcess(string CommandLine, string CurrentDirectory, ManagementBaseObject ProcessStartupInformation)
        {
            if (CurrentDirectory == "")
            {
                CurrentDirectory = null;
            }

            WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());

            oProv.mScope.Path.NamespacePath = @"root\cimv2";
            ManagementClass      MC       = oProv.GetClass("Win32_Process");
            ManagementBaseObject inParams = MC.GetMethodParameters("Create");

            inParams["CommandLine"]               = CommandLine;
            inParams["CurrentDirectory"]          = CurrentDirectory;
            inParams["ProcessStartupInformation"] = ProcessStartupInformation;

            ManagementBaseObject Result = MC.InvokeMethod("Create", inParams, null);

            switch (int.Parse(Result.GetPropertyValue("ReturnValue").ToString()))
            {
            case 0: return(int.Parse(Result.GetPropertyValue("ProcessID").ToString()));

            case 2: throw new System.Security.SecurityException("Access denied");

            case 3: throw new System.Security.SecurityException("Insufficient privilege");

            case 9: throw new Exception("Path not found: " + CommandLine);

            case 21: throw new Exception("Invalid parameter");

            default: throw new Exception("Unknown failure");
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Create a remote process
 /// </summary>
 /// <param name="CommandLine"></param>
 /// <param name="CurrentDirectory"></param>
 /// <param name="ProcessStartupInformation"></param>
 /// <returns>Returns ProcessID or 0 if failed</returns>
 public UInt32 CreateProcess(string CommandLine, string CurrentDirectory, ManagementBaseObject ProcessStartupInformation)
 {
     try
     {
         WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
         oProv.mScope.Path.NamespacePath = @"root\cimv2";
         ManagementClass      oProcess = oProv.GetClass("Win32_Process");
         ManagementBaseObject inParams = oProcess.GetMethodParameters("Create");
         inParams["CommandLine"]               = CommandLine;
         inParams["CurrentDirectory"]          = CurrentDirectory;
         inParams["ProcessStartupInformation"] = ProcessStartupInformation;
         ManagementBaseObject outParams = oProcess.InvokeMethod("Create", inParams, null);
         if (int.Parse(outParams["ReturnValue"].ToString()) == 0)
         {
             return(UInt32.Parse(outParams["ProcessId"].ToString()));
         }
         else
         {
             return(0);
         }
     }
     catch
     {
         throw new Exception("Unable to start command: " + CommandLine);
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Set Registry String Value
        /// </summary>
        /// <param name="hDefKey">HKLM = 2147483650</param>
        /// <param name="sSubKeyName"></param>
        /// <param name="sValueName"></param>
        /// <param name="sValue"></param>
        public void SetStringValue(UInt32 hDefKey, String sSubKeyName, String sValueName, String sValue)
        {
            WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());

            oProv.mScope.Path.NamespacePath = @"ROOT\default";
            ManagementBaseObject inParams = oProv.GetClass("StdRegProv").GetMethodParameters("SetStringValue");

            inParams["hDefKey"]     = hDefKey;
            inParams["sSubKeyName"] = sSubKeyName;
            inParams["sValueName"]  = sValueName;
            inParams["sValue"]      = sValue;
            oProv.ExecuteMethod("StdRegProv", "SetStringValue", inParams);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Install a Windows Installer Package
        /// </summary>
        /// <param name="sPath"></param>
        /// <param name="sOptions"></param>
        /// <returns>MSI Exit Code</returns>
        public UInt32 InstallMSI(string sPath, string sOptions)
        {
            WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());

            oProv.mScope.Path.NamespacePath = @"root\cimv2";
            ManagementClass      MC       = oProv.GetClass("Win32_Product");
            ManagementBaseObject inParams = MC.GetMethodParameters("Install");

            inParams.SetPropertyValue("PackageLocation", sPath);
            inParams.SetPropertyValue("Options", sOptions);
            ManagementBaseObject Result = MC.InvokeMethod("Install", inParams, null);

            return((UInt32)Result.GetPropertyValue("ReturnValue"));
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Set a DWORD Registry Value from a UInt32 Value
 /// </summary>
 /// <param name="hDefKey"></param>
 /// <param name="sSubKeyName"></param>
 /// <param name="sValueName"></param>
 /// <param name="uValue"></param>
 public void SetDWord(UInt32 hDefKey, string sSubKeyName, string sValueName, UInt32 uValue)
 {
     try
     {
         WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
         oProv.mScope.Path.NamespacePath = @"ROOT\default";
         ManagementBaseObject inParams = oProv.GetClass("StdRegProv").GetMethodParameters("SetDWORDValue");
         inParams["hDefKey"]     = hDefKey;
         inParams["sSubKeyName"] = sSubKeyName;
         inParams["sValueName"]  = sValueName;
         inParams["uValue"]      = uValue;
         ManagementBaseObject outParams = oProv.ExecuteMethod("StdRegProv", "SetDWORDValue", inParams);
     }
     catch
     {
         throw;
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Delete a Registry Key and all Subkeys
        /// </summary>
        /// <param name="hDefKey"></param>
        /// <param name="sSubKeyName"></param>
        public void DeleteKey(UInt32 hDefKey, String sSubKeyName)
        {
            try
            {
                //Delete all subkeys
                ArrayList Subkeys = RegKeys(hDefKey, sSubKeyName);
                foreach (string skey in Subkeys)
                {
                    DeleteKey(hDefKey, sSubKeyName + @"\" + skey);
                }

                WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
                oProv.mScope.Path.NamespacePath = @"ROOT\default";
                ManagementBaseObject inParams = oProv.GetClass("StdRegProv").GetMethodParameters("DeleteKey");
                inParams["hDefKey"]     = hDefKey;
                inParams["sSubKeyName"] = sSubKeyName;
                oProv.ExecuteMethod("StdRegProv", "DeleteKey", inParams);
            }
            catch
            {
                throw;
            }
        }