472,958 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

[API] Win32 Resources - List Icon Resource Names

I wrote some code that is supposed to enumerate through the specified file's
win32 resources and return a string-array of all icon names. When it runs,
it returns a string-array with a bunch of numbers in sequential order (1-55
when ran against iexplore.exe).

When I open up iexplore.exe in Visual Studio, I see 23 icons. Each icon has
1 or more sizes of the icon...I'm assuming that there are, in fact, 55 icon
resources in iexplore.exe, and the code I wrote is returning the indexes of
the icons? If so, how do I get the names/identifiers for the icons?

The following is my code for the ResourceExtracter class that hopefully
shows what I am trying to do...To use, just create a ResourceExtracter
instance and call GetIconNames passing the path to an exe or dll file
(C:\Program Files\Internet Explorer\iexplore.exe is the path I am using to
test with).

Thanks in advance :)

-------------------------------------------------------------------------

using System;
using System.Runtime.InteropServices;
using System.Collections;
using System.ComponentModel;

namespace Tests.Applications.ResourceViewer.UI
{
/// <summary>
/// Provides the methods to extract resources from a Win32 binary.
/// </summary>
public class ResourceExtracter
{

#region Private Members
//
================================================== ===============
// Private Members
//
================================================== ===============

private enum ResourceType : int
{
Cursor = 0x00000001,
Bitmap = 0x00000002,
Icon = 0x00000003,
Menu = 0x00000004,
Dialog = 0x00000005,
String = 0x00000006,
FontDir = 0x00000007,
Font = 0x00000008,
Accelerator = 0x00000009,
RcData = 0x0000000a,
MessageTable = 0x0000000b
}

private const int LOAD_LIBRARY_AS_DATAFILE = 0x00000002;
private const int ERROR_RESOURCE_TYPE_NOT_FOUND = 0x00000715;

private string mFilePath = null;

private delegate bool EnumResNameDelegate(
IntPtr ModuleHandle,
IntPtr Type,
IntPtr Name,
IntPtr Param
);
#endregion

#region API Declarations
//
================================================== ===============
// API Declarations
//
================================================== ===============

[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr LoadLibraryEx(
string FileName,
IntPtr FileHandle,
uint Flags
);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool FreeLibrary(IntPtr ModuleHandle);

[
DllImport(
"kernel32.dll",
EntryPoint = "EnumResourceNamesW",
CharSet = CharSet.Unicode,
SetLastError = true
)
]
private static extern bool EnumResourceNamesWithName(
IntPtr ModuleHandle,
ResourceType Type,
EnumResNameDelegate EnumFunc,
IntPtr Param
);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr FindResource(
IntPtr ModuleHandle,
string Name,
string Type
);

[
DllImport(
"kernel32.dll",
EntryPoint = "EnumResourceNamesW",
CharSet = CharSet.Unicode,
SetLastError = true
)
]
private static extern bool EnumResourceNamesWithID(
IntPtr ModuleHandle,
ResourceType Type,
EnumResNameDelegate EnumFunc,
IntPtr Param
);
#endregion

#region Constructors / Destructors
//
================================================== ===============
// Constructors / Destructors
//
================================================== ===============

/// <summary>
/// Creates a new instance of the <see
cref="ResourceExtracter"/>
/// class.
/// </summary>
/// <param name="FilePath"></param>
public ResourceExtracter(string FilePath)
{
mFilePath = FilePath;
}
#endregion

#region Public Properties
//
================================================== ===============
// Public Properties
//
================================================== ===============

/// <summary>
/// Specifies the path of the Win32 binary.
/// </summary>
public string FilePath
{
get {
return mFilePath;
}
set {
mFilePath = value;
}
}
#endregion

#region Public Methods
//
================================================== ===============
// Public Methods
//
================================================== ===============

/// <summary>
/// Gets the icon resource names for the current file.
/// </summary>
public string[] GetIconNames()
{
return GetResourceNames(ResourceType.Icon);
}

/// <summary>
/// Gets the string resource names for the current file.
/// </summary>
public string[] GetStringNames()
{
return GetResourceNames(ResourceType.String);
}
#endregion

#region Private Methods
//
================================================== ===============
// Private Methods
//
================================================== ===============

private string[] GetResourceNames(ResourceType Type)
{
ArrayList names = new ArrayList();

// Get the handle to the library.
IntPtr moduleHandle = GetLibraryModule(this.FilePath);

try {
FillResourceNames(moduleHandle, Type, names);
} finally {
// Cleanup.
FreeLibrary(moduleHandle);
}

// Convert the names into a string-array and return it.
return (string[]) names.ToArray(typeof(string));
}

/// <summary>
/// Load the specified library and return the handle.
/// </summary>
/// <param name="LibraryPath">
/// The path to the library to load (.dll or .exe).
/// </param>
/// <returns>
/// Returns an <see cref="IntPtr"/> that represents the handle
to
/// the library module loaded.
/// </returns>
private IntPtr GetLibraryModule(string LibraryPath)
{
// Load the library.
IntPtr result = LoadLibraryEx(
LibraryPath,
IntPtr.Zero,
LOAD_LIBRARY_AS_DATAFILE
);

if (result == IntPtr.Zero) {
// Error occurred.
throw new Win32Exception(Marshal.GetLastWin32Error());
} else {
return result;
}
}

/// <summary>
/// Fills the <paramref name="List"/> with all of the resource
names
/// of the specified <paramref name="ResourceType"/>.
/// </summary>
/// <param name="ModuleHandle">
/// The handle to the library module that contains the
resource(s)
/// to retrieve.
/// </param>
/// <param name="Type">The type of resource to retrieve.</param>
/// <param name="List">
/// The <see cref="ArrayList"/> to place the results into.
/// </param>
private void FillResourceNames(
IntPtr ModuleHandle,
ResourceType Type,
ArrayList List
)
{
// Get the ptr to the ArrayList.
GCHandle listHandle = GCHandle.Alloc(List);

try {
// Enumerate the resource names.
bool result = EnumResourceNamesWithID(
ModuleHandle,
Type,
new EnumResNameDelegate(EnumRes),
(IntPtr) listHandle
);

// Check for errors.
if (!result) {
int code = Marshal.GetLastWin32Error();
if (code != ERROR_RESOURCE_TYPE_NOT_FOUND) {
// Raise a Win32Exception for any errors except
// for when the specified resource type could
not
// be found.
throw new Win32Exception(code);
}
}
} finally {
// Release the allocated ArrayList.
listHandle.Free();
}
}

private static bool IsIntResource(IntPtr Value)
{
return (((ulong) Value) >> 16) == 0;
}

private bool EnumRes(
IntPtr ModuleHandle,
IntPtr Type,
IntPtr Name,
IntPtr Param
)
{
GCHandle listHandle = (GCHandle) Param;
ArrayList list = (ArrayList) listHandle.Target;

string name = null;
if (IsIntResource(Name)) {
name = ((int) Name).ToString();
} else {
name = Marshal.PtrToStringAuto(Name);
}

list.Add(name);
return true;
}
#endregion

}
}

Dec 22 '05 #1
0 2887

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tlviewer | last post by:
hello, The script below is a prototype for loading playing card images from the bitmap resource in cards.dll (WinXP) I like the idea of not keeping copies of the images as files. I'm able...
3
by: Christopher Burr | last post by:
I'm trying to retrieve a string resource from a Win32 file. In win32 world I would just use LoadLibrary and LoadString. What do I use in the .NET world? -- Chris Burr cburr@kcc.com
3
by: Kendall Gifford | last post by:
Greetings. While trying to get a simple app working, I've been forced to delve into embedded and/or linked resources a bit. I read all the reference for the System.Resources namespace as well as...
3
by: Shawn B. | last post by:
Greetings, When I add a resource to the project (VS.NET) it places a .resx file. There are many columns in the grid to input data. I just want to specify a Name/Value pair to hold a resource...
1
by: Dave Veeneman | last post by:
My form's resx file is losing resources that I have added with ResEditor.exe (the utility that ships with VS.Net. Here is what is happening: I add the icon to the .resx file with no problems. My...
1
by: Lucvdv | last post by:
In an app that was working, all I did was specify an icon for the main form (through the form designer) and recompile it. Result: MissingManifestResourceException After digging around, I found...
3
by: C-Services Holland b.v. | last post by:
Hi all, I've created a custom button which I can stick into my toolbox. This all works well and I see my button in the designer. This button uses customised graphics and I want to stick it in...
0
by: Nickneem | last post by:
I' m trying to disable all right mouse clicks by using the vbAccelerator Windows Hooks Library The small (systray / console) app. must catch all (right) mouseclicks before they are received by...
3
by: bbrewder | last post by:
I am interested in hearing how other people have handled sharing resources in multiple projects (for example, a save icon). We have a product that has many forms within many projects. Many of...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.