gpt4 book ai didi

c# - 如何从注册表中获取重定向的字符串?

转载 作者:行者123 更新时间:2023-11-30 14:56:39 25 4
gpt4 key购买 nike

我正在使用 Registry 从注册表中读取一些值.我需要访问的一些值使用 Registry String Redirection .

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

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

如何访问这个本地化字符串?

最佳答案

.NET 库中似乎没有内置方法来执行此操作,但是 as this C++ answer shows ,您可以调用RegLoadMUIStringWindows APIadvapi32.dll 中.

您可以在下面找到 RegistryKey 的扩展方法它可以通过遵循字符串重定向来加载值。

/// <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 导入是:

// 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 及更高版本!

关于c# - 如何从注册表中获取重定向的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22273956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com