473,725 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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 ResourceExtract er class that hopefully
shows what I am trying to do...To use, just create a ResourceExtract er
instance and call GetIconNames passing the path to an exe or dll file
(C:\Program Files\Internet Explorer\iexplo re.exe is the path I am using to
test with).

Thanks in advance :)

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

using System;
using System.Runtime. InteropServices ;
using System.Collecti ons;
using System.Componen tModel;

namespace Tests.Applicati ons.ResourceVie wer.UI
{
/// <summary>
/// Provides the methods to extract resources from a Win32 binary.
/// </summary>
public class ResourceExtract er
{

#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 EnumResNameDele gate(
IntPtr ModuleHandle,
IntPtr Type,
IntPtr Name,
IntPtr Param
);
#endregion

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

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

[DllImport("kern el32.dll", SetLastError = true)]
private static extern bool FreeLibrary(Int Ptr ModuleHandle);

[
DllImport(
"kernel32.d ll",
EntryPoint = "EnumResourceNa mesW",
CharSet = CharSet.Unicode ,
SetLastError = true
)
]
private static extern bool EnumResourceNam esWithName(
IntPtr ModuleHandle,
ResourceType Type,
EnumResNameDele gate EnumFunc,
IntPtr Param
);

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

[
DllImport(
"kernel32.d ll",
EntryPoint = "EnumResourceNa mesW",
CharSet = CharSet.Unicode ,
SetLastError = true
)
]
private static extern bool EnumResourceNam esWithID(
IntPtr ModuleHandle,
ResourceType Type,
EnumResNameDele gate EnumFunc,
IntPtr Param
);
#endregion

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

/// <summary>
/// Creates a new instance of the <see
cref="ResourceE xtracter"/>
/// class.
/// </summary>
/// <param name="FilePath" ></param>
public ResourceExtract er(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 GetResourceName s(ResourceType. Icon);
}

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

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

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

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

try {
FillResourceNam es(moduleHandle , Type, names);
} finally {
// Cleanup.
FreeLibrary(mod uleHandle);
}

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

/// <summary>
/// Load the specified library and return the handle.
/// </summary>
/// <param name="LibraryPa th">
/// 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 GetLibraryModul e(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.GetLast Win32Error());
} else {
return result;
}
}

/// <summary>
/// Fills the <paramref name="List"/> with all of the resource
names
/// of the specified <paramref name="ResourceT ype"/>.
/// </summary>
/// <param name="ModuleHan dle">
/// 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 FillResourceNam es(
IntPtr ModuleHandle,
ResourceType Type,
ArrayList List
)
{
// Get the ptr to the ArrayList.
GCHandle listHandle = GCHandle.Alloc( List);

try {
// Enumerate the resource names.
bool result = EnumResourceNam esWithID(
ModuleHandle,
Type,
new EnumResNameDele gate(EnumRes),
(IntPtr) listHandle
);

// Check for errors.
if (!result) {
int code = Marshal.GetLast Win32Error();
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(I ntPtr 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.Targ et;

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

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

}
}

Dec 22 '05 #1
0 2979

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

Similar topics

3
4459
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 to extract the card images as files, then load them in pygame as a surface, but I keep getting errors
3
1720
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
5658
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 all the material within the "Resources and Localization..." tutorial. While I'm confident I now know completely how to accomplish my original, simple task of embedding some icons into my assembly and use of the same at runtime, I realize I've a...
3
3587
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 of strings. My problem is that I also have to specify a type. So I usually specify System.String. I have no idea what I would put in there. I also have a difficult time locating information about this topic. Most of the resources I can find...
1
2002
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 app calls the resource, using the following code: System.Resources.ResourceManager resources = new System.Resources.ResourceManager(this.GetType()); this.Icon = (System.Drawing.Icon)resources.GetObject("Scenario.Icon"); The call works fine,...
1
1225
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 that the compiler generates .resource files in the debug build of the application, for all forms except the main one (form1). The .resx file (with the icon in it) exists in the source directory,
3
2009
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 the dll as an embedded resource. In the project tree I added a folder in which I stick the bitmaps (png's actually) and set he compile option for them to EmbeddedResource. But how do I access them from my (vb)source? The docs say:
0
1667
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 the users application (for instance IE). I' m not getting passed the stage where I write to the console catching clicks on my applications form.. I really doubt if this one's possible at all (especially for an API noob like me)..
3
2972
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 these forms have a save button with an icon (along with other common buttons and icons). At this point, we've added the icon to each project. Unfortunately, if we want to change the icon, we now have to go to every project and change it. It would...
0
8889
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.