Esempio n. 1
0
        public static String RetrieveAssociatedIcon([NotNull] String extensionOrFileOrProtocol, [CanBeNull] out Int32?identifier, out IconIdentifierType identifierType)
        {
            if (String.IsNullOrWhiteSpace(extensionOrFileOrProtocol))
            {
                throw new ArgumentNullException(nameof(extensionOrFileOrProtocol));
            }

            identifier     = null;
            identifierType = IconIdentifierType.Unknown;

            // Special case: The result buffer contains "%1". We treat this case like: "oh hey, your supplied file path is already the file containing the icon!".
            var result = SafeNativeMethods.AssocQueryString(extensionOrFileOrProtocol, NativeMethods.ASSOCSTR.ASSOCSTR_DEFAULTICON);

            if (result == "%1")
            {
                return(String.Empty);
            }

            if (result.IndexOf(',') == -1)
            {
                identifier     = 0;
                identifierType = IconIdentifierType.Index;
                return(result);
            }

            Int32 extractedIdentifier;
            var   file = NativeResourceDescriptor.SplitResourceString(result, out extractedIdentifier);

            identifier     = extractedIdentifier;
            identifierType = IconIdentifier.Identify(extractedIdentifier);
            return(file);
        }
Esempio n. 2
0
        public Icon ExtractIconResource([NotNull] NativeResourceDescriptor nativeResourceDescriptor)
        {
            if (nativeResourceDescriptor == null)
            {
                throw new ArgumentNullException(nameof(nativeResourceDescriptor));
            }

            return(this.ExtractIconResource(nativeResourceDescriptor, Size.Empty));
        }
Esempio n. 3
0
        public Icon ExtractIconResource([NotNull] NativeResourceDescriptor nativeResourceDescriptor, Size desiredSize)
        {
            if (nativeResourceDescriptor == null)
            {
                throw new ArgumentNullException(nameof(nativeResourceDescriptor));
            }

            var resourceId = nativeResourceDescriptor.ResourceId;

            return(this.ExtractIconResource(resourceId, desiredSize));
        }
Esempio n. 4
0
        public String GetResourceString([NotNull] NativeResourceDescriptor nativeResourceDescriptor)
        {
            if (nativeResourceDescriptor == null)
            {
                throw new ArgumentNullException(nameof(nativeResourceDescriptor));
            }

            var resourceId = nativeResourceDescriptor.ResourceId;

            return(this.GetResourceString(resourceId));
        }
Esempio n. 5
0
 public static Boolean TryParseFromResourceString(String resourceString,
                                                  out NativeResourceDescriptor nativeResourceDescriptor)
 {
     try
     {
         nativeResourceDescriptor = NativeResourceDescriptor.ParseFromResourceString(resourceString);
         return(true);
     }
     catch
     {
         nativeResourceDescriptor = null;
         return(false);
     }
 }
Esempio n. 6
0
        public static NativeResourceDescriptor ParseFromResourceString(String resourceString)
        {
            try
            {
                Int32 identifier;
                var   file = NativeResourceDescriptor.SplitResourceString(resourceString, out identifier);

                return(new NativeResourceDescriptor(file, (UInt32)Math.Abs(identifier)));
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Cannot parse '{resourceString}' to NativeResourceDescriptor.", ex);
            }
        }
Esempio n. 7
0
        public IEnumerable <UInt32> EnumerateIconResources(IntPtr resourceType, CancellationToken cancellationToken)
        {
            var resourceIds = new HashSet <UInt32>();

            NativeMethods.EnumResNameProc enumResNameProc = (module, type, value, param) =>
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(false);
                }

                if (NativeExecutable.IsIntResource(value))
                {
                    resourceIds.Add((UInt32)Math.Abs(value.ToInt32()));
                }
                else
                {
                    // value is a pointer to a string which represants the name of the resource
                    var   resourceName = Marshal.PtrToStringAnsi(value);
                    Int32 resourceId;
                    if (NativeResourceDescriptor.TryParseResourceIdentifier(resourceName, out resourceId))
                    {
                        resourceIds.Add((UInt32)Math.Abs(resourceId));
                    }
                }

                return(true);
            };

            var result = NativeMethods.EnumResourceNames(this.Handle, resourceType, enumResNameProc, IntPtr.Zero);
            // This happens if the specified file contains no resources of the desired type.
            var lastWin32Error = Marshal.GetLastWin32Error();

            if (result || lastWin32Error == (Int32)NativeMethods.SystemErrorCode.ERROR_RESOURCE_TYPE_NOT_FOUND || lastWin32Error == (Int32)NativeMethods.SystemErrorCode.ERROR_RESOURCE_ENUM_USER_STOP)
            {
                return(resourceIds);
            }

            throw new Win32Exception(lastWin32Error);
        }