using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; using System.ComponentModel; namespace test2Enumressource { class Program { private const uint RT_CURSOR = 0x00000001; private const uint RT_BITMAP = 0x00000002; private const uint RT_ICON = 0x00000003; private const uint RT_MENU = 0x00000004; private const uint RT_DIALOG = 0x00000005; private const uint RT_STRING = 0x00000006; private const uint RT_FONTDIR = 0x00000007; private const uint RT_FONT = 0x00000008; private const uint RT_ACCELERATOR = 0x00000009; private const uint RT_RCDATA = 0x00000010; private const uint RT_MESSAGETABLE = 0x00000011; private const uint LOAD_LIBRARY_AS_DATAFILE = 0x00000002; [DllImport("kernel32.dll", SetLastError = true)] private static extern IntPtr LoadLibraryEx( string lpFileName, IntPtr hFile, uint dwFlags); [DllImport("kernel32.dll", SetLastError = true)] private static extern bool FreeLibrary(IntPtr hModule); [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] static extern bool EnumResourceNames( IntPtr hModule, string lpszType, EnumResNameDelegate lpEnumFunc, IntPtr lParam); #region équivalent des macros private static bool IS_INTRESOURCE(IntPtr value) { if (((uint)value) > ushort.MaxValue) return false; return true; } private static uint GET_RESOURCE_ID(IntPtr value) { if (IS_INTRESOURCE(value) == true) return (uint)value; throw new System.NotSupportedException("value is not an ID!"); } private static string GET_RESOURCE_NAME(IntPtr value) { if (IS_INTRESOURCE(value) == true) return value.ToString(); return Marshal.PtrToStringUni((IntPtr)value); } #endregion équivalent des macros #region Enumération des langues des ressources [DllImport("kernel32.dll")] static extern bool EnumResourceLanguages(IntPtr hModule, IntPtr lpszType, IntPtr lpName, EnumResLangDelegate lpEnumFunc, IntPtr lParam); private delegate bool EnumResLangDelegate( IntPtr hModule, IntPtr lpszType, IntPtr lpszName, ushort wIDLanguage, IntPtr lParam); private static bool EnumResLangCallback( IntPtr hModule, IntPtr lpszType, IntPtr lpszName, ushort wIDLanguage, IntPtr lParam) { Console.WriteLine("\t\tLanguage: {0}", wIDLanguage); return true; } #endregion Enumération des langues des ressources #region Enumération des noms des ressources private delegate bool d_EnumResourceTypes( IntPtr hModule, IntPtr lpszType, long lParam); [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] static extern bool EnumResourceNames( IntPtr hModule, IntPtr lpszType, EnumResNameDelegate lpEnumFunc, IntPtr lParam); private delegate bool EnumResNameDelegate( IntPtr hModule, IntPtr lpszType, IntPtr lpszName, IntPtr lParam); static bool EnumRes( IntPtr hModule, IntPtr lpszType, IntPtr lpszName, IntPtr lParam) { //Console.WriteLine("Type: {0}\tName: {1}", GET_RESOURCE_NAME(lpszType), GET_RESOURCE_NAME(lpszName)); Console.WriteLine("\tName: {0}", GET_RESOURCE_NAME(lpszName)); bool bret = EnumResourceLanguages(hModule, lpszType, lpszName, new EnumResLangDelegate(EnumResLangCallback), lParam); if (bret == false) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return true; } #endregion Enumération des noms des ressources #region Enumération des types des ressources [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] private extern static long EnumResourceTypes(IntPtr hModule, d_EnumResourceTypes callback, long lParam); static bool EnumResourceTypesCallback(IntPtr hModule, IntPtr lpszType, long lParam) { if (IS_INTRESOURCE(lpszType)) { Console.WriteLine("Type : {0}", GET_RESOURCE_ID(lpszType)); } else { Console.WriteLine("Type : {0}", GET_RESOURCE_NAME(lpszType)); } if (EnumResourceNames(hModule,lpszType, new EnumResNameDelegate(EnumRes), IntPtr.Zero) == false) { Console.WriteLine("erreur: {0}", Marshal.GetLastWin32Error()); } return true; } #endregion Enumération des types des ressources [STAThread] static void Main(string[] args) { string file = @"C:\Users\noel\Desktop\mod-themecpl.dll"; file = @"C:\windows\system32\notepad.exe"; if (args.Length == 1){ file = args[0]; } IntPtr hMod = LoadLibraryEx(file, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE); long lParam = 0; EnumResourceTypes(hMod, new d_EnumResourceTypes(EnumResourceTypesCallback), lParam); FreeLibrary(hMod); } } }