A resource Id. There're two types of resource Ids, reserved integer numbers (eg. RT_ICON) and custom string names (eg. "CUSTOM").
Пример #1
0
        internal static void WriteFile(
            IntPtr h,
            ResourceId resourceType,
            ResourceId resourceName,
            UInt16 resourceLanguage,
            string input)
        {
            using (FileStream binaryStream = new FileStream(input, FileMode.Open, FileAccess.Read))
            {
                byte[] data = null;

                try
                {
                    data = new byte[binaryStream.Length];
                    binaryStream.Read(data, 0, (int)binaryStream.Length);
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Error reading {0}: {1}", input, ex.Message),
                        ex);
                }

                Write(h, resourceType, resourceName, resourceLanguage, data);
            }
        }
Пример #2
0
 public TestLoadDialogResourceTestDataEntry(
     string filename, ResourceId resourceId, int numberOfDialogControls)
 {
     _filename = filename;
     _resourceId = resourceId;
     _numberOfDialogControls = numberOfDialogControls;
 }
Пример #3
0
 /// <summary>
 /// A new icon resource.
 /// </summary>
 public IconImageResource(ResourceId type)
     : base(IntPtr.Zero,
         IntPtr.Zero,
         type,
         new ResourceId(IntPtr.Zero),
         ResourceUtil.NEUTRALLANGID, 
         Marshal.SizeOf(typeof(Kernel32.GRPICONDIRENTRY)))
 {
 }
Пример #4
0
 /// <summary>
 /// A new string resource of a given block id.
 /// </summary>
 /// <param name="blockId">Block id.</param>
 public StringResource(ResourceId blockId)
     : base(IntPtr.Zero,
         IntPtr.Zero,
         new ResourceId(Kernel32.ResourceTypes.RT_STRING),
         blockId,
         ResourceUtil.NEUTRALLANGID,
         0)
 {
 }
 public void TestResourceTypeResourceId()
 {
     // known resource types
     foreach (Kernel32.ResourceTypes resourceType in Enum.GetValues(typeof(Kernel32.ResourceTypes)))
     {
         ResourceId rid = new ResourceId(resourceType);
         Assert.AreEqual(resourceType, rid.ResourceType);
         Assert.AreEqual(((uint) resourceType).ToString(), rid.Name);
         Assert.AreEqual((IntPtr) resourceType, rid.Id);
     }
 }
Пример #6
0
        /// <summary>
        /// A structured resource embedded in an executable module.
        /// </summary>
        /// <param name="hModule">Module handle.</param>
        /// <param name="hResource">Resource handle.</param>
        /// <param name="type">Resource type.</param>
        /// <param name="name">Resource name.</param>
        /// <param name="language">Language ID.</param>
        /// <param name="size">Resource size.</param>
        internal Resource(IntPtr hModule, IntPtr hResource, ResourceId type, ResourceId name, UInt16 language, int size)
        {
            _hModule = hModule;
            _type = type;
            _name = name;
            _language = language;
            _hResource = hResource;
            _size = size;

            LockAndReadResource(hModule, hResource);
        }
 public void TestIntResourceId()
 {
     // zero resource id
     ResourceId zeroRid = new ResourceId(IntPtr.Zero);
     Assert.AreEqual(IntPtr.Zero, zeroRid.Id);
     Assert.AreEqual("0", zeroRid.Name);
     // known resource types
     foreach (Kernel32.ResourceTypes resourceType in Enum.GetValues(typeof(Kernel32.ResourceTypes)))
     {
         ResourceId rid = new ResourceId((IntPtr)resourceType);
         Assert.AreEqual(new IntPtr((uint)resourceType), rid.Id);
         Assert.AreEqual(((uint)resourceType).ToString(), rid.Name);
     }
 }
Пример #8
0
 /// <summary>
 /// Create a new icon image resource from a file icon.
 /// </summary>
 /// <param name="icon">File icon.</param>
 /// <param name="type">Resource type.</param>
 /// <param name="name">Resource id.</param>
 /// <param name="language">Resource language.</param>
 public IconImageResource(IconFileIcon icon, ResourceId type, ResourceId name, UInt16 language)
 {
     _name = name;
     _type = type;
     _language = language;
     _header.bColors = icon.Header.bColors;
     _header.bHeight = icon.Header.bHeight;
     _header.bReserved = icon.Header.bReserved;
     _header.bWidth = icon.Header.bWidth;
     _header.dwImageSize = icon.Header.dwImageSize;
     _header.wBitsPerPixel = icon.Header.wBitsPerPixel;
     _header.wPlanes = icon.Header.wPlanes;
     _header.nID = (UInt16) name.Id;
     _image = new DeviceIndependentBitmap(icon.Image);
 }
 public void TestStringResourceId()
 {
     // empty resource id
     ResourceId emptyRid = new ResourceId(String.Empty);
     Assert.AreEqual(String.Empty, emptyRid.Name);
     Assert.AreEqual(String.Empty, Marshal.PtrToStringUni(emptyRid.Id));
     // known resource types
     foreach (Kernel32.ResourceTypes resourceType in Enum.GetValues(typeof(Kernel32.ResourceTypes)))
     {
         ResourceId rid = new ResourceId((IntPtr) resourceType);
         Assert.AreEqual(resourceType, rid.ResourceType);
         Assert.AreEqual(((uint) resourceType).ToString(), rid.Name);
     }
     // string resource types
     ResourceId stringRid = new ResourceId("CUSTOM");
     Assert.AreEqual("CUSTOM", stringRid.Name);
     Assert.AreEqual("CUSTOM", Marshal.PtrToStringUni(stringRid.Id));
 }
