DWORD GetIpNetTable(
PMIB_IPNETTABLE pIpNetTable, // buffer for mapping table
PULONG pdwSize, // size of buffer
BOOL bOrder // sort by IP address
);
PMIB_IPNETTABLE pIpNetTable, // buffer for mapping table
PULONG pdwSize, // size of buffer
BOOL bOrder // sort by IP address
);
out MIB_IPNETTABLE pIpNetTable, // buffer for mapping table
out uint pdwSize, // size of buffer
bool bOrder // sort by IP address
); 另外你要自己补全MIB_IPNETTABLE的声明.
MIB_IPNETTABLE 在C#中没有这个类型啊?这个函数在System32下面的IPHLPAPI.dll中。我应该怎么找这个MIB_IPNETTABLE的声明啊?
typedef struct _MIB_IPNETTABLE {
DWORD dwNumEntries;
MIB_IPNETROW table[ANY_SIZE];
} MIB_IPNETTABLE, *PMIB_IPNETTABLE;这是在MSDN中找到的结构定义。那个方法导入的时候前面要加“DllImport”:
[DllImport ("IPHLPAPI.dll")]
[DllImport("IPHLPAPI.dll")]
extern uint GetIpNetTable(out MIB_IPNETTABLE pIpNetTable, out uint pdwSize,bool bOrder);
我只声明了这些,还差一些数据类型的定义,这些怎么办?
static extern int GetIpNetTable(IntPtr pIpNetTable, ref int pdwSize, bool bOrder);
namespace test
{
class test
{
[DllImport("IPHLPAPI.dll")]
extern uint GetIpNetTable(out MIB_IPNETTABLE pIpNetTable, out uint pdwSize,bool bOrder);
}
}
大家能不能帮我补全,我刚接触C#调用API,一点也不清楚。
变长体处理起来会比较麻烦.改用指针会简单很多.
这里给个用指针遍历表的例子:using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;struct MIB_IPNETROW
{
public uint dwIndex;
public uint dwPhysAddrLen;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] bPhysAddr;//[];
public uint dwAddr;
public uint dwType;
}
struct MIB_IPNETTABLE
{
public int dwNumEntries;
public MIB_IPNETROW table;
}unsafe class program
{
[DllImport("IPHLPAPI.dll")]
static extern uint GetIpNetTable(void* pIpNetTable, out int pdwSize, bool bOrder);
static void Main()
{
int bufSize; GetIpNetTable(null, out bufSize, false);
byte* pbuf = stackalloc byte[bufSize];
GetIpNetTable(pbuf, out bufSize, false);
//获取行数
int numEntries = *(int*)pbuf;
int size = Marshal.SizeOf(typeof(MIB_IPNETROW));
//移动指针至第一行
pbuf = (byte*)(((int*)pbuf) + 1); //遍历表输出行
for (int i = 0; i < numEntries; ++i,pbuf += size)
{
MIB_IPNETROW row = (MIB_IPNETROW)Marshal.PtrToStructure((IntPtr)pbuf, typeof(MIB_IPNETROW));
IPAddress ip = new IPAddress(row.dwAddr);
Console.Write("{0}=>", ip);
if (row.dwPhysAddrLen == 0)
{
Console.Write("N/A");
}
else
{
int j;
for (j = 0; j < row.dwPhysAddrLen - 1; ++j)
{
Console.Write("{0:X2}-", row.bPhysAddr[j]);
}
Console.Write("{0:X2}", row.bPhysAddr[j]);
}
Console.WriteLine();
}
Console.WriteLine("{0} rows displayed", numEntries);
}
}