如何从注册表重定向字符串? [英] How to get redirected string from the registry?

查看:336
本文介绍了如何从注册表重定向字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的注册表。一些我需要访问使用的注册表字符串重定向

I'm reading some values from the registry using Registry. Some of the values I need to access use Registry String Redirection.

这种含有值的一个例子是:

One example of such a containing value is:

@%SystemRoot%\system32\shell32.dll,-21791

我如何可以访问此本地化字符串?

How can I access this localized string?

推荐答案

有似乎是在.net库中没有内置的方法做到这一点,但是因为这C ++的回答显示,你可以调用的 RegLoadMUIString ADVAPI32.DLL 的Windows API 的。

There seems to be no built-in method in the .NET library to do this, however as this C++ answer shows, you can call RegLoadMUIString in advapi32.dll of the Windows API.

下面你可以找到的的RegistryKey 可以通过以下字符串重定向装载值。

Below you can find an extension method for RegistryKey which can load a value by following the string redirection.

/// <summary>
///   Retrieves the multilingual string associated with the specified name. Returns null if the name/value pair does not exist in the registry.
///   The key must have been opened using 
/// </summary>
/// <param name = "key">The registry key to load the string from.</param>
/// <param name = "name">The name of the string to load.</param>
/// <returns>The language-specific string, or null if the name/value pair does not exist in the registry.</returns>
public static string LoadMuiStringValue( this RegistryKey key, string name )
{
    const int initialBufferSize = 1024;
    var output = new StringBuilder( initialBufferSize );
    int requiredSize;
    IntPtr keyHandle = key.Handle.DangerousGetHandle();
    ErrorCode result = (ErrorCode)AdvApi32.RegLoadMUIString( keyHandle, name, output, output.Capacity, out requiredSize, AdvApi32.RegistryLoadMuiStringOptions.None, null );

    if ( result == ErrorCode.MoreData )
    {
        output.EnsureCapacity( requiredSize );
        result = (ErrorCode)AdvApi32.RegLoadMUIString( keyHandle, name, output, output.Capacity, out requiredSize, AdvApi32.RegistryLoadMuiStringOptions.None, null );
    }

    return result == ErrorCode.Success ? output.ToString() : null;
}



所需的COM进口是:

The required COM imports are:

// Snippet of ErrorCode.
enum ErrorCode
{
    Success = 0x0000,
    MoreData = 0x00EA
}

[DllImport( Dll, CharSet = CharSet.Unicode )]
public extern static int RegLoadMUIString(
    IntPtr registryKeyHandle, string value,
    StringBuilder outputBuffer, int outputBufferSize, out int requiredSize,
    RegistryLoadMuiStringOptions options, string path );

/// <summary>
///   Determines the behavior of <see cref="RegLoadMUIString" />.
/// </summary>
[Flags]
internal enum RegistryLoadMuiStringOptions : uint
{
    None = 0,
    /// <summary>
    ///   The string is truncated to fit the available size of the output buffer. If this flag is specified, copiedDataSize must be NULL.
    /// </summary>
    Truncate = 1
}



请记住,这将仅适用于Vista的工作,更高!

Remember that this will only work for Vista and higher!

这篇关于如何从注册表重定向字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