예제 #1
0
 public Resource(IntPtr hModule, IntPtr hResource, IntPtr type, IntPtr name, ushort wIDLanguage, int size)
 {
     _hModule   = hModule;
     _type      = ResourceUtil.GetResourceName(type);
     _name      = ResourceUtil.GetResourceName(name);
     _language  = wIDLanguage;
     _hResource = hResource;
     _size      = size;
 }
예제 #2
0
        public void LoadFrom(string filename, IntPtr name, IntPtr type)
        {
            _type = ResourceUtil.GetResourceName(type);
            _name = ResourceUtil.GetResourceName(name);

            IntPtr hModule = IntPtr.Zero;

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

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

                IntPtr hRes = Kernel32.FindResource(hModule, name, type);
                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());
                }

                Read(hModule, lpRes);
            }
            finally
            {
                if (hModule != IntPtr.Zero)
                {
                    Kernel32.FreeLibrary(hModule);
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Enumerate resource types
        /// </summary>
        private bool EnumResourceTypesImpl(IntPtr hModule, IntPtr lpszType, IntPtr lParam)
        {
            string typename = ResourceUtil.GetResourceName(lpszType);

            _resourceTypes.Add(typename);

            // enumerate resource names
            if (!Kernel32.EnumResourceNames(hModule, lpszType, new Kernel32.EnumResourceNamesDelegate(EnumResourceNamesImpl), IntPtr.Zero))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            return(true);
        }
예제 #4
0
        /// <summary>
        /// Enumerate resource languages within a resource by name
        /// </summary>
        private bool EnumResourceLanguages(IntPtr hModule, IntPtr lpszType, IntPtr lpszName, ushort wIDLanguage, IntPtr lParam)
        {
            string type = ResourceUtil.GetResourceName(lpszType);

            List <Resource> resources = null;

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

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

            Resource rc = null;

            switch (ResourceUtil.GetResourceName(lpszType))
            {
            case "16":     // Kernel32.RT_VERSION:
                rc = new VersionResource(hModule, hResourceGlobal, lpszType, lpszName, wIDLanguage, size);
                break;

            case "14":     // Kernel32.RT_GROUP_ICON
                rc = new GroupIconResource(hModule, hResourceGlobal, lpszType, lpszName, wIDLanguage, size);
                break;

            // \todo: specialize other resource types
            default:
                rc = new Resource(hModule, hResourceGlobal, lpszType, lpszName, wIDLanguage, size);
                break;
            }

            resources.Add(rc);
            return(true);
        }