Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions WmiLight.UnitTests/ExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
});
Expand All @@ -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.");
}
}
});
Expand All @@ -64,13 +64,45 @@ 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.");
}
}
});

// 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<InvalidParameterException>(() => {

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<InvalidParameterException>(() =>
{
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.");
}
});
}
}
}
18 changes: 17 additions & 1 deletion WmiLight/Exceptions/InvalidParameterException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace WmiLight
using WmiLight.Wbem;

namespace WmiLight
{
#region Description
/// <summary>
Expand All @@ -20,6 +22,20 @@ internal InvalidParameterException(HResultInfo hresultInfo)
{
}

#region Description
/// <summary>
/// Initializes a new instance of the <see cref="InvalidParameterException"/> class.
/// </summary>
/// <param name="methodName">The method name.</param>
/// <param name="className">The class name.</param>
/// <param name="hresult">The HRESULT.</param>
#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
}
}
14 changes: 13 additions & 1 deletion WmiLight/Wbem/WbemServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions WmiLight/WmiConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ public void DeleteInstance(WmiObject instance)
/// <returns>The value returned by the method.</returns>
/// <exception cref="System.ArgumentNullException">The <paramref name="method"/> parameter is <c>null</c>.</exception>
/// <exception cref="System.ObjectDisposedException">Object already disposed.</exception>
/// <exception cref="InvalidParameterException">The WMI method parameters are invalid.</exception>
#endregion
public object ExecuteMethod(WmiMethod method, out WmiMethodParameters outParameters)
{
Expand All @@ -446,6 +447,7 @@ public object ExecuteMethod(WmiMethod method, out WmiMethodParameters outParamet
/// <returns>The value returned by the method.</returns>
/// <exception cref="System.ArgumentNullException">The <paramref name="method"/> parameter is <c>null</c>.</exception>
/// <exception cref="System.ObjectDisposedException">Object already disposed.</exception>
/// <exception cref="InvalidParameterException">The WMI method parameters are invalid.</exception>
#endregion
public TResult ExecuteMethod<TResult>(WmiMethod method, out WmiMethodParameters outParameters)
{
Expand All @@ -465,6 +467,7 @@ public TResult ExecuteMethod<TResult>(WmiMethod method, out WmiMethodParameters
/// <returns>The value returned by the method.</returns>
/// <exception cref="System.ArgumentNullException">The <paramref name="method"/> parameter is <c>null</c>.</exception>
/// <exception cref="System.ObjectDisposedException">Object already disposed.</exception>
/// <exception cref="InvalidParameterException">The WMI method parameters are invalid.</exception>
#endregion
public object ExecuteMethod(WmiMethod method, WmiMethodParameters inParameters, out WmiMethodParameters outParameters)
{
Expand All @@ -484,6 +487,7 @@ public object ExecuteMethod(WmiMethod method, WmiMethodParameters inParameters,
/// <returns>The value returned by the method.</returns>
/// <exception cref="System.ArgumentNullException">The <paramref name="method"/> parameter is <c>null</c>.</exception>
/// <exception cref="System.ObjectDisposedException">Object already disposed.</exception>
/// <exception cref="InvalidParameterException">The WMI method parameters are invalid.</exception>
#endregion
public TResult ExecuteMethod<TResult>(WmiMethod method, WmiMethodParameters inParameters, out WmiMethodParameters outParameters)
{
Expand All @@ -503,6 +507,7 @@ public TResult ExecuteMethod<TResult>(WmiMethod method, WmiMethodParameters inPa
/// <returns>The value returned by the method.</returns>
/// <exception cref="System.ArgumentNullException">The <paramref name="method"/> parameter is <c>null</c>.</exception>
/// <exception cref="System.ObjectDisposedException">Object already disposed.</exception>
/// <exception cref="InvalidParameterException">The WMI method parameters are invalid.</exception>
#endregion
public object ExecuteMethod(WmiMethod method, WmiObject instance, out WmiMethodParameters outParameters)
{
Expand All @@ -522,6 +527,7 @@ public object ExecuteMethod(WmiMethod method, WmiObject instance, out WmiMethodP
/// <returns>The value returned by the method.</returns>
/// <exception cref="System.ArgumentNullException">The <paramref name="method"/> parameter is <c>null</c>.</exception>
/// <exception cref="System.ObjectDisposedException">Object already disposed.</exception>
/// <exception cref="InvalidParameterException">The WMI method parameters are invalid.</exception>
#endregion
public TResult ExecuteMethod<TResult>(WmiMethod method, WmiObject instance, out WmiMethodParameters outParameters)
{
Expand All @@ -542,6 +548,7 @@ public TResult ExecuteMethod<TResult>(WmiMethod method, WmiObject instance, out
/// <returns>The value returned by the method.</returns>
/// <exception cref="System.ArgumentNullException">The <paramref name="method"/> parameter is <c>null</c>.</exception>
/// <exception cref="System.ObjectDisposedException">Object already disposed.</exception>
/// <exception cref="InvalidParameterException">The WMI method parameters are invalid.</exception>
#endregion
public object ExecuteMethod(WmiMethod method, WmiObject instance, WmiMethodParameters inParameters, out WmiMethodParameters outParameters)
{
Expand All @@ -562,6 +569,7 @@ public object ExecuteMethod(WmiMethod method, WmiObject instance, WmiMethodParam
/// <returns>The value returned by the method.</returns>
/// <exception cref="System.ArgumentNullException">The <paramref name="method"/> parameter is <c>null</c>.</exception>
/// <exception cref="System.ObjectDisposedException">Object already disposed.</exception>
/// <exception cref="InvalidParameterException">The WMI method parameters are invalid.</exception>
#endregion
public TResult ExecuteMethod<TResult>(WmiMethod method, WmiObject instance, WmiMethodParameters inParameters, out WmiMethodParameters outParameters)
{
Expand Down