Skip to content

Commit

Permalink
Enabled multi target build and fixed some potential trimming issues
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKuschnik committed Dec 8, 2023
1 parent 8c4619e commit 9c3d829
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 288 deletions.
8 changes: 4 additions & 4 deletions WmiLight.Native/WmiLight.Native.rc
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ END
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 5,0,2,0
PRODUCTVERSION 5,0,2,0
FILEVERSION 5,1,0,0
PRODUCTVERSION 5,1,0,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -61,12 +61,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "Martin Kuschnik"
VALUE "FileDescription", "The native part of the WmiLight lib."
VALUE "FileVersion", "5.0.2.0"
VALUE "FileVersion", "5.1.0.0"
VALUE "InternalName", "WmiLight.Native"
VALUE "LegalCopyright", "Copyright 2023 Martin Kuschnik"
VALUE "OriginalFilename", "WmiLight.Native.dll"
VALUE "ProductName", "WmiLight"
VALUE "ProductVersion", "5.0.2.0"
VALUE "ProductVersion", "5.1.0.0"
END
END
BLOCK "VarFileInfo"
Expand Down
109 changes: 0 additions & 109 deletions WmiLight/Internal/Attribute/ExceptionAttribute.cs

This file was deleted.

14 changes: 0 additions & 14 deletions WmiLight/Internal/Attribute/HResultAttribute.cs

This file was deleted.

139 changes: 32 additions & 107 deletions WmiLight/Internal/HResult.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
namespace WmiLight
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using WmiLight.Wbem;

#region Description
/// <summary>
Expand Down Expand Up @@ -111,32 +109,16 @@ internal struct HResult : IEquatable<HResult>
{
#region Fields

#region Description
/// <summary>
/// The lock object to sync parallel access to the <see cref="HResult.map"/> field.
/// </summary>
#endregion
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static readonly object MapLockObj = new object();

#region Description
/// <summary>
/// The Exception-Map.
/// </summary>
#endregion
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static Dictionary<HResult, Tuple<Enum, ExceptionAttribute>> map;

#region Description
/// <summary>
/// The value ob the <see cref="HResult"/> as <see cref="Int32"/>.
/// </summary>
#endregion
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly int value;

#endregion

#region Constructors

#region Description
Expand All @@ -149,8 +131,8 @@ public HResult(int intValue)
{
this.value = intValue;
}
#endregion

#endregion

#region Properties

Expand All @@ -167,45 +149,6 @@ public bool Failed
}
}

#region Description
/// <summary>
/// Gets the exception map.
/// </summary>
#endregion
private static Dictionary<HResult, Tuple<Enum, ExceptionAttribute>> Map
{
get
{
if (HResult.map == null)
{
lock (HResult.MapLockObj)
{
if (HResult.map == null)
{
Dictionary<HResult, Tuple<Enum, ExceptionAttribute>> newToStringValues = new Dictionary<HResult, Tuple<Enum, ExceptionAttribute>>();

foreach (Type hresultEnum in typeof(HResult).Assembly.GetTypes().Where<Type>(t => t.IsEnum && t.GetCustomAttributes(typeof(HResultAttribute), false).Any()))
{
foreach (Enum item in Enum.GetValues(hresultEnum))
{
IEnumerable<ExceptionAttribute> attributes = hresultEnum.GetMember(item.ToString()).First().GetCustomAttributes(typeof(ExceptionAttribute), false) as IEnumerable<ExceptionAttribute>;

if (!newToStringValues.ContainsKey(item.GetHashCode()))
{
newToStringValues[item.GetHashCode()] = new Tuple<Enum, ExceptionAttribute>(item, attributes.FirstOrDefault<ExceptionAttribute>());
}
}
}

HResult.map = newToStringValues;
}
}
}

return HResult.map;
}
}

#endregion

#region Methods
Expand All @@ -223,48 +166,32 @@ public bool Failed
{
if (hr.Failed)
{
Tuple<Enum, ExceptionAttribute> tupel = null;

if (HResult.Map.TryGetValue(hr, out tupel))
switch (hr)
{
if (tupel.Item2 != null)
{
ConstructorInfo ctorInfo = tupel.Item2.ExceptionType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(HResultInfo) }, null);

if (ctorInfo != null)
{
return ctorInfo.Invoke(new object[] { new HResultInfo(hr, tupel.Item2.ExceptionMessage, tupel.Item1.ToString()) }) as Exception;
}
else
{
ctorInfo = tupel.Item2.ExceptionType.GetConstructor(new Type[] { typeof(string) });

if (ctorInfo != null)
{
return ctorInfo.Invoke(new object[] { tupel.Item2.ExceptionMessage }) as Exception;
}
else
{
ctorInfo = tupel.Item2.ExceptionType.GetConstructor(Type.EmptyTypes);

if (ctorInfo != null)
{
return ctorInfo.Invoke(Type.EmptyTypes) as Exception;
}
#if DEBUG
else
{
Debug.Write(string.Format("The exception '{0}' does not contain a supported constructor.", tupel.Item2.ExceptionType.Name), "[WARNING]");
}
#endif
}
}
}

return new WmiException(new HResultInfo(hr, string.Format("Exception from HRESULT: {0} ({1})", "0x" + hr.value.ToString("x"), hr.ToString()), tupel.Item1.ToString()));
}
case (int)WbemStatus.WBEM_E_ACCESS_DENIED:
return new UnauthorizedAccessException("Access is denied.");

return Marshal.GetExceptionForHR(hr.value);
case (int)WbemStatus.WBEM_E_OUT_OF_MEMORY:
return new OutOfMemoryException("There was not enough memory to complete the operation.");

case (int)WbemStatus.WBEM_E_INVALID_PARAMETER:
return new InvalidParameterException(new HResultInfo(hr, "A specified parameter is not valid.", WbemStatus.WBEM_E_INVALID_PARAMETER.ToString()));

case (int)WbemStatus.WBEM_E_INVALID_NAMESPACE:
return new InvalidNamespaceException(new HResultInfo(hr, "The specified namespace did not exist on the server.", WbemStatus.WBEM_E_INVALID_NAMESPACE.ToString()));

case (int)WbemStatus.WBEM_E_TRANSPORT_FAILURE:
return new TransportFailureException(new HResultInfo(hr, "The remote procedure call (RPC) link between the current process and WMI failed.", WbemStatus.WBEM_E_TRANSPORT_FAILURE.ToString()));

case (int)WbemStatus.WBEM_E_LOCAL_CREDENTIALS:
return new LocalCredentialsException(new HResultInfo(hr, "Username, password, or authority can only used on a remote connection.", WbemStatus.WBEM_E_LOCAL_CREDENTIALS.ToString()));

case (int)WbemStatus.WBEM_E_FAILED:
return new WmiException(new HResultInfo(hr, "An unspecified error occurred.", WbemStatus.WBEM_E_FAILED.ToString()));

default:
return Marshal.GetExceptionForHR(hr.value);
}
}

return null;
Expand Down Expand Up @@ -363,7 +290,7 @@ public bool Failed
}

#endregion

#region Description
/// <summary>
/// Serves as a hash function.
Expand Down Expand Up @@ -412,11 +339,9 @@ public bool Equals(HResult hr)
#endregion
public override string ToString()
{
Tuple<Enum, ExceptionAttribute> tupel;

if (HResult.Map.TryGetValue(this, out tupel))
if (Enum.IsDefined(typeof(WbemStatus), this.value))
{
return tupel.Item1.ToString();
return ((WbemStatus)value).ToString();
}
else
{
Expand Down
Loading

0 comments on commit 9c3d829

Please sign in to comment.