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."
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."