Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用间接字符串加载资源字符串?_C#_.net_Winapi_Resourcebundle - Fatal编程技术网

C# 如何使用间接字符串加载资源字符串?

C# 如何使用间接字符串加载资源字符串?,c#,.net,winapi,resourcebundle,C#,.net,Winapi,Resourcebundle,我正在尝试转换由HNetCfg.FwPolicy2返回的防火墙规则的Grouping属性中的间接字符串。在PowerShell中: $fw = New-Object -ComObject 'HNetCfg.FwPolicy2' $fw.Rules | Select-Object -ExpandProperty 'Grouping' | Select-Object -Unique 返回以下内容: 如何将这些间接字符串转换为托管代码中的实际组名? 我试图使用加载模块/程序集,然后使用加载字符串,但

我正在尝试转换由
HNetCfg.FwPolicy2
返回的防火墙规则的
Grouping
属性中的间接字符串。在PowerShell中:

$fw = New-Object -ComObject 'HNetCfg.FwPolicy2'
$fw.Rules | Select-Object -ExpandProperty 'Grouping' | Select-Object -Unique
返回以下内容:

如何将这些间接字符串转换为托管代码中的实际组名?

我试图使用加载模块/程序集,然后使用加载字符串,但
LoadString
不加载资源。这是我的密码:

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr GetModuleHandle(string lpModuleName);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int LoadString(IntPtr hInstance, uint uID, StringBuilder lpBuffer, int nBufferMax);

[DllImport("kernel32.dll")]
private static extern uint GetLastError();

public static string GetGroupingFromResource(string resourceInfo)
{
    if (String.IsNullOrEmpty(resourceInfo))
    {
        return resourceInfo;
    }

    var matches = Regex.Match(resourceInfo, @"^@([^,]+),-(\d+)$");
    if (! matches.Success)
    {
        return resourceInfo;
    }

    var modulePath = matches.Groups[1].Value;
    UInt32 resourceID;
    if (!UInt32.TryParse(matches.Groups[2].Value, out resourceID))
    {
        return resourceInfo;
    }

    resourceID += 10000;

    Console.Out.WriteLine(string.Format("Assembly Path: {0}", modulePath));
    Console.Out.WriteLine(string.Format("Resource ID:   {0}", resourceID));

    modulePath = Environment.ExpandEnvironmentVariables(modulePath);

    var searchPaths = Environment.GetEnvironmentVariable("PATH").Split(';');
    if (! System.IO.Path.IsPathRooted(modulePath))
    {
        foreach (var searchPath in searchPaths)
        {
            var fullModulePath = System.IO.Path.Combine(searchPath, modulePath);
            if (System.IO.File.Exists(fullModulePath))
            {
                modulePath = fullModulePath;
                break;
            }
        }
    }

    Console.Out.WriteLine(string.Format("Module Path: {0}", modulePath));
    var moduleHandle = GetModuleHandle(modulePath);
    var lastError = GetLastError();
    Console.Out.WriteLine("Last Error: {0}", lastError);
    if (lastError != 0x0)
    {
        return null;
    }

    var grouping = new StringBuilder();
    LoadString(moduleHandle, resourceID, grouping, 255);
    lastError = GetLastError();
    Console.Out.WriteLine("Last Error: {0}", lastError);
    if (lastError != Win32ErrorCodes.Ok)
    {
        return null;
    }

    Console.Out.WriteLine(grouping.ToString());
    return grouping.ToString();

}

您需要使用C++代码,如代码< > WChar SZ(1024);如果(S_OK==SHLoadIndirectString(L“@FirewallAPI.dll,-32752”,sz,RTL_NUMBER_OF(sz),0)){..}@RbMm如果您将其作为答案添加,我将接受它。不是C++代码,想。你需要使用C++代码,比如代码< > WChar SZ(1024);如果(S_OK==SHLoadIndirectString(L“@FirewallAPI.dll,-32752”,sz,RTL_NUMBER_OF(sz),0)){..}@RbMm如果您将其作为答案添加,我将接受它。不过,不是C++代码。
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr GetModuleHandle(string lpModuleName);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int LoadString(IntPtr hInstance, uint uID, StringBuilder lpBuffer, int nBufferMax);

[DllImport("kernel32.dll")]
private static extern uint GetLastError();

public static string GetGroupingFromResource(string resourceInfo)
{
    if (String.IsNullOrEmpty(resourceInfo))
    {
        return resourceInfo;
    }

    var matches = Regex.Match(resourceInfo, @"^@([^,]+),-(\d+)$");
    if (! matches.Success)
    {
        return resourceInfo;
    }

    var modulePath = matches.Groups[1].Value;
    UInt32 resourceID;
    if (!UInt32.TryParse(matches.Groups[2].Value, out resourceID))
    {
        return resourceInfo;
    }

    resourceID += 10000;

    Console.Out.WriteLine(string.Format("Assembly Path: {0}", modulePath));
    Console.Out.WriteLine(string.Format("Resource ID:   {0}", resourceID));

    modulePath = Environment.ExpandEnvironmentVariables(modulePath);

    var searchPaths = Environment.GetEnvironmentVariable("PATH").Split(';');
    if (! System.IO.Path.IsPathRooted(modulePath))
    {
        foreach (var searchPath in searchPaths)
        {
            var fullModulePath = System.IO.Path.Combine(searchPath, modulePath);
            if (System.IO.File.Exists(fullModulePath))
            {
                modulePath = fullModulePath;
                break;
            }
        }
    }

    Console.Out.WriteLine(string.Format("Module Path: {0}", modulePath));
    var moduleHandle = GetModuleHandle(modulePath);
    var lastError = GetLastError();
    Console.Out.WriteLine("Last Error: {0}", lastError);
    if (lastError != 0x0)
    {
        return null;
    }

    var grouping = new StringBuilder();
    LoadString(moduleHandle, resourceID, grouping, 255);
    lastError = GetLastError();
    Console.Out.WriteLine("Last Error: {0}", lastError);
    if (lastError != Win32ErrorCodes.Ok)
    {
        return null;
    }

    Console.Out.WriteLine(grouping.ToString());
    return grouping.ToString();

}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace ConsoleApp
{
    class Program
    {
        [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
        public static extern int SHLoadIndirectString(string pszSource, StringBuilder pszOutBuf, int cchOutBuf, IntPtr ppvReserved);

        static void Main(string[] args)
        {
            StringBuilder buffer = new StringBuilder(1024);

            string resourcePath = "@FirewallAPI.dll,-32752";

            int result = SHLoadIndirectString(resourcePath, buffer, buffer.Capacity, IntPtr.Zero);

            if (result == 0) Console.WriteLine(buffer.ToString()); // return "Network Discovery" on Windows 10 (1607)

            Console.ReadKey();
        }
    }
}