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?
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With