2017-11-27 12:18:01 -05:00
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using Vanara.InteropServices;
|
|
|
|
using static Vanara.PInvoke.Macros;
|
|
|
|
|
|
|
|
// ReSharper disable InconsistentNaming
|
|
|
|
|
|
|
|
namespace Vanara.PInvoke
|
|
|
|
{
|
|
|
|
/// <summary>Predefined resource types.</summary>
|
|
|
|
public enum ResourceType : ushort
|
|
|
|
{
|
|
|
|
/// <summary>Accelerator table.</summary>
|
|
|
|
RT_ACCELERATOR = 9,
|
|
|
|
|
|
|
|
/// <summary>Animated cursor.</summary>
|
|
|
|
RT_ANICURSOR = 21,
|
|
|
|
|
|
|
|
/// <summary>Animated icon.</summary>
|
|
|
|
RT_ANIICON = 22,
|
|
|
|
|
|
|
|
/// <summary>Bitmap resource.</summary>
|
|
|
|
RT_BITMAP = 2,
|
|
|
|
|
|
|
|
/// <summary>Hardware-dependent cursor resource.</summary>
|
|
|
|
RT_CURSOR = 1,
|
|
|
|
|
|
|
|
/// <summary>Dialog box.</summary>
|
|
|
|
RT_DIALOG = 5,
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Allows a resource editing tool to associate a string with an .rc file. Typically, the string is the name of the header file that provides
|
|
|
|
/// symbolic names. The resource compiler parses the string but otherwise ignores the value.
|
|
|
|
/// </summary>
|
|
|
|
RT_DLGINCLUDE = 17,
|
|
|
|
|
|
|
|
/// <summary>Font resource.</summary>
|
|
|
|
RT_FONT = 8,
|
|
|
|
|
|
|
|
/// <summary>Font directory resource.</summary>
|
|
|
|
RT_FONTDIR = 7,
|
|
|
|
|
|
|
|
/// <summary>Hardware-independent cursor resource.</summary>
|
|
|
|
RT_GROUP_CURSOR = 12,
|
|
|
|
|
|
|
|
/// <summary>Hardware-independent icon resource.</summary>
|
|
|
|
RT_GROUP_ICON = 14,
|
|
|
|
|
|
|
|
/// <summary>HTML resource.</summary>
|
|
|
|
RT_HTML = 23,
|
|
|
|
|
|
|
|
/// <summary>Hardware-dependent icon resource.</summary>
|
|
|
|
RT_ICON = 3,
|
|
|
|
|
|
|
|
/// <summary>Side-by-Side Assembly Manifest.</summary>
|
|
|
|
RT_MANIFEST = 24,
|
|
|
|
|
|
|
|
/// <summary>Menu resource.</summary>
|
|
|
|
RT_MENU = 4,
|
|
|
|
|
|
|
|
/// <summary>Message-table entry.</summary>
|
|
|
|
RT_MESSAGETABLE = 11,
|
|
|
|
|
|
|
|
/// <summary>Plug and Play resource.</summary>
|
|
|
|
RT_PLUGPLAY = 19,
|
|
|
|
|
|
|
|
/// <summary>Application-defined resource (raw data).</summary>
|
|
|
|
RT_RCDATA = 10,
|
|
|
|
|
|
|
|
/// <summary>String-table entry.</summary>
|
|
|
|
RT_STRING = 6,
|
|
|
|
|
|
|
|
/// <summary>Version resource.</summary>
|
|
|
|
RT_VERSION = 16,
|
|
|
|
|
|
|
|
/// <summary>VXD.</summary>
|
|
|
|
RT_VXD = 20,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Helper structure to use for a pointer that can morph into a string, pointer or integer.</summary>
|
2018-08-18 21:33:40 -04:00
|
|
|
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Auto)]
|
2017-11-27 12:18:01 -05:00
|
|
|
public struct ResourceId : IEquatable<string>, IEquatable<IntPtr>, IEquatable<int>, IEquatable<ResourceId>
|
|
|
|
{
|
2018-08-18 21:33:40 -04:00
|
|
|
/// <summary>The name</summary>
|
|
|
|
[FieldOffset(0)]
|
|
|
|
public string name;
|
|
|
|
|
2017-11-27 12:18:01 -05:00
|
|
|
/// <summary>The PTR</summary>
|
2018-08-18 21:33:40 -04:00
|
|
|
[FieldOffset(0)]
|
2017-11-27 12:18:01 -05:00
|
|
|
public IntPtr ptr;
|
|
|
|
|
|
|
|
/// <summary>Gets or sets an integer identifier.</summary>
|
|
|
|
/// <value>The identifier.</value>
|
|
|
|
public int id
|
|
|
|
{
|
|
|
|
get => IS_INTRESOURCE(ptr) ? (ushort)ptr.ToInt32() : 0;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value > ushort.MaxValue || value <= 0) throw new ArgumentOutOfRangeException(nameof(id));
|
|
|
|
ptr = (IntPtr)(ushort)value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Represent a NULL value.</summary>
|
|
|
|
public static readonly ResourceId Null = new ResourceId();
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="ResourceId"/> to <see cref="int"/>.</summary>
|
|
|
|
/// <param name="r">The r.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator int(ResourceId r) => r.id;
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="ResourceId"/> to <see cref="IntPtr"/>.</summary>
|
|
|
|
/// <param name="r">The r.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator IntPtr(ResourceId r) => r.ptr;
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="string"/> to <see cref="ResourceId"/>.</summary>
|
|
|
|
/// <param name="resName">Name of the resource.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
2018-08-18 21:33:40 -04:00
|
|
|
public static implicit operator ResourceId(string resName) => new ResourceId { name = resName };
|
2017-11-27 12:18:01 -05:00
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="int"/> to <see cref="ResourceId"/>.</summary>
|
|
|
|
/// <param name="resId">The resource identifier.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator ResourceId(int resId) => new ResourceId { id = resId };
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="ResourceType"/> to <see cref="ResourceId"/>.</summary>
|
|
|
|
/// <param name="resType">Type of the resource.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator ResourceId(ResourceType resType) => new ResourceId { id = (int)resType };
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="IntPtr"/> to <see cref="ResourceId"/>.</summary>
|
|
|
|
/// <param name="p">The PTR.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator ResourceId(IntPtr p) => new ResourceId { ptr = p };
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="ResourceId"/> to <see cref="string"/>.</summary>
|
|
|
|
/// <param name="r">The r.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator string(ResourceId r) => r.ToString();
|
|
|
|
|
|
|
|
/// <summary>Determines whether the specified <see cref="System.Object"/>, is equal to this instance.</summary>
|
|
|
|
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
|
|
|
|
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
switch (obj)
|
|
|
|
{
|
|
|
|
case null:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case string s:
|
|
|
|
return Equals(s);
|
|
|
|
|
|
|
|
case int i:
|
|
|
|
return Equals(i);
|
|
|
|
|
|
|
|
case IntPtr p:
|
|
|
|
return Equals(p);
|
|
|
|
|
|
|
|
case ResourceId r:
|
|
|
|
return Equals(r);
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (!obj.GetType().IsPrimitive) return false;
|
|
|
|
try { return Equals(Convert.ToInt32(obj)); } catch { return false; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Returns a hash code for this instance.</summary>
|
|
|
|
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
|
|
|
|
public override int GetHashCode() => ptr.GetHashCode();
|
|
|
|
|
|
|
|
/// <summary>Returns a <see cref="string"/> that represents this instance.</summary>
|
|
|
|
/// <returns>A <see cref="string"/> that represents this instance.</returns>
|
|
|
|
public override string ToString() => IS_INTRESOURCE(ptr) ? $"#{ptr.ToInt32()}" : Marshal.PtrToStringAuto(ptr);
|
|
|
|
|
|
|
|
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
|
|
|
|
/// <param name="other">An object to compare with this object.</param>
|
|
|
|
/// <returns>true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.</returns>
|
|
|
|
bool IEquatable<int>.Equals(int other) => ptr.ToInt32().Equals(other);
|
|
|
|
|
|
|
|
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
|
|
|
|
/// <param name="other">An object to compare with this object.</param>
|
|
|
|
/// <returns>true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.</returns>
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
bool IEquatable<string>.Equals(string other) => string.Equals(ToString(), other);
|
|
|
|
|
|
|
|
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
|
|
|
|
/// <param name="other">An object to compare with this object.</param>
|
|
|
|
/// <returns>true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.</returns>
|
|
|
|
bool IEquatable<IntPtr>.Equals(IntPtr other) => ptr.Equals(other);
|
|
|
|
|
|
|
|
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
|
|
|
|
/// <param name="other">An object to compare with this object.</param>
|
|
|
|
/// <returns>true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.</returns>
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
bool IEquatable<ResourceId>.Equals(ResourceId other) => string.Equals(other.ToString(), ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Helper structure to use for a pointer that can morph into a string, pointer or integer.</summary>
|
2018-08-18 21:33:40 -04:00
|
|
|
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
|
2017-11-27 12:18:01 -05:00
|
|
|
public struct ResourceIdUni : IEquatable<string>, IEquatable<IntPtr>, IEquatable<int>, IEquatable<ResourceIdUni>
|
|
|
|
{
|
2018-08-18 21:33:40 -04:00
|
|
|
/// <summary>The name</summary>
|
|
|
|
[FieldOffset(0)]
|
|
|
|
public string name;
|
|
|
|
|
2017-11-27 12:18:01 -05:00
|
|
|
/// <summary>The PTR</summary>
|
2018-08-18 21:33:40 -04:00
|
|
|
[FieldOffset(0)]
|
2017-11-27 12:18:01 -05:00
|
|
|
public IntPtr ptr;
|
|
|
|
|
|
|
|
/// <summary>Gets or sets an integer identifier.</summary>
|
|
|
|
/// <value>The identifier.</value>
|
|
|
|
public int id
|
|
|
|
{
|
|
|
|
get => IS_INTRESOURCE(ptr) ? (ushort)ptr.ToInt32() : 0;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value > ushort.MaxValue || value <= 0) throw new ArgumentOutOfRangeException(nameof(id));
|
|
|
|
ptr = (IntPtr)(ushort)value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Represent a NULL value.</summary>
|
|
|
|
public static readonly ResourceIdUni Null = new ResourceIdUni();
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="ResourceIdUni"/> to <see cref="int"/>.</summary>
|
|
|
|
/// <param name="r">The r.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator int(ResourceIdUni r) => r.id;
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="ResourceIdUni"/> to <see cref="IntPtr"/>.</summary>
|
|
|
|
/// <param name="r">The r.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator IntPtr(ResourceIdUni r) => r.ptr;
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="string"/> to <see cref="ResourceIdUni"/>.</summary>
|
|
|
|
/// <param name="resName">Name of the resource.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
2018-08-18 21:33:40 -04:00
|
|
|
public static implicit operator ResourceIdUni(string resName) => new ResourceIdUni { name = resName };
|
2017-11-27 12:18:01 -05:00
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="int"/> to <see cref="ResourceIdUni"/>.</summary>
|
|
|
|
/// <param name="resId">The resource identifier.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator ResourceIdUni(int resId) => new ResourceIdUni { id = resId };
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="ResourceType"/> to <see cref="ResourceIdUni"/>.</summary>
|
|
|
|
/// <param name="resType">Type of the resource.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator ResourceIdUni(ResourceType resType) => new ResourceIdUni { id = (int)resType };
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="IntPtr"/> to <see cref="ResourceIdUni"/>.</summary>
|
|
|
|
/// <param name="p">The PTR.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator ResourceIdUni(IntPtr p) => new ResourceIdUni { ptr = p };
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="ResourceIdUni"/> to <see cref="string"/>.</summary>
|
|
|
|
/// <param name="r">The r.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator string(ResourceIdUni r) => r.ToString();
|
|
|
|
|
|
|
|
/// <summary>Determines whether the specified <see cref="System.Object"/>, is equal to this instance.</summary>
|
|
|
|
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
|
|
|
|
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
switch (obj)
|
|
|
|
{
|
|
|
|
case null:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case string s:
|
|
|
|
return Equals(s);
|
|
|
|
|
|
|
|
case int i:
|
|
|
|
return Equals(i);
|
|
|
|
|
|
|
|
case IntPtr p:
|
|
|
|
return Equals(p);
|
|
|
|
|
|
|
|
case ResourceIdUni r:
|
|
|
|
return Equals(r);
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (!obj.GetType().IsPrimitive) return false;
|
|
|
|
try { return Equals(Convert.ToInt32(obj)); } catch { return false; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Returns a hash code for this instance.</summary>
|
|
|
|
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
|
|
|
|
public override int GetHashCode() => ptr.GetHashCode();
|
|
|
|
|
|
|
|
/// <summary>Returns a <see cref="string"/> that represents this instance.</summary>
|
|
|
|
/// <returns>A <see cref="string"/> that represents this instance.</returns>
|
|
|
|
public override string ToString() => IS_INTRESOURCE(ptr) ? $"#{ptr.ToInt32()}" : Marshal.PtrToStringUni(ptr);
|
|
|
|
|
|
|
|
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
|
|
|
|
/// <param name="other">An object to compare with this object.</param>
|
|
|
|
/// <returns>true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.</returns>
|
|
|
|
bool IEquatable<int>.Equals(int other) => ptr.ToInt32().Equals(other);
|
|
|
|
|
|
|
|
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
|
|
|
|
/// <param name="other">An object to compare with this object.</param>
|
|
|
|
/// <returns>true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.</returns>
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
bool IEquatable<string>.Equals(string other) => string.Equals(ToString(), other);
|
|
|
|
|
|
|
|
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
|
|
|
|
/// <param name="other">An object to compare with this object.</param>
|
|
|
|
/// <returns>true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.</returns>
|
|
|
|
bool IEquatable<IntPtr>.Equals(IntPtr other) => ptr.Equals(other);
|
|
|
|
|
|
|
|
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
|
|
|
|
/// <param name="other">An object to compare with this object.</param>
|
|
|
|
/// <returns>true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.</returns>
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
bool IEquatable<ResourceIdUni>.Equals(ResourceIdUni other) => string.Equals(other.ToString(), ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Represents a system resource name that can identify as a string, integer, or pointer.</summary>
|
|
|
|
/// <seealso cref="SafeHandle"/>
|
2018-08-18 21:33:40 -04:00
|
|
|
public sealed class SafeResourceId : GenericSafeHandle, IEquatable<string>, IEquatable<int>, IEquatable<SafeResourceId>, IEquatable<IntPtr>
|
2017-11-27 12:18:01 -05:00
|
|
|
{
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="SafeResourceId"/> class.</summary>
|
|
|
|
/// <param name="resName">Name of the resource.</param>
|
2018-08-18 21:33:40 -04:00
|
|
|
public SafeResourceId(string resName) : base(CloseHandle)
|
2017-11-27 12:18:01 -05:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(resName)) throw new ArgumentNullException(nameof(resName));
|
2018-08-18 21:33:40 -04:00
|
|
|
SetHandle(Marshal.StringToCoTaskMemUni(resName));
|
2017-11-27 12:18:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="SafeResourceId"/> class.</summary>
|
|
|
|
/// <param name="resId">The resource identifier.</param>
|
2018-08-18 21:33:40 -04:00
|
|
|
public SafeResourceId(int resId) : base(CloseHandle)
|
2017-11-27 12:18:01 -05:00
|
|
|
{
|
|
|
|
if (resId > ushort.MaxValue || resId <= 0) throw new ArgumentOutOfRangeException(nameof(resId));
|
|
|
|
SetHandle((IntPtr)(ushort)resId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="SafeResourceId"/> class.</summary>
|
|
|
|
/// <param name="resType">Type of the resource.</param>
|
|
|
|
public SafeResourceId(ResourceType resType) : this((int)resType)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="SafeResourceId"/> class.</summary>
|
|
|
|
/// <param name="ptr">The PTR.</param>
|
2018-08-18 21:33:40 -04:00
|
|
|
public SafeResourceId(IntPtr ptr) : base(CloseHandle)
|
2017-11-27 12:18:01 -05:00
|
|
|
{
|
|
|
|
if (IS_INTRESOURCE(ptr))
|
|
|
|
SetHandle(ptr);
|
|
|
|
else
|
|
|
|
{
|
2018-08-18 21:33:40 -04:00
|
|
|
var s = (string)Marshal.PtrToStringUni(ptr)?.Clone();
|
2017-11-27 12:18:01 -05:00
|
|
|
if (s != null)
|
2018-08-18 21:33:40 -04:00
|
|
|
SetHandle(Marshal.StringToCoTaskMemUni(s));
|
2017-11-27 12:18:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-18 21:33:40 -04:00
|
|
|
/// <summary>When overridden in a derived class, gets a value indicating whether the handle value is invalid.</summary>
|
2017-11-27 12:18:01 -05:00
|
|
|
public override bool IsInvalid => handle == IntPtr.Zero;
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="SafeResourceId"/> to <see cref="System.Int32"/>.</summary>
|
|
|
|
/// <param name="r">The r.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
2018-08-18 21:33:40 -04:00
|
|
|
public static implicit operator int(SafeResourceId r)
|
|
|
|
=> IS_INTRESOURCE(r.handle) ? (ushort)r.handle.ToInt32() : 0;
|
2017-11-27 12:18:01 -05:00
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="SafeResourceId"/> to <see cref="IntPtr"/>.</summary>
|
|
|
|
/// <param name="r">The r.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator IntPtr(SafeResourceId r) => r.handle;
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="string"/> to <see cref="SafeResourceId"/>.</summary>
|
|
|
|
/// <param name="resName">Name of the resource.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator SafeResourceId(string resName) => new SafeResourceId(resName);
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="System.Int32"/> to <see cref="SafeResourceId"/>.</summary>
|
|
|
|
/// <param name="resId">The resource identifier.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator SafeResourceId(int resId) => new SafeResourceId(resId);
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="ResourceType"/> to <see cref="SafeResourceId"/>.</summary>
|
|
|
|
/// <param name="resType">Type of the resource.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator SafeResourceId(ResourceType resType) => new SafeResourceId(resType);
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="IntPtr"/> to <see cref="SafeResourceId"/>.</summary>
|
|
|
|
/// <param name="ptr">The PTR.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator SafeResourceId(IntPtr ptr) => new SafeResourceId(ptr);
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="SafeResourceId"/> to <see cref="string"/>.</summary>
|
|
|
|
/// <param name="r">The r.</param>
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
public static implicit operator string(SafeResourceId r) => r.ToString();
|
|
|
|
|
|
|
|
/// <summary>Determines whether the specified <see cref="System.Object"/>, is equal to this instance.</summary>
|
|
|
|
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
|
|
|
|
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
switch (obj)
|
|
|
|
{
|
|
|
|
case null:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case string s:
|
|
|
|
return Equals(s);
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (!obj.GetType().IsPrimitive) return false;
|
|
|
|
try { return Equals(Convert.ToInt32(obj)); } catch { return false; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
|
|
|
|
/// <param name="other">An object to compare with this object.</param>
|
|
|
|
/// <returns>true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.</returns>
|
2018-08-18 21:33:40 -04:00
|
|
|
public bool Equals(string other) => string.Equals(ToString(), other);
|
2017-11-27 12:18:01 -05:00
|
|
|
|
|
|
|
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
|
|
|
|
/// <param name="other">An object to compare with this object.</param>
|
|
|
|
/// <returns>true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.</returns>
|
2018-08-18 21:33:40 -04:00
|
|
|
public bool Equals(int other) => other == handle.ToInt32();
|
2017-11-27 12:18:01 -05:00
|
|
|
|
|
|
|
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
|
|
|
|
/// <param name="other">An object to compare with this object.</param>
|
|
|
|
/// <returns>true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.</returns>
|
2018-08-18 21:33:40 -04:00
|
|
|
public bool Equals(SafeResourceId other)
|
|
|
|
=> IS_INTRESOURCE(handle) && other != null ? handle == other.handle : Equals(other?.ToString());
|
2017-11-27 12:18:01 -05:00
|
|
|
|
|
|
|
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
|
|
|
|
/// <param name="other">An object to compare with this object.</param>
|
|
|
|
/// <returns>true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.</returns>
|
2018-08-18 21:33:40 -04:00
|
|
|
public bool Equals(IntPtr other) => new SafeResourceId(other).Equals(this);
|
|
|
|
|
|
|
|
/// <summary>Returns a hash code for this instance.</summary>
|
|
|
|
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
|
|
|
|
public override int GetHashCode() => handle.GetHashCode();
|
2017-11-27 12:18:01 -05:00
|
|
|
|
2018-08-18 21:33:40 -04:00
|
|
|
/// <summary>Returns a <see cref="string"/> that represents this instance.</summary>
|
|
|
|
/// <returns>A <see cref="string"/> that represents this instance.</returns>
|
|
|
|
public override string ToString()
|
|
|
|
=> IS_INTRESOURCE(handle) ? $"#{handle.ToInt32()}" : Marshal.PtrToStringUni(handle);
|
|
|
|
|
|
|
|
private static bool CloseHandle(IntPtr handle)
|
|
|
|
{
|
|
|
|
if (handle != IntPtr.Zero && !IS_INTRESOURCE(handle)) Marshal.FreeCoTaskMem(handle);
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-27 12:18:01 -05:00
|
|
|
}
|
|
|
|
}
|