SepehrM
SepehrM

Reputation: 1097

Get text from dll using index

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?

Upvotes: 6

Views: 2233

Answers (1)

SLaks
SLaks

Reputation: 888185

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

Upvotes: 8

Related Questions