Using [DllImport ("iphlpapi.dll")] on Windows 7 64 bit

[DllImport("iphlpapi.dll")] private static extern int GetTcpTable(IntPtr pTcpTable, ref int pdwSize, bool bOrder); 

I use this DLL and call the function as GetTcpTable(IntPtr.Zero, ref iBytes, false)

it works fine in windows 7 32-bit os but doesn't work in 64-bit os.How can i get it to work in windows 7 64 bit os?

+4
source share
1 answer

It looks like the problem is with your signature:

 private static extern int GetTcpTable(IntPtr pTcpTable, ref int pdwSize, bool bOrder); 

Looking at the documentation , the following is displayed instead:

 public static extern int GetTcpTable(byte[] pTcpTable, out int pdwSize, bool bOrder); 

You are using IntPtr, but it must be a buffer that will be filled with the MIB_TCPTABLE structure.

I tried to find articles, citing the correct way to implement all this, but did not find much. I am shocked that pinvoke.net does not even list GetTcpTable and the structure I mentioned above.

EDIT: You can take a look at GetExtendedTcpTable()

I also found that there is GetTcpTable2 () that was introduced in Vista. It contains the same signature, so maybe just see if it causes a call, and it makes it magically work on x64, since your current call supposedly works on x86. It may not work 100% right out of the box, but I'm curious if it takes you further or at least a different result.

+1
source

Source: https://habr.com/ru/post/1482692/


All Articles