473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

GetExtendedTcpTable API Part 2

Hi all, I am trying to use the GetExtendedTcpTable API function and I having
some problems.

The program generates memory access errors, but not every time I run it
(about 1 n 3 times).

I first tried using StructureToPtr to read the TCP data, but then changed to
reading it 4 bytes at a time which did not make much diffrence in stability.

It does actualy return all the data correctly, but it crashes almost
randomly.

I think the problem might be something to do with the value returned in
dwTcpTableSize because when I check it's value it can be arround 380 when
there are only 4 TCP table records (each of which are 24 bytes long + 4
bytes for item count 96+4 ?).

I have included the whole function below followed by the API declaration. I
have used aliases for some types because it helps me convert the APIs from
MSDN so I have included my imports section.

Please help Thanks,
Michael.

<VB.Net 2005 on XP Pro SP2>

Imports
____________________________________
Imports System.Runtime.InteropServices

'** C++ Conversions Aliases. ***

Imports DWORD = System.UInt32

Imports BOOL = System.Boolean

'********************************
Function
____________________________________
Public Function GetExtendedTCPdata() As Integer

'* Use IP Version 4

Dim AF_INET As Integer = 2

'* Wlll store the TCP TABLE ROW Count.

Dim dwTcpTableSize As DWORD = 0

Dim RetVal As Integer

'* Get The Size of the TCP Table Data

RetVal = GetExtendedTcpTable(IntPtr.Zero, dwTcpTableSize, 1, AF_INET, 5, 0)

'* Create a Buffer

Dim iptrToTcpTableArray As IntPtr =
Marshal.AllocHGlobal(Marshal.SizeOf(dwTcpTableSize ))

RetVal = GetExtendedTcpTable(iptrToTcpTableArray, dwTcpTableSize, 1,
AF_INET, 5, 0)

'* Create a List to hold the managed TCP ROWS after

Dim ListOfTcpRows As New List(Of MIB_TCPROW_OWNER_PID)

If RetVal = ERROR_SUCCESS Then

Dim dwTCPRowCount As DWORD

'* Get the total number / count of TCP ROWS.

dwTCPRowCount = Marshal.ReadInt32(iptrToTcpTableArray, 0)

'* Offset for walking through the unmanaged structures

Dim intOffset As Integer = 0

For I As DWORD = 0 To dwTCPRowCount - 1

Dim TCP_ROW As MIB_TCPROW_OWNER_PID ' * Structure 24 Bytes Long

'* Read a sngle data structure from unmanaged memory 4 bytes at a time then
increment offset and loop

With TCP_ROW

..dwState = Marshal.ReadInt32(iptrToTcpTableArray, intOffset + 4)

..dwLocalAddr = Marshal.ReadInt32(iptrToTcpTableArray, intOffset + 8)

..dwLocalPort = Marshal.ReadInt32(iptrToTcpTableArray, intOffset + 12)

..dwRemoteAddr = Marshal.ReadInt32(iptrToTcpTableArray, intOffset + 16)

..dwRemotePort = Marshal.ReadInt32(iptrToTcpTableArray, intOffset + 20)

..dwOwningPid = Marshal.ReadInt32(iptrToTcpTableArray, intOffset + 24)

End With

'* Add the filled TCP ROW into the List.

ListOfTcpRows.Add(TCP_ROW)

'* Increment the byte offset by 24 bytes (1 TCP ROW)

intOffset += 24

Next

End If

'* Free the allocated memory that holds the TCP DATA ROWS

Marshal.FreeHGlobal(iptrToTcpTableArray)

GC.Collect()

'TODO return the data in a generics list

Return 0 ' ListOfTcpRows

End Function

API DECLARATION
________________

Public Declare Unicode Function GetExtendedTcpTable Lib _

"IPHLPAPI.DLL" _

( _

ByVal pTcpTable As IntPtr, _

ByRef pDwSize As DWORD, _

ByVal bOrder As BOOL, _

ByVal ulAf As Integer, _

ByVal TCP_TABLE_CLASS As Integer, _

ByVal Reserved As Integer) As Integer


Jan 5 '07 #1
3 5847
Changed dwTcpTableSize from DWORD to Integer, seems stable now, I will leave
it running a while and call that function in a Timer object as a quick test.

"Michael M." <no****@mike.comwrote in message
news:uQ**************@TK2MSFTNGP03.phx.gbl...
Hi all, I am trying to use the GetExtendedTcpTable API function and I
having some problems.

The program generates memory access errors, but not every time I run it
(about 1 n 3 times).

I first tried using StructureToPtr to read the TCP data, but then changed
to reading it 4 bytes at a time which did not make much diffrence in
stability.

It does actualy return all the data correctly, but it crashes almost
randomly.

I think the problem might be something to do with the value returned in
dwTcpTableSize because when I check it's value it can be arround 380 when
there are only 4 TCP table records (each of which are 24 bytes long + 4
bytes for item count 96+4 ?).

I have included the whole function below followed by the API declaration.
I have used aliases for some types because it helps me convert the APIs
from MSDN so I have included my imports section.

Please help Thanks,
Michael.

<VB.Net 2005 on XP Pro SP2>

Imports
____________________________________
Imports System.Runtime.InteropServices

'** C++ Conversions Aliases. ***

Imports DWORD = System.UInt32

Imports BOOL = System.Boolean

'********************************
Function
____________________________________
Public Function GetExtendedTCPdata() As Integer

'* Use IP Version 4

Dim AF_INET As Integer = 2

'* Wlll store the TCP TABLE ROW Count.

Dim dwTcpTableSize As DWORD = 0

Dim RetVal As Integer

'* Get The Size of the TCP Table Data

