Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text from dll using index

Tags:

string

c#

dll

How can I get strings from windows dlls like mssvp.dll and themeui.dll using the index? In the registry or theme files there are some strings (like DisplayName in themes) that are pointing to a dll and an index number instead of the real texts. For example I have: DisplayName=@%SystemRoot%\System32\themeui.dll,-2106 in windows theme file. So how can I retrieve the real strings from those dlls using C# and .Net 4.0?

like image 739
SepehrM Avatar asked Aug 20 '13 19:08

SepehrM


1 Answers

You need to use P/Invoke:

    /// <summary>Returns a string resource from a DLL.</summary>
    /// <param name="DLLHandle">The handle of the DLL (from LoadLibrary()).</param>
    /// <param name="ResID">The resource ID.</param>
    /// <returns>The name from the DLL.</returns>
    static string GetStringResource(IntPtr handle, uint resourceId) {
        StringBuilder buffer = new StringBuilder(8192);     //Buffer for output from LoadString()

        int length = NativeMethods.LoadString(handle, resourceId, buffer, buffer.Capacity);

        return buffer.ToString(0, length);      //Return the part of the buffer that was used.
    }


    static class NativeMethods {
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
        internal static extern IntPtr LoadLibrary(string lpLibFileName);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
        internal static extern int LoadString(IntPtr hInstance, uint wID, StringBuilder lpBuffer, int nBufferMax);

        [DllImport("kernel32.dll")]
        public static extern int FreeLibrary(IntPtr hLibModule);
    }
like image 116
SLaks Avatar answered Nov 03 '22 17:11

SLaks