diff --git a/WmiLight.UnitTests/ExceptionTests.cs b/WmiLight.UnitTests/ExceptionTests.cs index 22362d9..cd8a44c 100644 --- a/WmiLight.UnitTests/ExceptionTests.cs +++ b/WmiLight.UnitTests/ExceptionTests.cs @@ -32,7 +32,7 @@ public void InvalidClassException_Is_Thrown() { foreach (WmiObject _ in connection.CreateQuery("SELECT * FROM INVALID_CLASS_123")) { - Assert.Fail("Should not reach here due to timeout."); + Assert.Fail("Should not reach here."); } } }); @@ -47,7 +47,7 @@ public void InvalidQueryException_Is_Thrown_Without_A_Query() { foreach (WmiObject _ in connection.CreateQuery("THIS IS NOT VALID")) { - Assert.Fail("Should not reach here due to timeout."); + Assert.Fail("Should not reach here."); } } }); @@ -64,7 +64,7 @@ public void InvalidQueryException_Is_Thrown_Witt_A_Query() { foreach (WmiObject _ in connection.CreateQuery("THIS IS NOT VALID", EnumeratorBehaviorOption.DirectRead)) { - Assert.Fail("Should not reach here due to timeout."); + Assert.Fail("Should not reach here."); } } }); @@ -72,5 +72,37 @@ public void InvalidQueryException_Is_Thrown_Witt_A_Query() // DirectRead -> Query not set Assert.AreEqual("THIS IS NOT VALID", ex.Query, "Query is different."); } + + [TestMethod] + public void InvalidParameterException_Is_Thrown_For_WBEM_E_INVALID_METHOD_PARAMETERS() + { + Assert.ThrowsException(() => { + + using (WmiConnection connection = new WmiConnection(@"root\Microsoft\Windows\Storage")) + using (WmiMethod method = connection.GetMethod("MSFT_Volume", "GetSupportedFileSystems")) + { + // Don't set required parameters and try to execute + connection.ExecuteMethod(method, null, out WmiMethodParameters outParameters); + + Assert.Fail("Should not reach here."); + } + }); + } + + [TestMethod] + public void InvalidParameterException_Is_Thrown_For_WBEM_E_INVALID_PARAMETER() + { + Assert.ThrowsException(() => + { + using (WmiConnection connection = new WmiConnection()) + using (WmiMethod method = connection.GetMethod("Win32_Process", "Create")) + { + // Don't set required parameters and try to execute + connection.ExecuteMethod(method, null, out WmiMethodParameters outParameters); + + Assert.Fail("Should not reach here."); + } + }); + } } } diff --git a/WmiLight/Exceptions/InvalidParameterException.cs b/WmiLight/Exceptions/InvalidParameterException.cs index 05086e8..ecafc9d 100644 --- a/WmiLight/Exceptions/InvalidParameterException.cs +++ b/WmiLight/Exceptions/InvalidParameterException.cs @@ -1,4 +1,6 @@ -namespace WmiLight +using WmiLight.Wbem; + +namespace WmiLight { #region Description /// @@ -20,6 +22,20 @@ internal InvalidParameterException(HResultInfo hresultInfo) { } + #region Description + /// + /// Initializes a new instance of the class. + /// + /// The method name. + /// The class name. + /// The HRESULT. + #endregion + internal InvalidParameterException(string methodName, string className, WbemStatus hresult) + : base($"Parameters provided for the method {methodName} ({className}) are not valid.") + { + this.HResult = (int)hresult; + } + #endregion } } diff --git a/WmiLight/Wbem/WbemServices.cs b/WmiLight/Wbem/WbemServices.cs index b039ff0..9c152ff 100644 --- a/WmiLight/Wbem/WbemServices.cs +++ b/WmiLight/Wbem/WbemServices.cs @@ -157,7 +157,19 @@ internal void ExecuteMethod(string classNameOrPath, string methodName, IntPtr in HResult hResult = NativeMethods.ExecMethod(this, classNameOrPath, methodName, IntPtr.Zero, inParams, out IntPtr pOutParams); if (hResult.Failed) - throw (Exception)hResult; + { + switch (hResult) + { + case (int)WbemStatus.WBEM_E_INVALID_METHOD_PARAMETERS: + throw new InvalidParameterException(methodName, classNameOrPath, WbemStatus.WBEM_E_INVALID_METHOD_PARAMETERS); + + case (int)WbemStatus.WBEM_E_INVALID_PARAMETER: + throw new InvalidParameterException(methodName, classNameOrPath, WbemStatus.WBEM_E_INVALID_PARAMETER); + + default: + throw (Exception)hResult; + } + } if (pOutParams != IntPtr.Zero) outParams = new WbemClassObject(pOutParams); diff --git a/WmiLight/WmiConnection.cs b/WmiLight/WmiConnection.cs index d5fb131..8ec3655 100644 --- a/WmiLight/WmiConnection.cs +++ b/WmiLight/WmiConnection.cs @@ -428,6 +428,7 @@ public void DeleteInstance(WmiObject instance) /// The value returned by the method. /// The parameter is null. /// Object already disposed. + /// The WMI method parameters are invalid. #endregion public object ExecuteMethod(WmiMethod method, out WmiMethodParameters outParameters) { @@ -446,6 +447,7 @@ public object ExecuteMethod(WmiMethod method, out WmiMethodParameters outParamet /// The value returned by the method. /// The parameter is null. /// Object already disposed. + /// The WMI method parameters are invalid. #endregion public TResult ExecuteMethod(WmiMethod method, out WmiMethodParameters outParameters) { @@ -465,6 +467,7 @@ public TResult ExecuteMethod(WmiMethod method, out WmiMethodParameters /// The value returned by the method. /// The parameter is null. /// Object already disposed. + /// The WMI method parameters are invalid. #endregion public object ExecuteMethod(WmiMethod method, WmiMethodParameters inParameters, out WmiMethodParameters outParameters) { @@ -484,6 +487,7 @@ public object ExecuteMethod(WmiMethod method, WmiMethodParameters inParameters, /// The value returned by the method. /// The parameter is null. /// Object already disposed. + /// The WMI method parameters are invalid. #endregion public TResult ExecuteMethod(WmiMethod method, WmiMethodParameters inParameters, out WmiMethodParameters outParameters) { @@ -503,6 +507,7 @@ public TResult ExecuteMethod(WmiMethod method, WmiMethodParameters inPa /// The value returned by the method. /// The parameter is null. /// Object already disposed. + /// The WMI method parameters are invalid. #endregion public object ExecuteMethod(WmiMethod method, WmiObject instance, out WmiMethodParameters outParameters) { @@ -522,6 +527,7 @@ public object ExecuteMethod(WmiMethod method, WmiObject instance, out WmiMethodP /// The value returned by the method. /// The parameter is null. /// Object already disposed. + /// The WMI method parameters are invalid. #endregion public TResult ExecuteMethod(WmiMethod method, WmiObject instance, out WmiMethodParameters outParameters) { @@ -542,6 +548,7 @@ public TResult ExecuteMethod(WmiMethod method, WmiObject instance, out /// The value returned by the method. /// The parameter is null. /// Object already disposed. + /// The WMI method parameters are invalid. #endregion public object ExecuteMethod(WmiMethod method, WmiObject instance, WmiMethodParameters inParameters, out WmiMethodParameters outParameters) { @@ -562,6 +569,7 @@ public object ExecuteMethod(WmiMethod method, WmiObject instance, WmiMethodParam /// The value returned by the method. /// The parameter is null. /// Object already disposed. + /// The WMI method parameters are invalid. #endregion public TResult ExecuteMethod(WmiMethod method, WmiObject instance, WmiMethodParameters inParameters, out WmiMethodParameters outParameters) {