0

我正在尝试在 c# .net 中调用本机 api。谁能帮我将下面的代码翻译成 C# 调用?我会很感激的。

dwResult = ::MprAdminMIBServerConnect( pwcComputerName.GetText(), &hMibServer );

dwResult = ::MprAdminServerGetInfo( hMibServer, 0, (LPBYTE*)&pServerBuf );

// I want to read the below variables as string
pInObjectEntry->Put(L"rastotalportstoconnectto", pServerBuf->dwTotalPorts );
pInObjectEntry->Put(L"rasportsinuse", pServerBuf->dwPortsInUse );

这是示例代码,谁能告诉我如何读取 dwTotalPorts 和 dwPortsInUse 的值?

  class RASCollector
    {
        [DllImport("mprapi.dll", SetLastError = false)]
        public static extern UInt32 MprAdminMIBServerConnect([MarshalAs(UnmanagedType.LPWStr)] string lpwsServerName, out IntPtr phMibServer);

        [DllImport("mprapi.dll", SetLastError = false)]
        public static extern UInt32 MprAdminServerGetInfo(IntPtr phMprServer, UInt32 dwLevel, out byte[] pServerBuf);

        public void Run()
        {
            IntPtr hMibServer = new IntPtr();
            UInt32 result;

            result = MprAdminMIBServerConnect("localhost", out hMibServer);

            byte[] pServerBuf;

            result = MprAdminServerGetInfo(hMibServer, 0, out pServerBuf);
        }
    }
4

1 回答 1

0

它是通过使用 InterOP 完成的。您需要像这样导入每个函数:

using System.Runtime.InteropServices;

[DllImport("mprapi.dll", SetLastError = false)]
public static extern UInt32 MprAdminMIBServerConnect([MarshalAs(UnmanagedType.LPWStr)] string lpwsServerName, out IntPtr phMibServer);

MSDN 库中定义的数据类型应转换为对应的 C# 数据类型。您应该查看这篇文章以获取更多信息:http: //msdn.microsoft.com/en-us/library/ac7ay120.aspx

有关在 C# 中编组复杂数据结构和指针的更多信息:http: //blogs.msdn.com/b/dsvc/archive/2009/02/18/marshalling-complicated-structures-using-pinvoke.aspx

于 2013-02-26T14:11:36.770 回答