/// <summary>
        /// Enumerate resource languages within a resource by name
        /// </summary>
        /// <param name="hModule">Module handle.</param>
        /// <param name="lpszType">Resource type.</param>
        /// <param name="lpszName">Resource name.</param>
        /// <param name="wIDLanguage">Language ID.</param>
        /// <param name="lParam">Additional parameter.</param>
        /// <returns>TRUE if successful.</returns>
        private bool EnumResourceLanguages(IntPtr hModule, IntPtr lpszType, IntPtr lpszName, UInt16 wIDLanguage, IntPtr lParam)
        {
            List <Resource> resources = null;
            ResourceId      type      = new ResourceId(lpszType);

            if (!_resources.TryGetValue(type, out resources))
            {
                resources        = new List <Resource>();
                _resources[type] = resources;
            }

            ResourceId name            = new ResourceId(lpszName);
            IntPtr     hResource       = Kernel32.FindResourceEx(hModule, lpszType, lpszName, wIDLanguage);
            IntPtr     hResourceGlobal = Kernel32.LoadResource(hModule, hResource);
            int        size            = Kernel32.SizeofResource(hModule, hResource);

            using (var aligned = new Aligned(hResourceGlobal, size))
            {
                try
                {
                    resources.Add(CreateResource(hModule, aligned.Ptr, type, name, wIDLanguage, size));
                }
                catch (Exception ex)
                {
                    _innerException = new Exception(string.Format("Error loading resource '{0}' {1} ({2}).",
                                                                  name, type.TypeName, wIDLanguage), ex);
                    throw ex;
                }
            }

            return(true);
        }
示例#2
0
        /// <summary>
        /// Lock and read the resource.
        /// </summary>
        /// <param name="hModule">Module handle.</param>
        /// <param name="hResource">Resource handle.</param>
        internal void LockAndReadResource(IntPtr hModule, IntPtr hResource)
        {
            if (hResource == IntPtr.Zero)
            {
                return;
            }

            IntPtr lpRes = Kernel32.LockResource(hResource);

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

            using (var aligned = new Aligned(lpRes, _size))
                Read(hModule, aligned.Ptr);
        }
示例#3
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());
            }

            using (var aligned = new Aligned(lpRes, _size))
            {
                _type     = type;
                _name     = name;
                _language = lang;

                Read(hModule, aligned.Ptr);
            }
        }