2017-11-27 12:18:01 -05:00
using System ;
using System.Runtime.InteropServices ;
2018-08-18 21:38:27 -04:00
using Vanara.Extensions ;
2017-11-27 12:18:01 -05:00
using Vanara.InteropServices ;
using static Vanara . PInvoke . Macros ;
2020-09-03 12:51:23 -04:00
#pragma warning disable IDE1006 // Naming Styles
2017-11-27 12:18:01 -05:00
namespace Vanara.PInvoke
{
/// <summary>Predefined resource types.</summary>
2019-09-05 21:59:25 -04:00
[PInvokeData("winuser.h")]
2017-11-27 12:18:01 -05:00
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>
2018-11-19 23:18:50 -05:00
/// 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.
2017-11-27 12:18:01 -05:00
/// </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:38:27 -04:00
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
2019-09-05 21:59:25 -04:00
[PInvokeData("winuser.h")]
2018-11-19 23:18:50 -05:00
public struct ResourceId : IEquatable < string > , IEquatable < IntPtr > , IEquatable < int > , IEquatable < ResourceId > , IHandle
2017-11-27 12:18:01 -05:00
{
2018-11-19 23:18:50 -05:00
private IntPtr ptr ;
2017-11-27 12:18:01 -05:00
/// <summary>Gets or sets an integer identifier.</summary>
/// <value>The identifier.</value>
public int id
{
get = > IS_INTRESOURCE ( ptr ) ? ( ushort ) ptr . ToInt32 ( ) : 0 ;
set
{
2022-10-10 11:27:03 -04:00
if ( value is > ushort . MaxValue or < = 0 ) throw new ArgumentOutOfRangeException ( nameof ( id ) ) ;
2017-11-27 12:18:01 -05:00
ptr = ( IntPtr ) ( ushort ) value ;
}
}
2020-11-27 12:32:29 -05:00
/// <summary>Determines whether this value is an integer identifier for a resource.</summary>
/// <returns>If the value is a resource identifier, the return value is <see langword="true"/>. Otherwise, the return value is <see langword="false"/>.</returns>
public bool IsIntResource = > IS_INTRESOURCE ( ptr ) ;
2017-11-27 12:18:01 -05:00
/// <summary>Represent a NULL value.</summary>
2022-10-10 11:27:03 -04:00
public static readonly ResourceId NULL = new ( ) ;
2017-11-27 12:18:01 -05:00
2022-10-10 11:27:03 -04:00
/// <inheritdoc/>
public static bool operator = = ( ResourceId left , ResourceId right ) = > left . Equals ( right ) ;
/// <inheritdoc/>
public static bool operator ! = ( ResourceId left , ResourceId right ) = > ! left . Equals ( right ) ;
/// <summary>Performs an implicit conversion from <see cref="SafeResourceId"/> to <see cref="int"/>.</summary>
2017-11-27 12:18:01 -05:00
/// <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="int"/> to <see cref="ResourceId"/>.</summary>
/// <param name="resId">The resource identifier.</param>
/// <returns>The result of the conversion.</returns>
2022-10-10 11:27:03 -04:00
public static implicit operator ResourceId ( int resId ) = > new ( ) { id = resId } ;
2017-11-27 12:18:01 -05:00
/// <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>
2022-10-10 11:27:03 -04:00
public static implicit operator ResourceId ( ResourceType resType ) = > new ( ) { id = ( int ) resType } ;
2017-11-27 12:18:01 -05:00
/// <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>
2022-10-10 11:27:03 -04:00
public static implicit operator ResourceId ( IntPtr p ) = > new ( ) { ptr = p } ;
2017-11-27 12:18:01 -05:00
/// <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>
2018-11-19 23:18:50 -05:00
public static explicit operator string ( ResourceId r ) = > r . ToString ( ) ;
2017-11-27 12:18:01 -05:00
2022-10-10 11:27:03 -04:00
/// <summary>Determines whether the specified <see cref="object"/>, is equal to this instance.</summary>
/// <param name="obj">The <see cref="object"/> to compare with this instance.</param>
/// <returns><c>true</c> if the specified <see cref="object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
2017-11-27 12:18:01 -05:00
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 ) ;
2019-11-04 14:58:33 -05:00
case IHandle h :
return Equals ( h . DangerousGetHandle ( ) ) ;
2017-11-27 12:18:01 -05:00
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>
2018-11-28 09:46:19 -05:00
public bool Equals ( int other ) = > ptr . ToInt32 ( ) . Equals ( 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>
/// <exception cref="NotImplementedException"></exception>
2018-11-28 09:46:19 -05: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-11-28 09:46:19 -05:00
public bool Equals ( IntPtr other ) = > ptr . Equals ( 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>
/// <exception cref="NotImplementedException"></exception>
2018-11-28 09:46:19 -05:00
public bool Equals ( ResourceId other ) = > string . Equals ( other . ToString ( ) , ToString ( ) ) ;
2017-11-27 12:18:01 -05:00
2018-11-19 23:18:50 -05:00
/// <inheritdoc/>
public IntPtr DangerousGetHandle ( ) = > ptr ;
2017-11-27 12:18:01 -05:00
}
2019-11-04 14:58:33 -05:00
/// <summary>Helper structure to use for a pointer that can morph into a string, handle or integer.</summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
[PInvokeData("winuser.h")]
public struct ResourceIdOrHandle < THandle > : IEquatable < string > , IEquatable < int > , IEquatable < ResourceIdOrHandle < THandle > > , IHandle where THandle : IHandle
{
private 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
{
2022-10-10 11:27:03 -04:00
if ( value is > ushort . MaxValue or < = 0 ) throw new ArgumentOutOfRangeException ( nameof ( id ) ) ;
2019-11-04 14:58:33 -05:00
ptr = ( IntPtr ) ( ushort ) value ;
}
}
/// <summary>Represent a NULL value.</summary>
2022-10-10 11:27:03 -04:00
public static readonly ResourceIdOrHandle < THandle > NULL = new ( ) ;
/// <inheritdoc/>
public static bool operator = = ( ResourceIdOrHandle < THandle > left , ResourceIdOrHandle < THandle > right ) = > left . Equals ( right ) ;
/// <inheritdoc/>
public static bool operator ! = ( ResourceIdOrHandle < THandle > left , ResourceIdOrHandle < THandle > right ) = > ! left . Equals ( right ) ;
2019-11-04 14:58:33 -05:00
2022-10-10 11:27:03 -04:00
/// <summary>Performs an implicit conversion from <see cref="SafeResourceId"/> to <see cref="int"/>.</summary>
2019-11-04 14:58:33 -05:00
/// <param name="r">The r.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator int ( ResourceIdOrHandle < THandle > r ) = > r . id ;
/// <summary>Performs an implicit conversion from <see cref="ResourceIdOrHandle{THandle}"/> to <see cref="IntPtr"/>.</summary>
/// <param name="r">The r.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator IntPtr ( ResourceIdOrHandle < THandle > r ) = > r . ptr ;
/// <summary>Performs an implicit conversion from <see cref="int"/> to <see cref="ResourceIdOrHandle{THandle}"/>.</summary>
/// <param name="resId">The resource identifier.</param>
/// <returns>The result of the conversion.</returns>
2022-10-10 11:27:03 -04:00
public static implicit operator ResourceIdOrHandle < THandle > ( int resId ) = > new ( ) { id = resId } ;
2019-11-04 14:58:33 -05:00
/// <summary>Performs an implicit conversion from <see cref="ResourceType"/> to <see cref="ResourceIdOrHandle{THandle}"/>.</summary>
/// <param name="resType">Type of the resource.</param>
/// <returns>The result of the conversion.</returns>
2022-10-10 11:27:03 -04:00
public static implicit operator ResourceIdOrHandle < THandle > ( ResourceType resType ) = > new ( ) { id = ( int ) resType } ;
2019-11-04 14:58:33 -05:00
/// <summary>Performs an implicit conversion from <see cref="IntPtr"/> to <see cref="ResourceIdOrHandle{THandle}"/>.</summary>
/// <param name="p">The PTR.</param>
/// <returns>The result of the conversion.</returns>
2022-10-10 11:27:03 -04:00
public static implicit operator ResourceIdOrHandle < THandle > ( THandle p ) = > new ( ) { ptr = p . DangerousGetHandle ( ) } ;
2019-11-04 14:58:33 -05:00
/// <summary>Performs an implicit conversion from <see cref="ResourceIdOrHandle{THandle}"/> to <see cref="string"/>.</summary>
/// <param name="r">The r.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator string ( ResourceIdOrHandle < THandle > r ) = > r . ToString ( ) ;
2022-10-10 11:27:03 -04:00
/// <summary>Determines whether the specified <see cref="object"/>, is equal to this instance.</summary>
/// <param name="obj">The <see cref="object"/> to compare with this instance.</param>
/// <returns><c>true</c> if the specified <see cref="object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
2019-11-04 14:58:33 -05:00
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 ResourceIdOrHandle < THandle > r :
return Equals ( r ) ;
case IHandle h :
return Equals ( h . DangerousGetHandle ( ) ) ;
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>
public bool 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>
public bool 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>
public bool 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>
public bool Equals ( ResourceIdOrHandle < THandle > other ) = > string . Equals ( other . ToString ( ) , ToString ( ) ) ;
/// <inheritdoc/>
public IntPtr DangerousGetHandle ( ) = > ptr ;
}
2017-11-27 12:18:01 -05:00
/// <summary>Represents a system resource name that can identify as a string, integer, or pointer.</summary>
/// <seealso cref="SafeHandle"/>
2018-11-19 23:18:50 -05:00
public class SafeResourceId : GenericSafeHandle , IEquatable < string > , IEquatable < int > , IEquatable < SafeResourceId > , IEquatable < ResourceId > , IEquatable < IntPtr > , IHandle
2017-11-27 12:18:01 -05:00
{
2018-08-18 21:38:27 -04:00
/// <summary>Represent a NULL value.</summary>
2022-10-10 11:27:03 -04:00
public static readonly SafeResourceId Null = new ( ) ;
2018-08-18 21:38:27 -04:00
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-11-28 14:33:55 -05:00
/// <param name="charSet">The character set.</param>
/// <exception cref="System.ArgumentNullException">resName</exception>
2018-08-18 21:38:27 -04:00
public SafeResourceId ( string resName , CharSet charSet = CharSet . Auto )
2017-11-27 12:18:01 -05:00
{
if ( string . IsNullOrEmpty ( resName ) ) throw new ArgumentNullException ( nameof ( resName ) ) ;
2018-08-18 21:38:27 -04:00
CharSet = charSet ;
SetHandle ( StringHelper . AllocString ( resName , charSet ) ) ;
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>
2020-02-20 15:55:34 -05:00
public SafeResourceId ( int resId ) = > id = resId ;
2017-11-27 12:18:01 -05:00
/// <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:38:27 -04:00
public SafeResourceId ( IntPtr ptr )
2017-11-27 12:18:01 -05:00
{
if ( IS_INTRESOURCE ( ptr ) )
SetHandle ( ptr ) ;
else
{
2018-08-18 21:38:27 -04:00
var s = StringHelper . GetString ( ptr , CharSet ) ;
2017-11-27 12:18:01 -05:00
if ( s ! = null )
2018-08-18 21:38:27 -04:00
SetHandle ( StringHelper . AllocString ( s , CharSet ) ) ;
2017-11-27 12:18:01 -05:00
}
}
2018-08-18 21:38:27 -04:00
/// <summary>Initializes a new instance of the <see cref="SafeResourceId"/> class.</summary>
protected SafeResourceId ( ) { }
/// <summary>Gets or sets the character set to use on resource strings.</summary>
/// <value>The character set.</value>
public virtual CharSet CharSet { get ; set ; } = CharSet . Auto ;
/// <summary>Gets or sets an integer identifier.</summary>
/// <value>The identifier.</value>
public int id
{
get = > IsIntResource ? ( ushort ) handle . ToInt32 ( ) : 0 ;
set
{
2022-10-10 11:27:03 -04:00
if ( value is > short . MaxValue or < short . MinValue ) throw new ArgumentOutOfRangeException ( nameof ( id ) ) ;
2018-11-28 09:46:19 -05:00
InternalCloseMethod ( handle ) ;
2020-02-20 15:55:34 -05:00
SetHandle ( ( IntPtr ) unchecked ( ( ushort ) value ) ) ;
2018-08-18 21:38:27 -04:00
}
}
/// <summary>Gets a value indicating whether this instance is an integer-based resource.</summary>
/// <value><c>true</c> if this instance is an integer-based resource; otherwise, <c>false</c>.</value>
public bool IsIntResource = > IS_INTRESOURCE ( handle ) ;
2018-11-19 23:18:50 -05:00
/// <inheritdoc/>
public override bool IsInvalid = > handle = = IntPtr . Zero ;
2018-08-18 21:38:27 -04:00
/// <inheritdoc/>
2018-11-28 09:46:19 -05:00
protected override Func < IntPtr , bool > CloseMethod = > InternalCloseMethod ;
2018-08-18 21:38:27 -04:00
2018-11-19 23:18:50 -05:00
/// <summary>Gets the string representation of a resource identifier.</summary>
/// <param name="ptr">The resource identifier.</param>
/// <param name="charSet">The character set.</param>
/// <returns>The string representation.</returns>
public static string GetString ( IntPtr ptr , CharSet charSet = CharSet . Auto ) = > IS_INTRESOURCE ( ptr ) ? $"#{ptr.ToInt32()}" : StringHelper . GetString ( ptr , charSet ) ;
2022-10-10 11:27:03 -04:00
/// <summary>Performs an implicit conversion from <see cref="SafeResourceId"/> to <see cref="int"/>.</summary>
2017-11-27 12:18:01 -05:00
/// <param name="r">The r.</param>
/// <returns>The result of the conversion.</returns>
2018-08-18 21:38:27 -04:00
public static implicit operator int ( SafeResourceId r ) = > r . IsIntResource ? ( ushort ) r . handle . ToInt32 ( ) : 0 ;
2017-11-27 12:18:01 -05:00
2018-11-19 23:18:50 -05:00
/// <summary>Performs an implicit conversion from <see cref="SafeResourceId"/> to <see cref="ResourceId"/>.</summary>
/// <param name="h">The safe handle instance.</param>
2017-11-27 12:18:01 -05:00
/// <returns>The result of the conversion.</returns>
2018-11-19 23:18:50 -05:00
public static implicit operator ResourceId ( SafeResourceId h ) = > h . handle ;
2017-11-27 12:18:01 -05:00
/// <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>
2022-10-10 11:27:03 -04:00
public static implicit operator SafeResourceId ( string resName ) = > new ( resName ) ;
2017-11-27 12:18:01 -05:00
2022-10-10 11:27:03 -04:00
/// <summary>Performs an implicit conversion from <see cref="int"/> to <see cref="SafeResourceId"/>.</summary>
2017-11-27 12:18:01 -05:00
/// <param name="resId">The resource identifier.</param>
/// <returns>The result of the conversion.</returns>
2022-10-10 11:27:03 -04:00
public static implicit operator SafeResourceId ( int resId ) = > new ( resId ) ;
2017-11-27 12:18:01 -05:00
/// <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>
2022-10-10 11:27:03 -04:00
public static implicit operator SafeResourceId ( ResourceType resType ) = > new ( resType ) ;
2017-11-27 12:18:01 -05:00
/// <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>
2022-10-10 11:27:03 -04:00
public static implicit operator SafeResourceId ( IntPtr ptr ) = > new ( ptr ) ;
2017-11-27 12:18:01 -05:00
/// <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 ( ) ;
2018-11-28 09:46:19 -05:00
/// <summary>
/// Gets a cloned handle that also, if a string resource, copies the string to a new handle which must be released using StringHelper.FreeString.
/// </summary>
/// <returns>A safe copy of this resource id.</returns>
2022-10-10 11:27:03 -04:00
public SafeResourceId Clone ( ) = > new ( handle ) ;
2018-11-28 09:46:19 -05:00
2022-10-10 11:27:03 -04:00
/// <summary>Determines whether the specified <see cref="object"/>, is equal to this instance.</summary>
/// <param name="obj">The <see cref="object"/> to compare with this instance.</param>
/// <returns><c>true</c> if the specified <see cref="object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
2017-11-27 12:18:01 -05:00
public override bool Equals ( object obj )
{
switch ( obj )
{
case null :
return false ;
2018-11-19 23:18:50 -05:00
case ResourceId resId :
return Equals ( resId ) ;
2017-11-27 12:18:01 -05:00
case string s :
return Equals ( s ) ;
2018-08-18 21:38:27 -04:00
case int i :
return Equals ( i ) ;
case IntPtr p :
return Equals ( p ) ;
case SafeResourceId r :
return Equals ( r ) ;
2017-11-27 12:18:01 -05:00
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-11-28 09:46:19 -05: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-11-28 09:46:19 -05: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-11-28 09:46:19 -05:00
public bool Equals ( SafeResourceId other ) = > string . Equals ( other . ToString ( ) , 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-11-28 09:46:19 -05:00
public bool Equals ( ResourceId other ) = > other . Equals ( ( ResourceId ) this ) ;
2017-11-27 12:18:01 -05:00
2018-11-19 23:18:50 -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-11-28 09:46:19 -05:00
public bool Equals ( IntPtr other ) = > new SafeResourceId ( other ) . Equals ( this ) ;
/// <inheritdoc/>
public override int GetHashCode ( ) = > handle . GetHashCode ( ) ;
/// <inheritdoc/>
public override string ToString ( ) = > GetString ( handle , CharSet ) ;
private bool InternalCloseMethod ( IntPtr h )
{
if ( h ! = IntPtr . Zero & & ! IS_INTRESOURCE ( h ) )
StringHelper . FreeString ( h ) ;
return true ;
}
2017-11-27 12:18:01 -05:00
}
}