FindResource虽然存在,但找不到资源

时间:2013-02-21 16:54:55

标签: c# winapi resources pinvoke

我的fingers are hurtin'。我正在尝试使用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的帖子,我读了很多这些帖子但没有修复这个问题。

1 个答案:

答案 0 :(得分:3)

FindResource()返回句柄,而不是指向资源的指针;获取数据需要几个步骤。您需要LoadResource()然后LockResource()来获取指向实际资源字节的指针。

据我所知,这是旧的16位日的延续。