473,383 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Load Resources from Win32 Executables

I am attempting to read all of the resources from a Win32 executable, and
load each of them to display them on a screen. So, please help figure out
why I can't seem to get it right :)

First off, I have my API declarations (will post that at bottom). I make a
call to LoadLibraryEx. Then, if the result is not zero, I make a call to
EnumResourceNames. EnumResourceNamesDelegate (delegate for
EnumResourceNames) is called for every found resource, and this part works
fine. The names and types are passed in, but the names don't match the
actual resource names. They are passed as IntPtr's and event if I change
the type in the API declaration and delegate and function to ... say ..
string, I still get the same value...the value for type is correct (for an
Icon, I get the value of 3...which maps to RT_ICON). But the name is always
an indexed value. Or, what I believe, is the index for the Icon in the
resource list. The first one is always 1, the 2nd is 2, et cetera.

From this point, I get stuck. How can I get the actual name that I need to
use to pass to LoadResource in which the result of that call gets passed as
the handle to System.Drawing.Icon.FromHandle (for Icons),
System.Drawing.Bitmap.FromHbitmap (for Bitmaps), et cetera?

When I call LoadResource, it returns a handle (non-zero). I pass this to
Icon.FromHandle and no exception is thrown. The Height and Width properties
of the Icon are both 0 and when I save the icon to disk, it is 0 bytes (the
file is).

Any and all help is much appreciated...been stuck on this for awhile :)

Thanks,
Mythran

API Declarations that i have...

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 = 0x0000000a;
private const uint RT_MESSAGETABLE = 0x0000000b;

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", EntryPoint = "EnumResourceNamesW", CharSet =
CharSet.Unicode, SetLastError = true)]
static extern bool EnumResourceNamesWithName(
IntPtr hModule,
string lpszType,
EnumResNameDelegate lpEnumFunc,
IntPtr lParam);

[DllImport("kernel32.dll", EntryPoint = "EnumResourceNamesW", CharSet =
CharSet.Unicode, SetLastError = true)]
static extern bool EnumResourceNamesWithID(
IntPtr hModule,
uint lpszType,
EnumResNameDelegate lpEnumFunc,
IntPtr lParam);

private delegate bool EnumResNameDelegate(
IntPtr hModule,
IntPtr lpszType,
IntPtr lpszName,
IntPtr lParam);

[DllImport("kernel32.dll")]
static extern IntPtr FindResource(
IntPtr hModule,
IntPtr lpName,
IntPtr lpType);

[DllImport("user32.dll")]
static extern IntPtr LoadIcon(
IntPtr hModule,
string Name
);

[DllImport("kernel32.dll", SetLastError=true)]
static extern IntPtr LoadResource(IntPtr hModule, IntPtr hResInfo);
------------------------------------------------------------------

Dec 23 '05 #1
1 4670
Mythran,

In your EnumResourceNameDelegate, you get the type and the name of the
resource in a string. From there, you can call the FindResource API method,
which will give you the handle that you can pass to LoadResource, which will
give you the appropriate handle which you can then use.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:ec**************@TK2MSFTNGP10.phx.gbl...
I am attempting to read all of the resources from a Win32 executable, and
load each of them to display them on a screen. So, please help figure out
why I can't seem to get it right :)

First off, I have my API declarations (will post that at bottom). I make
a call to LoadLibraryEx. Then, if the result is not zero, I make a call
to EnumResourceNames. EnumResourceNamesDelegate (delegate for
EnumResourceNames) is called for every found resource, and this part works
fine. The names and types are passed in, but the names don't match the
actual resource names. They are passed as IntPtr's and event if I change
the type in the API declaration and delegate and function to ... say ..
string, I still get the same value...the value for type is correct (for an
Icon, I get the value of 3...which maps to RT_ICON). But the name is
always an indexed value. Or, what I believe, is the index for the Icon in
the resource list. The first one is always 1, the 2nd is 2, et cetera.

From this point, I get stuck. How can I get the actual name that I need
to use to pass to LoadResource in which the result of that call gets
passed as the handle to System.Drawing.Icon.FromHandle (for Icons),
System.Drawing.Bitmap.FromHbitmap (for Bitmaps), et cetera?

When I call LoadResource, it returns a handle (non-zero). I pass this to
Icon.FromHandle and no exception is thrown. The Height and Width
properties of the Icon are both 0 and when I save the icon to disk, it is
0 bytes (the file is).

Any and all help is much appreciated...been stuck on this for awhile :)

Thanks,
Mythran

API Declarations that i have...

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 = 0x0000000a;
private const uint RT_MESSAGETABLE = 0x0000000b;

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", EntryPoint = "EnumResourceNamesW", CharSet =
CharSet.Unicode, SetLastError = true)]
static extern bool EnumResourceNamesWithName(
IntPtr hModule,
string lpszType,
EnumResNameDelegate lpEnumFunc,
IntPtr lParam);

[DllImport("kernel32.dll", EntryPoint = "EnumResourceNamesW", CharSet =
CharSet.Unicode, SetLastError = true)]
static extern bool EnumResourceNamesWithID(
IntPtr hModule,
uint lpszType,
EnumResNameDelegate lpEnumFunc,
IntPtr lParam);

private delegate bool EnumResNameDelegate(
IntPtr hModule,
IntPtr lpszType,
IntPtr lpszName,
IntPtr lParam);

[DllImport("kernel32.dll")]
static extern IntPtr FindResource(
IntPtr hModule,
IntPtr lpName,
IntPtr lpType);

[DllImport("user32.dll")]
static extern IntPtr LoadIcon(
IntPtr hModule,
string Name
);

[DllImport("kernel32.dll", SetLastError=true)]
static extern IntPtr LoadResource(IntPtr hModule, IntPtr hResInfo);
------------------------------------------------------------------

Dec 24 '05 #2

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

Similar topics

5
by: Greg | last post by:
Set Up Error - Failed to load resources from resource file Please check your set up. This plesently cordal and verbose error is greeting me in a very random and unpredictable way in a current VB...
3
by: Ramani | last post by:
Hi, We are running a ASP .NET application on Win2K server (.NET framework 1.1) I have recently started getting this error - Failed to load resources from resource file. I would like to...
0
by: Kevin P | last post by:
Hi News Group, In an application written in C# and VS.Net using Dotnet Framework 1.1.4322 on XP and Win 2000 machines, we randomly get the error "Failed to load resources from resource file....
0
by: Paul | last post by:
I have an existing application that has been installed and running for several months. I recently made a minor change to this application. When the application is installed on the user machine and...
3
by: Preference | last post by:
Hello, I am developing a .NET application in managed C++. Until now we have executed without problems in a lot of different computers. But a few days ago we detect a problem in some laptops. In...
0
by: Erald Kulk | last post by:
l.s. i'm using the setup wizard in vs.net for c# to create a setup wizard. since i've made some forms localizable i get some extra folders (en and en-US) in the release folder in my project....
0
by: Wal Turner | last post by:
Hi. The following error I seemingly get at random times when my application is loading. ************** Exception Text ************** System.NullReferenceException: Object reference not set to an...
0
by: bleedledeep | last post by:
I have 3 C# applications that run as a group. If I run each of these applications individually, they all start fine. If I have a batch file that starts each of them quickly, on *most* machines...
0
by: Markus Wildgruber | last post by:
Hi! On one of our computers we get the following error message over and over again at various locations when running our WinForms app: Failed to load resources from resource file. Please check...
4
by: bclegg | last post by:
Hi, I am getting the above error when start my application which I deployed successfully. The msi was produced using a .net launch project. I can't see how to dig deeper to find out what is...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.