我的手指很痛。我正在尝试使用 WinApi (FindResource) 从我的 exe 加载资源。我已经使用 WinApi 成功创建了一个字符串资源,并通过 PE 资源管理器、资源黑客和十六进制编辑器验证了它们确实存在。
我不能做的是通过 FindResource 找到它们。该函数完成,返回一些指针并将 LastError 设置为 0。但指针b指向无效内存。我尝试以 4 种不同的方式导入 FindResource,并且都具有相同的结果(b1、b2、b3、b4 表示每个声明的 b)。我发现我需要先 LoadLibrary 才能让 FindResource 找到“某物”(a1 = a2是 2 个相同的模块句柄,它们在加载后正好指向内存中 exe 的开头,只是用不同的函数获得的)。
我认为a (a = a1 = a2) 和b必须位于同一内存空间中(偏移到模块开头?)。但是 b 到 a 的差异始终相同 16568。在十六进制编辑器中,我看到我的字符串在偏移量 8000 左右,并且在每个资源创建后都会有所不同。我有一些想法可能是错的,但我不确定:
MAKELANGID(0, 0)
~ 我知道 0 和 0 代表中立,我在某处读到可能有关于*.mui
文件的内容?- 资源创造制造了破碎的资源?实际上,两个资源编辑器都没有问题来显示我的资源
- 导入声明可能是错误的,但我尝试了很多可能性,这些是最好的
- 资源需要打开/锁定/什么?
我怎样才能成功找到资源?我的代码中有错误吗?
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();
}
}
我知道互联网上有很多很多关于 FindResource 的帖子,我阅读了很多,但没有一个解决了这个问题。