Пример #10
0
 internal static void Write(
     IntPtr h,
     ResourceId resourceType,
     ResourceId resourceName,
     UInt16 resourceLanguage,
     byte[] buffer)
 {
     try
     {
         if (!UpdateResource(h, resourceType.Id, resourceName.Id, resourceLanguage, buffer, (uint) buffer.Length))
             throw new Win32Exception(Marshal.GetLastWin32Error());
     }
     catch (Exception ex)
     {
         throw new Exception(string.Format("Error writing {0}: {1}", resourceName, ex.Message),
             ex);
     }
 }
Пример #11
0
 /// <summary>
 /// Save a resource to an executable (.exe or .dll) file.
 /// </summary>
 /// <param name="filename">Path to an executable file.</param>
 /// <param name="name">Resource name.</param>
 /// <param name="type">Resource type.</param>
 /// <param name="langid">Language id.</param>
 internal void SaveTo(string filename, ResourceId type, ResourceId name, UInt16 langid)
 {
     byte[] data = WriteAndGetBytes();
     SaveTo(filename, type, name, langid, data);
 }
Пример #12
0
        /// <summary>
        /// Load a resource from an executable (.exe or .dll) module.
        /// </summary>
        /// <param name="hModule">An executable (.exe or .dll) module.</param>
        /// <param name="type">Resource type.</param>
        /// <param name="name">Resource name.</param>
        /// <param name="lang">Resource language.</param>
        internal void LoadFrom(IntPtr hModule, ResourceId type, ResourceId name, UInt16 lang)
        {
            if (IntPtr.Zero == hModule)
                throw new Win32Exception(Marshal.GetLastWin32Error());

            IntPtr hRes = Kernel32.FindResourceEx(hModule, type.Id, name.Id, lang);
            if (IntPtr.Zero == hRes)
                throw new Win32Exception(Marshal.GetLastWin32Error());

            IntPtr hGlobal = Kernel32.LoadResource(hModule, hRes);
            if (IntPtr.Zero == hGlobal)
                throw new Win32Exception(Marshal.GetLastWin32Error());

            IntPtr lpRes = Kernel32.LockResource(hGlobal);

            if (lpRes == IntPtr.Zero)
                throw new Win32Exception(Marshal.GetLastWin32Error());

            _size = Kernel32.SizeofResource(hModule, hRes);
            if (_size <= 0)
                throw new Win32Exception(Marshal.GetLastWin32Error());

            _type = type;
            _name = name;
            _language = lang;

            Read(hModule, lpRes);
        }
Пример #13
0
        /// <summary>
        /// Load a resource from an executable (.exe or .dll) file.
        /// </summary>
        /// <param name="filename">An executable (.exe or .dll) file.</param>
        /// <param name="name">Resource name.</param>
        /// <param name="type">Resource type.</param>
        /// <param name="lang">Resource language.</param>
        internal void LoadFrom(string filename, ResourceId type, ResourceId name, UInt16 lang)
        {
            IntPtr hModule = IntPtr.Zero;

            try
            {
                hModule = Kernel32.LoadLibraryEx(filename, IntPtr.Zero,
                    Kernel32.DONT_RESOLVE_DLL_REFERENCES | Kernel32.LOAD_LIBRARY_AS_DATAFILE);

                LoadFrom(hModule, type, name, lang);
            }
            finally
            {
                if (hModule != IntPtr.Zero)
                    Kernel32.FreeLibrary(hModule);
            }
        }
Пример #14
0
        /// <summary>
        /// Save a resource to an executable (.exe or .dll) file.
        /// </summary>
        /// <param name="filename">Path to an executable file.</param>
        /// <param name="name">Resource name.</param>
        /// <param name="type">Resource type.</param>
        /// <param name="lang">Resource language.</param>
        /// <param name="data">Resource data.</param>
        internal static void SaveTo(string filename, ResourceId type, ResourceId name, UInt16 lang, byte[] data)
        {
            IntPtr h = Kernel32.BeginUpdateResource(filename, false);

            if (h == IntPtr.Zero)
                throw new Win32Exception(Marshal.GetLastWin32Error());

            if (!Kernel32.UpdateResource(h, type.Id, name.Id,
                lang, data, (data == null ? 0 : (uint)data.Length)))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            if (!Kernel32.EndUpdateResource(h, false))
                throw new Win32Exception(Marshal.GetLastWin32Error());
        }