RetVal = GetExtendedTcpTable(IntPtr.Zero, dwTcpTableSize, 1, AF_INET, 5,
0)

'* Create a Buffer

Dim iptrToTcpTableArray As IntPtr =
Marshal.AllocHGlobal(Marshal.SizeOf(dwTcpTableSize ))

RetVal = GetExtendedTcpTable(iptrToTcpTableArray, dwTcpTableSize, 1,
AF_INET, 5, 0)

'* Create a List to hold the managed TCP ROWS after

Dim ListOfTcpRows As New List(Of MIB_TCPROW_OWNER_PID)

If RetVal = ERROR_SUCCESS Then

Dim dwTCPRowCount As DWORD

'* Get the total number / count of TCP ROWS.

dwTCPRowCount = Marshal.ReadInt32(iptrToTcpTableArray, 0)

'* Offset for walking through the unmanaged structures

Dim intOffset As Integer = 0

For I As DWORD = 0 To dwTCPRowCount - 1

Dim TCP_ROW As MIB_TCPROW_OWNER_PID ' * Structure 24 Bytes Long

'* Read a sngle data structure from unmanaged memory 4 bytes at a time
then increment offset and loop

With TCP_ROW

.dwState = Marshal.ReadInt32(iptrToTcpTableArray, intOffset + 4)

.dwLocalAddr = Marshal.ReadInt32(iptrToTcpTableArray, intOffset + 8)

.dwLocalPort = Marshal.ReadInt32(iptrToTcpTableArray, intOffset + 12)

.dwRemoteAddr = Marshal.ReadInt32(iptrToTcpTableArray, intOffset + 16)

.dwRemotePort = Marshal.ReadInt32(iptrToTcpTableArray, intOffset + 20)

.dwOwningPid = Marshal.ReadInt32(iptrToTcpTableArray, intOffset + 24)

End With

'* Add the filled TCP ROW into the List.

ListOfTcpRows.Add(TCP_ROW)

'* Increment the byte offset by 24 bytes (1 TCP ROW)

intOffset += 24

Next

End If

'* Free the allocated memory that holds the TCP DATA ROWS

Marshal.FreeHGlobal(iptrToTcpTableArray)

GC.Collect()

'TODO return the data in a generics list

Return 0 ' ListOfTcpRows

End Function

API DECLARATION
________________

Public Declare Unicode Function GetExtendedTcpTable Lib _

"IPHLPAPI.DLL" _

( _

ByVal pTcpTable As IntPtr, _

ByRef pDwSize As DWORD, _

ByVal bOrder As BOOL, _

ByVal ulAf As Integer, _

ByVal TCP_TABLE_CLASS As Integer, _

ByVal Reserved As Integer) As Integer


Jan 5 '07 #2
>Dim iptrToTcpTableArray As IntPtr =
>Marshal.AllocHGlobal(Marshal.SizeOf(dwTcpTableSiz e))
Here you're just allocating a 4 byte (= SizeOf(Integer)) buffer. What
you really want is

Dim iptrToTcpTableArray As IntPtr =
Marshal.AllocHGlobal(dwTcpTableSize)
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 5 '07 #3
Thanks Mattias,

I Just realised I had made that change allready and that is what solved my
problem not changing it's type as I belive both would allocate 4 bytes. I
had changed it to this:
Dim iptrToTcpTableArray As IntPtr = Marshal.AllocHGlobal((dwTcpTableSize))

but it still important that you pointed it out because I kind of stumbled
upon the fix. Now I have the real reason the code is now stable.

I was confused; so the Marshal.SizeOf just returns the size of the
underlying data type, I guess it makes sense now you point it out. I
probably made the same mistake using StructureToPtr()

:0)

Michael.
"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:u9*************@TK2MSFTNGP04.phx.gbl...
Dim iptrToTcpTableArray As IntPtr =
Marshal.AllocHGlobal(Marshal.SizeOf(dwTcpTableSi ze))

Here you're just allocating a 4 byte (= SizeOf(Integer)) buffer. What
you really want is

Dim iptrToTcpTableArray As IntPtr =
Marshal.AllocHGlobal(dwTcpTableSize)
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jan 5 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Jim Cochrane | last post by:
I just google-searched this group and could not find any references to this. I'm trying to figure out how to specify a three-part header with html. For example, left part ...
2
by: JumpinJeff | last post by:
I am about to revamp the most referenced table in my whole database and am unsure the best way to approch it. The table's primary key is also used as a part number for all the items in the table. ...
3
by: ChadDiesel | last post by:
Hello everyone. I need some advice on table structure for a new project I've been given. One of our customers sends us an Excel spreadsheet each week containing their order. Currently, someone...
7
by: Shannan Casteel via AccessMonster.com | last post by:
I have a form for entering part numbers along with the associated quantity for each part. There are 25 Part fields and 25 associated Quantity fields. If I go to record 1 and enter part number 1234...
2
by: maxw_cc | last post by:
Hi to all of you, I was wondering what the Semantics part in C standard is really for? What should be on the constraints part and what should be on the semantics part? Is the implementation...
2
by: Michael M. | last post by:
Hi All I am trying to use the function GetExtendedTcpTable, I looked on the MSDN found the C++ definition and tried to convert it but it does not seem to be working, it returns 87 which is =...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
1
by: tbrogdon | last post by:
I am new to this group and new to DBs. I am building a small DB for my work. We create sheetmetal parts. Each part has a part number (e.g., 1054471 or 50TG508506 - both formats are typical for a...
5
geolemon
by: geolemon | last post by:
I'm building a small-scale MRP system for an electronics manufacturing business. There are assemblies that are built, and parts that go into those assemblies. The individual component parts that go...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.