public static IEnumerable <ExecutableManifest> GetManifests(string filename) { string fullpath = Path.GetFullPath(filename); using (SafeLoadLibraryHandle library = SafeLoadLibraryHandle.LoadLibrary(fullpath, LoadLibraryFlags.LoadLibraryAsImageResource | LoadLibraryFlags.LoadLibraryAsDataFile)) { List <ExecutableManifest> manifests = new List <ExecutableManifest>(); EnumResourceNames(library, new IntPtr((int)ResType.MANIFEST), (a, b, c, d) => { try { manifests.Add(new ExecutableManifest(library, fullpath, c)); } catch (Win32Exception) { } catch (ArgumentException) { } return(true); }, IntPtr.Zero); return(manifests); } }
/// <summary> /// Load a library into memory. /// </summary> /// <param name="name">The path to the library.</param> /// <param name="flags">Additonal flags to pass to LoadLibraryEx</param> /// <returns></returns> public static SafeLoadLibraryHandle LoadLibrary(string name, LoadLibraryFlags flags) { SafeLoadLibraryHandle ret = LoadLibraryEx(name, IntPtr.Zero, flags); if (ret.IsInvalid) { throw new Win32Exception(); } return(ret); }
internal ExecutableManifest(SafeLoadLibraryHandle hModule, string fullpath, IntPtr hName) { FullPath = fullpath; IntPtr hResHandle = FindResource(hModule, hName, new IntPtr((int)ResType.MANIFEST)); if (hResHandle == IntPtr.Zero) { throw new ArgumentException("Can't find manifest resource"); } IntPtr hResource = LoadResource(hModule, hResHandle); IntPtr buf = LockResource(hResource); int size = SizeofResource(hModule, hResHandle); if (size <= 0) { throw new ArgumentException("Invalid manifest size"); } byte[] manifest = new byte[size]; Marshal.Copy(buf, manifest, 0, size); MemoryStream stm = new MemoryStream(manifest); try { XmlDocument doc = LoadDocument(stm); UiAccess = GetUiAccess(doc); AutoElevate = GetAutoElevate(doc); ExecutionLevel = GetExecutionLevel(doc); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.OmitXmlDeclaration = true; settings.NewLineOnAttributes = true; StringWriter string_writer = new StringWriter(); XmlWriter writer = XmlWriter.Create(string_writer, settings); doc.Save(writer); ManifestXml = string_writer.ToString(); } catch (XmlException) { ParseError = true; ManifestXml = Encoding.UTF8.GetString(stm.ToArray()); } }
private static extern IntPtr FindResource(SafeLoadLibraryHandle hModule, IntPtr lpName, IntPtr lpType);
private static extern int SizeofResource(SafeLoadLibraryHandle hModule, IntPtr hResInfo);
private static extern bool EnumResourceNames(SafeLoadLibraryHandle hModule, IntPtr lpszType, EnumResNameProcDelegate lpEnumFunc, IntPtr lParam);