6

如何使用索引从 mssvp.dll 和 themeui.dll 等 Windows dll 中获取字符串?在注册表或主题文件中,有一些字符串(如主题中的 DisplayName)指向 dll 和索引号,而不是实际文本。例如,我在 Windows 主题文件中有:DisplayName=@%SystemRoot%\System32\themeui.dll,-2106。那么如何使用 C# 和 .Net 4.0 从这些 dll 中检索真正的字符串?

4

1 回答 1

8

您需要使用 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);
    }
于 2013-08-20T23:51:14.530 回答