NetworkIsolationEnumAppContainers的C#包装器

时间:2013-04-16 18:14:03

标签: c# pinvoke

如何创建一个c#包装器来调用:

  [DllImport("FirewallAPI.dll")] 
        internal static extern uint NetworkIsolationEnumAppContainers(out uint pdwCntPublicACs, out IntPtr ppACs); 

我在运行时遇到AccessViolationException(尝试读取或写入受保护的内存):

        uint aux=0;
        IntPtr foo=new IntPtr();
        NetworkIsolationEnumAppContainers(out aux, out foo);

马歇尔的正确方法是什么? 感谢

2 个答案:

答案 0 :(得分:1)

您的p / invokes似乎已从此处解除:http://blogs.msdn.com/b/fiddler/archive/2011/12/10/fiddler-windows-8-apps-enable-loopback-network-isolation-exemption.aspx

那篇文章说:

  

他们的.NET声明(从BUILD会议开始)如下:

// Call this API to enumerate all of the AppContainers on the system 
[DllImport("FirewallAPI.dll")] 
internal static extern uint NetworkIsolationEnumAppContainers(
    out uint pdwCntPublicACs, out IntPtr ppACs); 
....

但是,API在发布之前已更改。 API的发布版本有一个额外的参数,位于不同的DLL中。该文档可在MSDN上找到。

您需要修改您的p / invoke才能使用此API。

答案 1 :(得分:0)

感谢您指向最新API的正确指针。这是现在正在运行的代码:

        uint pdwCntPublicACs = 0;
        IntPtr ppACs = new IntPtr();

        // Pin down variables
        GCHandle handle_pdwCntPublicACs = GCHandle.Alloc(pdwCntPublicACs, GCHandleType.Pinned);
        GCHandle handle_ppACs = GCHandle.Alloc(ppACs, GCHandleType.Pinned);


        uint retval = NetworkIsolationEnumAppContainers((Int32) NETISO_FLAG.NETISO_FLAG_MAX, out pdwCntPublicACs, out ppACs);

        //release pinned variables.
        handle_pdwCntPublicACs.Free();
        handle_ppACs.Free();