Beispiel #1
0
 public static extern bool EnumResourceNames(IntPtr hModule, ResourceType dwID, EnumResNameProcDelegate lpEnumFunc, IntPtr lParam);
Beispiel #2
0
 internal static extern bool EnumResourceNames(SafeLoadLibraryHandle hModule, IntPtr lpszType,
                                               EnumResNameProcDelegate lpEnumFunc, IntPtr lParam);
        public static MemoryStream GetIconMemoryStreamFromExeFile(string exeFilePath, int iconIndex, ref MemoryStream imageMemoryStream)
        {
            IntPtr iconHandle    = IntPtr.Zero;
            IntPtr libraryHandle = IntPtr.Zero;

            try
            {
                //Load executable file library
                Debug.WriteLine("Loading exe icon: " + exeFilePath);
                libraryHandle = LoadLibraryEx(exeFilePath, IntPtr.Zero, LoadLibraryFlags.LOAD_LIBRARY_AS_DATAFILE);
                if (libraryHandle == IntPtr.Zero)
                {
                    Debug.WriteLine("Failed to load icon from exe: " + exeFilePath);
                    return(null);
                }

                //Enumerate all icon groups
                List <string>           iconGroups = new List <string>();
                EnumResNameProcDelegate EnumResourceNamesCallback = (IntPtr hModule, ResourceTypes lpType, IntPtr lpEnumFunc, IntPtr lParam) =>
                {
                    try
                    {
                        string intPtrString = IntPtrToString(lpEnumFunc);
                        if (!string.IsNullOrWhiteSpace(intPtrString))
                        {
                            iconGroups.Add(intPtrString);
                            return(true);
                        }
                    }
                    catch { }
                    return(false);
                };
                EnumResourceNames(libraryHandle, ResourceTypes.GROUP_ICON, EnumResourceNamesCallback, IntPtr.Zero);

                //Select target icon group
                string iconGroup       = string.Empty;
                int    iconGroupsCount = iconGroups.Count;
                //Debug.WriteLine("Total icon groups: " + iconGroupsCount);
                if (iconGroupsCount > 0 && iconGroupsCount >= iconIndex)
                {
                    iconGroup = iconGroups[iconIndex];
                }
                else
                {
                    Debug.WriteLine("No exe icon found to load.");
                    return(null);
                }

                //Get all icons from group
                List <MEMICONDIRENTRY> iconDirEntryList = new List <MEMICONDIRENTRY>();
                IntPtr iconDirIntPtr = GetResourceDataIntPtrFromString(libraryHandle, iconGroup, ResourceTypes.GROUP_ICON);
                unsafe
                {
                    MEMICONDIR *     iconDir           = (MEMICONDIR *)iconDirIntPtr;
                    MEMICONDIRENTRY *iconDirEntryArray = &iconDir->idEntriesArray;
                    //Debug.WriteLine("Total icons in group: " + iconDir->idCount);
                    for (int entryId = 0; entryId < iconDir->idCount; entryId++)
                    {
                        try
                        {
                            iconDirEntryList.Add(iconDirEntryArray[entryId]);
                        }
                        catch { }
                    }
                }

                //Select largest icon
                MEMICONDIRENTRY iconDirEntry = iconDirEntryList.OrderByDescending(x => x.wBitCount).ThenByDescending(x => x.dwBytesInRes).FirstOrDefault();

                //Get icon bitmap data
                byte[] iconBytes = GetResourceDataBytesFromIntPtr(libraryHandle, (IntPtr)iconDirEntry.nIdentifier, ResourceTypes.ICON);

                //Encode icon bitmap frame
                if (iconBytes[0] == 0x28)
                {
                    //Debug.WriteLine("BMP image: " + iconBytes.Length);

                    //Create icon from the resource
                    iconHandle = CreateIconFromResourceEx(iconBytes, (uint)iconBytes.Length, true, IconVersion.Windows3x, iconDirEntry.bWidth, iconDirEntry.bHeight, IconResourceFlags.LR_DEFAULTCOLOR);

                    //Convert image data to bitmap
                    Bitmap bitmapImage = Icon.FromHandle(iconHandle).ToBitmap();

                    //Write bitmap to memorystream
                    bitmapImage.Save(imageMemoryStream, ImageFormat.Png);
                    imageMemoryStream.Seek(0, SeekOrigin.Begin);
                    return(imageMemoryStream);
                }
                else
                {
                    //Debug.WriteLine("PNG image: " + iconBytes.Length);
                    using (MemoryStream memoryStream = new MemoryStream(iconBytes))
                    {
                        //Convert image data to bitmap
                        Bitmap bitmapImage = new Bitmap(memoryStream);

                        //Write bitmap to memorystream
                        bitmapImage.Save(imageMemoryStream, ImageFormat.Png);
                        imageMemoryStream.Seek(0, SeekOrigin.Begin);
                        return(imageMemoryStream);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to load exe icon: " + ex.Message);
                return(null);
            }
            finally
            {
                if (libraryHandle != IntPtr.Zero)
                {
                    FreeLibrary(libraryHandle);
                }
                if (iconHandle != IntPtr.Zero)
                {
                    DestroyIcon(iconHandle);
                }
            }
        }
Beispiel #4
0
 public static extern bool EnumResourceNames(IntPtr hModule, string lpszType, EnumResNameProcDelegate lpEnumFunc, IntPtr lParam);
 private static extern bool EnumResourceNames(IntPtr hModule, int lpszType, EnumResNameProcDelegate lpEnumFunc, IntPtr lParam);
 private static extern bool EnumResourceNames(IntPtr hModule, int lpszType, EnumResNameProcDelegate lpEnumFunc, IntPtr lParam);
Beispiel #7
0
 static extern bool EnumResourceNames(IntPtr hModule, int dwId, EnumResNameProcDelegate lpEnumFunc, IntPtr lParam);
 public void EnumResourceNames(ResType resType,EnumResNameProcDelegate enumResNameProc)
 {
     EnumResourceNames(handle,resType,enumResNameProc,IntPtr.Zero);
 }
        public static void UpdateVersionResource(string fileName,string tatgetLID,IEnumerable<KeyValuePair<string,string>> datas)
        {
            var id	= default(ID);
            bool isGetResHandle	= false;
            var resHandle	= default(ResHandle);
            using(var loaded = new NativeLibrary(fileName,NativeLibrary.LoadFlags.None))
            {
                var enumResNameProc	= new EnumResNameProcDelegate((IntPtr loadedHandle,ResType lpszType,IntPtr lpszName,IntPtr lParam)=>
                    {
                        if(lpszType != ResType.VERSION)
                            return true;

                        isGetResHandle	= true;
                        resHandle.loadedHandle	= loadedHandle;
                        resHandle.lpszType	= lpszType;
                        resHandle.lpszName	= lpszName;
                        return false;
                    });

                loaded.EnumResourceNames(ResType.VERSION,enumResNameProc);
                if(!isGetResHandle)
                    throw new Exception("No version info in this file");

                FileResourceReader.Read(resHandle.loadedHandle,resHandle.lpszType,resHandle.lpszName,out id);
            }

            var versionModule	= FileResourceReader.Get<VersionInfoReader>(id);
            if(versionModule != null)
            {
                var stringInfoID	= versionModule.Resource[id.Index].StringFileInfoID;
                var allLID	= new List<string>(StringFileInfoReader.GetAllLangID(stringInfoID));
                foreach(var lid in allLID)
                {
                    StringFileInfoReader.AddStringTable(stringInfoID,lid,datas);
                    StringFileInfoReader.CopyLangID(stringInfoID,lid,tatgetLID);
                }

                FileResourceReader.Write(id,ResType.VERSION,resHandle.lpszName,fileName,LangID.ENGLISH_US);
            }
            else throw new Exception("Why we cannot get VersionInfoReader module ?");
        }
Beispiel #10
0
 internal static extern bool EnumResourceNames(IntPtr hModule, IntPtr lpszType, EnumResNameProcDelegate EnumResNameProc, IntPtr lParam);
Beispiel #11
0
 public static extern bool EnumResourceNames(IntPtr hModule, string lpszType, EnumResNameProcDelegate lpEnumFunc, IntPtr lParam);