Reputation: 14085
My fingers are hurtin'. I'm trying to load resources from my exe using WinApi (FindResource). I've successfully created a string resource using WinApi and verified that they DO exist with PE explorer, Resource Hacker and Hex Editor.
What I can't do is to find them via FindResource. The function finishes, returns some pointer and sets LastError to 0. But the pointer b points to invalid memory. I've tried to import FindResource in 4 different ways and all have the same results (b1, b2, b3, b4 mean b for each declaration). I discovered that I need to LoadLibrary first to make FindResource find "something" (a1 = a2 are 2 identical module handles that point exactly to the beginning of the exe in memory after loading, just acquired with different functions).
MAKELANGID(0, 0)
~ I understand that 0 and 0 stand for neutral, I read somewhere that there might be something about *.mui
files ?How can I successfully find the resources ? Do I have bugs in my code ?
public static class MyClass
{
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string fileName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern bool FreeLibrary(IntPtr module);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern IntPtr GetModuleHandle([MarshalAs(UnmanagedType.LPStr)] string filename);
[DllImport("kernel32.dll")]
public static extern IntPtr BeginUpdateResource([MarshalAs(UnmanagedType.LPStr)] string filename, bool deleteExistingResources);
[DllImport("kernel32.dll")]
public static extern bool UpdateResource(IntPtr resource, [MarshalAs(UnmanagedType.LPStr)] string type, [MarshalAs(UnmanagedType.LPStr)] string name, ushort language, IntPtr data, uint dataSize);
[DllImport("kernel32.dll")]
public static extern bool EndUpdateResource(IntPtr resource, bool discard);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, EntryPoint = "FindResource", SetLastError = true)]
public static extern IntPtr FindResource1(IntPtr module, [MarshalAs(UnmanagedType.LPStr)]string name, [MarshalAs(UnmanagedType.LPStr)] string type);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, EntryPoint = "FindResource", SetLastError = true)]
public static extern IntPtr FindResource2(IntPtr module, IntPtr name, IntPtr type);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, EntryPoint = "FindResource", SetLastError = true)]
public static extern IntPtr FindResource3(IntPtr module, [In, MarshalAs(UnmanagedType.LPStr)]string name, [In, MarshalAs(UnmanagedType.LPStr)] string type);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, EntryPoint = "FindResource", SetLastError = true)]
public static extern IntPtr FindResource4(IntPtr module, string lpName, string lpType);
public static ushort MAKELANGID(ushort primaryLanguage, ushort subLanguage)
{
return Convert.ToUInt16((subLanguage << 10) | primaryLanguage);
}
private static void test()
{
string filename1 = "MyApp.exe";
string filename2 = "MyApp2.exe";
//if (!File.Exists(filename2))
{
// creating resource: name = TEST, type = SYSO, lang-id = neutral
File.Copy(filename1, filename2, true);
IntPtr res = BeginUpdateResource(filename2, false);
string s = "aaahello world";
UpdateResource(res, "SYSO", "TEST", MAKELANGID(0, 0), Marshal.StringToHGlobalAnsi(s), (uint) (s.Length));
EndUpdateResource(res, false);
}
// find resource
IntPtr a1 = LoadLibrary(filename2);
IntPtr a2 = GetModuleHandle(filename2);
IntPtr hInstance = Marshal.GetHINSTANCE(typeof(MyClass).Module);
if (a1 == a2)
Console.WriteLine("a1 = a2 = " + a2 + ", hInstance: " + hInstance);
else
Console.WriteLine(a1 + " != " + a2);
for (int i = 0; i < 10; i++)
Console.WriteLine("." + Marshal.ReadByte(a2 + i));
IntPtr b1 = FindResource1(a2, "TEST", "SYSO");
IntPtr b2 = FindResource2(a2, Marshal.StringToHGlobalAnsi("TEST"), Marshal.StringToHGlobalAnsi("SYSO"));
IntPtr b3 = FindResource3(a2, "TEST", "SYSO");
IntPtr b4 = FindResource4(a2, "TEST", "SYSO");
Console.WriteLine(" -> res: " + b1 + ", " + (b1.ToInt32() - a2.ToInt32()) + ", " + Marshal.PtrToStringAnsi(b1));
Console.WriteLine(" -> res: " + b2 + ", " + (b2.ToInt32() - a2.ToInt32()) + ", " + Marshal.PtrToStringAnsi(b2));
Console.WriteLine(" -> res: " + b3 + ", " + (b3.ToInt32() - a2.ToInt32()) + ", " + Marshal.PtrToStringAnsi(b3));
Console.WriteLine(" -> res: " + b4 + ", " + (b4.ToInt32() - a2.ToInt32()) + ", " + Marshal.PtrToStringAnsi(b4));
for (int i = 0; i < 10; i++)
Console.WriteLine("." + Marshal.ReadByte(b1 + i));
}
[STAThread]
public static void Main()
{
test();
Console.ReadKey();
}
}
Upvotes: 1
Views: 1205
Reputation: 2313
FindResource()
returns a handle, not a pointer to the resource; it takes a couple more steps to get to the data. You need to LoadResource()
and then LockResource()
to get a pointer to actual resource bytes.
As far as I can tell this is a holdover from the old 16-bit days.
Upvotes: 3