This is a mirror of official site: http://jasper-net.blogspot.com/

Enumerating resources from a native library via WinAPI

| Sunday, January 30, 2011
This weekend I was thinking about writing a tool that would extract icons from a library or executable. As you know, some system libraries (like shell32.dll or user32.dll) have integrated resources, that can be bitmaps, icons, cursors and whatnot. The reason why I needed those icons is to be able to build consistent UIs with the Windows OS instance I am currently running.

It appeared to me a fairly easy task, but there were some interesting nuances I didn't consider before. Initially I thought about getting all image resources from a library at once. LoadImage was the perfect function for this, however it required the user to specify the resource ID and I had none of those. So I needed to somehow get the list of resources. For this purpose, I looked at EnumResourceNames, that, according to MSDN:

Enumerates resources of a specified type within a binary module.

Perfect! I now needed to add a signature that will allow me calling this method, and here is what I initially came up with:

[DllImport("kernel32.dll", SetLastError = true)]
public extern static bool EnumResourceNames(IntPtr hModule, int lpszType, EnumResNameProc lpEnumFunc, IntPtr lParam);

Seems to be the exact implementation, however with some serious flaws, as I will discuss later. What this function required is a handle of the actual library (or executable) that contains the resources. Yet another P/Invoke:

[DllImport("kernel32.dll", SetLastError=true)]
public extern static IntPtr LoadLibraryEx(string lpFileName, IntPtr hFile, int dwFlags);

I got this one right and whenever I wanted to load a specific library for resource analysis, I simply did this:

IntPtr libHandle = LoadLibraryEx(@"C:\windows\system32\shell32.dll", IntPtr.Zero, 2);

Read more: .NET Zone

Posted via email from Jasper-net

0 comments: