Reading (Icon) Resource from a Binary file,...

K

Kerem Gümrükcü

Hi,

this is a mixed question, half .NET and half Win32 so i will
post this to several groups (Crossposted). Well, it is no big
deal to use the pinvkoes of resource functions from the
windows api and they work just fine. I wrote a wrapper
function arround them with several parameters: a path
to a file, a resource identifier, a resource type and a out
parameter for a exception object. It looks like this:


public enum BinaryResourceTypes
{
RT_CURSOR = 1,
RT_BITMAP = 2,
RT_ICON = 3,
RT_MENU = 4,
RT_DIALOG = 5,
RT_STRING = 6,
RT_FONTDIR = 7,
RT_FONT = 8,
RT_ACCELERATOR = 9,
RT_RCDATA = 10,
RT_MESSAGETABLE = 11,
RT_GROUP_CURSOR = 12,
RT_GROUP_ICON = 14,
RT_VERSION = 16,
RT_DLGINCLUDE = 17,
RT_PLUGPLAY = 19,
RT_VXD = 20,
RT_ANICURSOR = 21,
RT_ANIICON = 22,
RT_HTML = 23,
RT_MANIFEST = 24
}


public static byte[] GetResourceFromBinary(string BinaryPath, string
ResourceName, object ResourceType, out Exception Error)
{
IntPtr hModule = IntPtr.Zero;
IntPtr hResource = IntPtr.Zero;

try
{
hModule = LoadLibrary(BinaryPath);

if (hModule == IntPtr.Zero)
{
Error = new Win32Exception();
return null;
}
else
{
Type t = ResourceType.GetType();

if (t == typeof(BinaryResourceTypes))
{
hResource = FindResource(hModule, ResourceName,
(int)ResourceType);
}
else if(t == typeof(string))
{
hResource = FindResource(hModule, ResourceName,
(string)ResourceType);
}

if (hResource == IntPtr.Zero)
{
throw new Win32Exception();
}
else
{
IntPtr hRes = LoadResource(hModule, hResource);

if (hRes == IntPtr.Zero)
{
throw new Win32Exception();
}
else
{
IntPtr ptrResMem = LockResource(hRes);

if (ptrResMem == IntPtr.Zero)
{
throw new Win32Exception();
}
else
{
uint ResSize = SizeofResource(hModule,
hResource);
byte[] ResourceBuffer = new byte[ResSize];

Marshal.Copy(ptrResMem, ResourceBuffer, 0,
(int)ResSize);

if (hModule != null)
{
FreeLibrary(hModule);
}

Error = null;
return ResourceBuffer;
}
}
}
}
}
catch (Exception err)
{
if (hModule != IntPtr.Zero)
{
FreeLibrary(hModule);
}

Error = err;
return null;
}
}

The returned data is byte-array with the binary representation of the
requested resource.
The problem here is, that i get garbage for icons, i mean if i look with a
hex editor inside a
icon that extracted with my code and then with a resource editor that did
that job, there is
a big difference between them,...whats my fault here? I want to extract a
Icon with a identifier
from a binary file,...is there something wrong with my code? I dont think
so, since it works
with other ressource data like Strings, Message tables, avis, user defined
stuff, etc,...but only
messes with icons! I think, there is something i have to take care, but
what?

Some Help would be great here,..

Thanks in advance,...

Regards

K.


--
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
s-----------------------
"This reply is provided as is, without warranty express or implied."
 
K

Kerem Gümrükcü

Here is a exceprt of what i get when running
the code:

http://www.pro-it-education.de/staff/keremg/misc/7_by_code.ico

And this one is returned from a resource editor:

http://www.pro-it-education.de/staff/keremg/misc/7_by_res_edit.ico

For whatever reason it reapeats a segment
insinde the binary data. You can see that with
a Hex Editor...


regards

K.

--
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."

Kerem Gümrükcü said:
Hi,

this is a mixed question, half .NET and half Win32 so i will
post this to several groups (Crossposted). Well, it is no big
deal to use the pinvkoes of resource functions from the
windows api and they work just fine. I wrote a wrapper
function arround them with several parameters: a path
to a file, a resource identifier, a resource type and a out
parameter for a exception object. It looks like this:


