Example #1
0
        public static IEnumerable <XElement> LoadFrom(string filename)
        {
            var manifests = new List <XElement>();

            using (DisposableModule dll = NativeMethods.LoadLibraryEx(filename, Unused.Nothing, LoadLibraryFlags.AsImageResource | LoadLibraryFlags.AsDatafile)) {
                // if we get back a valid module handle
                if (!dll.IsInvalid)
                {
                    // search all the 'manifest' resources
                    if (NativeMethods.EnumResourceNamesEx(dll, ResourceType.Manifest, (m, type, id, param) => {
                        // for each manifest, check the language
                        NativeMethods.EnumResourceLanguagesEx(m, type, id, (m1, resourceType, resourceId, language, unused) => {
                            // find the specific resource
                            var resource = NativeMethods.FindResourceEx(m1, resourceType, resourceId, language);
                            if (!resource.IsInvalid)
                            {
                                // get a handle to the resource data
                                var resourceData = NativeMethods.LoadResource(m1, resource);
                                if (!resourceData.IsInvalid)
                                {
                                    // copy the resource text out of the resource data
                                    try {
                                        var dataSize = NativeMethods.SizeofResource(m1, resource);
                                        var dataPointer = NativeMethods.LockResource(resourceData);

                                        // make sure that the pointer and size are legit.
                                        if (dataSize > 0 && dataPointer != IntPtr.Zero)
                                        {
                                            var data = new byte[dataSize];
                                            Marshal.Copy(dataPointer, data, 0, data.Length);
                                            var bomPresent = (data.Length >= 3 && data[0] == _utf[0] && data[1] == _utf[1] && data[2] == _utf[2]);

                                            // create an XElement for the data returned.
                                            // IIRC, manifests are always UTF-8, n'est-ce pas?
                                            manifests.Add(XElement.Parse(Encoding.UTF8.GetString(data, bomPresent ? 3 : 0, bomPresent ? data.Length - 3 : data.Length)));
                                        }
                                    } catch {
                                        // skip it if it doesn't load.
                                    }
                                }
                            }
                            return(true);
                        }, Unused.Nothing, ResourceEnumFlags.None, LanguageId.None);

                        return(true);
                    }, Unused.Nothing, ResourceEnumFlags.None, 0))
                    {
                    }
                }
            }
            return(manifests);
        }
Example #2
0
        static ShellApplication()
        {
            try {
                using (DisposableModule instance = NativeMethods.LoadLibrary("shell32.dll")) {
                    if (!instance.IsInvalid)
                    {
                        var buffer = new StringBuilder(0x100);

                        if (NativeMethods.LoadString(instance, 0x150a, buffer, buffer.Capacity) > 0)
                        {
                            _pin = buffer.ToString();
                        }
                        if (NativeMethods.LoadString(instance, 0x150b, buffer, buffer.Capacity) > 0)
                        {
                            _unpin = buffer.ToString();
                        }
                    }
                }
            } catch {
                // whoa, something went wrong. Let it be.
            }
        }