在.net中调用mprapi.dll函数

本文关键字:dll 函数 mprapi 调用 net | 更新日期: 2023-09-27 18:01:25

我正在尝试在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);
        }
    }

在.net中调用mprapi.dll函数

这是通过使用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