Reputation: 19130
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?
Upvotes: 4
Views: 728
Reputation: 19130
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.
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;
}
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
}
Remember that this will only work for Vista and higher!
Upvotes: 6