Пример #15
0
 /// <summary>
 /// Delete a resource from an executable (.exe or .dll) file.
 /// </summary>
 /// <param name="filename">Path to an executable file.</param>
 /// <param name="name">Resource name.</param>
 /// <param name="type">Resource type.</param>
 /// <param name="lang">Resource language.</param>
 internal static void Delete(string filename, ResourceId type, ResourceId name, UInt16 lang)
 {
     SaveTo(filename, type, name, lang, null);
 }
Пример #16
0
        /// <summary>
        /// An existing accelerator resource.
        /// </summary>
        /// <param name="hModule">Module handle.</param>
        /// <param name="hResource">Resource ID.</param>
        /// <param name="type">Resource type.</param>
        /// <param name="name">Resource name.</param>
        /// <param name="language">Language ID.</param>
        /// <param name="size">Resource size.</param>
        public AcceleratorResource(IntPtr hModule, IntPtr hResource, ResourceId type, ResourceId name, UInt16 language, int size)
            : base(hModule, hResource, type, name, language, size)
        {

        }
Пример #17
0
 /// <summary>
 /// An existing version resource.
 /// </summary>
 /// <param name="hModule">Module handle.</param>
 /// <param name="hResource">Resource ID.</param>
 /// <param name="type">Resource type.</param>
 /// <param name="name">Resource name.</param>
 /// <param name="language">Language ID.</param>
 /// <param name="size">Resource size.</param>
 public VersionResource(IntPtr hModule, IntPtr hResource, ResourceId type, ResourceId name, UInt16 language, int size)
     : base(hModule, hResource, type, name, language, size)
 {
 }
Пример #18
0
 /// <summary>
 /// An existing font resource.
 /// </summary>
 /// <param name="hModule">Module handle.</param>
 /// <param name="hResource">Resource ID.</param>
 /// <param name="type">Resource type.</param>
 /// <param name="name">Resource name.</param>
 /// <param name="language">Language ID.</param>
 /// <param name="size">Resource size.</param>
 public FontDirectoryResource(IntPtr hModule, IntPtr hResource, ResourceId type, ResourceId name, UInt16 language, int size)
     : base(hModule, hResource, type, name, language, size)
 {
 }
Пример #19
0
 /// <summary>
 /// Convert into an icon resource that can be written into an executable.
 /// </summary>
 /// <param name="icon">Icon image.</param>
 /// <param name="id">Icon Id.</param>
 /// <param name="language">Language.</param>
 /// <returns>An icon resource.</returns>
 public CursorResource(IconFileIcon icon, ResourceId id, UInt16 language)
     : base(icon, new ResourceId(Kernel32.ResourceTypes.RT_CURSOR), id, language)
 {
 }
Пример #20
0
 /// <summary>
 /// A generic resource.
 /// </summary>
 /// <param name="name">Resource name.</param>
 /// <param name="type">Resource type.</param>
 /// <param name="language">Resource language.</param>
 public GenericResource(ResourceId type, ResourceId name, UInt16 language)
 {
     _type = type;
     _name = name;
     _language = language;
 }
Пример #21
0
        /// <summary>
        /// Convert into an icon resource that can be written into an executable.
        /// </summary>
        /// <param name="icon">Icon image.</param>
        /// <param name="id">Icon Id.</param>
        /// <param name="language">Resource language.</param>
        /// <returns>An icon resource.</returns>
        public IconResource(IconFileIcon icon, ResourceId id, UInt16 language)
            : base(icon, new ResourceId(Kernel32.ResourceTypes.RT_ICON), id, language)
        {

        }
Пример #22
0
 public void TestResourceTypeInvalid()
 {
     ResourceId invalidRid = new ResourceId("CUSTOM");
     Console.WriteLine(invalidRid.ResourceType);
 }
Пример #23
0
 /// <summary>
 /// An existing cursor resource.
 /// </summary>
 /// <param name="hModule">Module handle.</param>
 /// <param name="hResource">Resource ID.</param>
 /// <param name="type">Resource type.</param>
 /// <param name="name">Resource name.</param>
 /// <param name="language">Language ID.</param>
 /// <param name="size">Resource size.</param>
 internal CursorResource(IntPtr hModule, IntPtr hResource, ResourceId type, ResourceId name, UInt16 language, int size)
     : base(hModule, hResource, type, name, language, size)
 {
 }
Пример #24
0
        /// <summary>
        /// Get the max icon id currently in the assembly so we don't overwrite
        /// the existing icons with our new icons
        /// </summary>
        private static ushort GetMaxIconId(string assembly)
        {
            using (var info = new ResourceInfo())
            {
                info.Load(assembly);

                ResourceId groupIconId = new ResourceId(Kernel32.ResourceTypes.RT_GROUP_ICON);
                if (info.Resources.ContainsKey(groupIconId))
                {
                    return info.Resources[groupIconId].OfType<IconDirectoryResource>().Min(idr => idr.Icons.Min(icon => icon.Id));
                }
            }
            return 0;
        }