我试图在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);
}
}
答案 0 :(得分: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