public enum BinaryResourceTypes
{
RT_CURSOR = 1,
RT_BITMAP = 2,
RT_ICON = 3,
RT_MENU = 4,
RT_DIALOG = 5,
RT_STRING = 6,
RT_FONTDIR = 7,
RT_FONT = 8,
RT_ACCELERATOR = 9,
RT_RCDATA = 10,
RT_MESSAGETABLE = 11,
RT_GROUP_CURSOR = 12,
RT_GROUP_ICON = 14,
RT_VERSION = 16,
RT_DLGINCLUDE = 17,
RT_PLUGPLAY = 19,
RT_VXD = 20,
RT_ANICURSOR = 21,
RT_ANIICON = 22,
RT_HTML = 23,
RT_MANIFEST = 24
}


public static byte[] GetResourceFromBinary(string BinaryPath,
string ResourceName, object ResourceType, out Exception Error)
{
IntPtr hModule = IntPtr.Zero;
IntPtr hResource = IntPtr.Zero;

try
{
hModule = LoadLibrary(BinaryPath);

if (hModule == IntPtr.Zero)
{
Error = new Win32Exception();
return null;
}
else
{
Type t = ResourceType.GetType();

if (t == typeof(BinaryResourceTypes))
{
hResource = FindResource(hModule, ResourceName,
(int)ResourceType);
}
else if(t == typeof(string))
{
hResource = FindResource(hModule, ResourceName,
(string)ResourceType);
}

if (hResource == IntPtr.Zero)
{
throw new Win32Exception();
}
else
{
IntPtr hRes = LoadResource(hModule, hResource);

if (hRes == IntPtr.Zero)
{
throw new Win32Exception();
}
else
{
IntPtr ptrResMem = LockResource(hRes);

if (ptrResMem == IntPtr.Zero)
{
throw new Win32Exception();
}
else
{
uint ResSize = SizeofResource(hModule,
hResource);
byte[] ResourceBuffer = new byte[ResSize];

Marshal.Copy(ptrResMem, ResourceBuffer, 0,
(int)ResSize);

if (hModule != null)
{
FreeLibrary(hModule);
}

Error = null;
return ResourceBuffer;
}
}
}
}
}
catch (Exception err)
{
if (hModule != IntPtr.Zero)
{
FreeLibrary(hModule);
}

Error = err;
return null;
}
}

The returned data is byte-array with the binary representation of the
requested resource.
The problem here is, that i get garbage for icons, i mean if i look with a
hex editor inside a
icon that extracted with my code and then with a resource editor that did
that job, there is
a big difference between them,...whats my fault here? I want to extract a
Icon with a identifier
from a binary file,...is there something wrong with my code? I dont think
so, since it works
with other ressource data like Strings, Message tables, avis, user defined
stuff, etc,...but only
messes with icons! I think, there is something i have to take care, but
what?

Some Help would be great here,..

Thanks in advance,...

Regards

K.


--
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
s-----------------------
"This reply is provided as is, without warranty express or implied."
 
M

mayayana

Neither of your files seems to be an icon. The first
appears to be a .bmp file.

I've never used FindResource. Maybe someone will
offer help with that. On the other hand, if you actually
want to parse the resource data this might provide a
little help in terms of figuring out the layout:

www.jsware.net/jsware/scripts.php5#iconextr

It's a VBScript that extracts icons from PE files by
directly parsing the file to find the resource table and
then walking the resource table structure to locate and
reconstitute any stored icons.

The icon headers are stored separately from the
icon and mask bitmaps. And the header needs a little
work in order to reassemble the .ico file. The ICONDIRENTRY
structure for the file is stored as a slightly different
ICOHEADER structure. So you have to get the ICOHEADER
for each icon in the file, convert it to an appropriate
ICONDIRENTRY, match it up with its bitmap pair, and
then prepend an ICONDIR structure to all of that.
 
M

mayayana

i found out, that this is not "that easy" to get a simple icon
from a binary file:

It's not terribly hard to do it directly, without any
API (as the linked VBScript demonstrates), but it is
somewhat tedious and complex because the resource
table structure is complex.

Unfortunately I can't see what you're referring to.
I don't use .Net to begin with, but also codeproject.com
is one of those sites that won't allow downloads unless
one signs up to be a member.
 
K

Kerem Gümrükcü

Contact me if you like, i can send you the
sample code and binary if you like:

(e-mail address removed)


Regards

Kerem
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top