Bitterblue
Bitterblue

Reputation: 14085

FindResource Doesn't Find Resources Although they DO Exist

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


I thought that a (a = a1 = a2) and b must lie in the same memory space (offset to module beginning?). But the difference of b to a is always the same 16568. In Hex Editor I see that my string is somewhere around offset 8000 and varies after each resource create. I have ideas what could be wrong but I'm not sure:

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();
    }
}


I know there are many, many, many posts about FindResource on the internet and I read many of them but none had this problem fixed.

Upvotes: 1

Views: 1205

Answers (1)

HerrJoebob
HerrJoebob

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

Related Questions