By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,889 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,889 IT Pros & Developers. It's quick & easy.

How to call api ?

P: n/a
I need to call the following api
DWORD GetAdaptersInfo(
PIP_ADAPTER_INFO pAdapterInfo,
PULONG pOutBufLen
);

What decalre ststement will be needed for these two pointers?

Nov 21 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Hi,
You may want to use WMI (System.Management namespace) to get network adapter
info.

If you still want to use the same API, this thread has an example (in C#) by
Willy Denoyette. Check it out:
http://www.developersdex.com/csharp/...P11.phx.gbl%3E

HTH.

"Marni" <www> wrote in message news:#1**************@TK2MSFTNGP15.phx.gbl...
I need to call the following api
DWORD GetAdaptersInfo(
PIP_ADAPTER_INFO pAdapterInfo,
PULONG pOutBufLen
);

What decalre ststement will be needed for these two pointers?
Nov 21 '05 #2

P: n/a
If anyone can do a declare staement for me it will be very
helpfull.

"Shiva" <sh******@online.excite.com> wrote in message
news:O4****************@TK2MSFTNGP15.phx.gbl...
Hi,
You may want to use WMI (System.Management namespace) to get network adapter info.

If you still want to use the same API, this thread has an example (in C#) by Willy Denoyette. Check it out:
http://www.developersdex.com/csharp/...P11.phx.gbl%3E
HTH.

"Marni" <www> wrote in message news:#1**************@TK2MSFTNGP15.phx.gbl... I need to call the following api
DWORD GetAdaptersInfo(
PIP_ADAPTER_INFO pAdapterInfo,
PULONG pOutBufLen
);

What decalre ststement will be needed for these two pointers?


Nov 21 '05 #3

P: n/a
Marni,

Just a sample of an API not yours,

I hope this helps?

Cor
\\\
Public Structure SYSTEMTIME
Public Year As Short
Public Month As Short
Public DayOfWeek As Short
Public Day As Short
Public Hour As Short
Public Minute As Short
Public Second As Short
Public Milliseconds As Short
End Structure
Public Declare Sub GetSystemTime _
Lib "kernel32" Alias "GetSystemTime" _
(ByRef lpSystemTime As SYSTEMTIME)
Public Declare Sub SetSystemTime _
Lib "kernel32" Alias "SetSystemTime" _
(ByRef lpSystemTime As SYSTEMTIME)
Private Sub Form1_Load(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim systime As SYSTEMTIME
GetSystemTime(systime)
MessageBox.Show(systime.Year.ToString())
MessageBox.Show(Now.ToString("MM/yyyy"))
End Sub
///

"Marni" <www>
If anyone can do a declare staement for me it will be very
helpfull.

"Shiva" <sh******@online.excite.com> wrote in message
news:O4****************@TK2MSFTNGP15.phx.gbl...
Hi,
You may want to use WMI (System.Management namespace) to get network

adapter
info.

If you still want to use the same API, this thread has an example (in C#)

by
Willy Denoyette. Check it out:

http://www.developersdex.com/csharp/...P11.phx.gbl%3E

HTH.

"Marni" <www> wrote in message

news:#1**************@TK2MSFTNGP15.phx.gbl...
I need to call the following api
DWORD GetAdaptersInfo(
PIP_ADAPTER_INFO pAdapterInfo,
PULONG pOutBufLen
);

What decalre ststement will be needed for these two pointers?

Nov 21 '05 #4

P: n/a
Here is the VB.NET equivalent of Willy Denoyette's C# wrapper class for the
GetAdaptersInfo() API. The GetMacAddress() is the method that uses the API
(as appeared in his original post).

Hope this helps.

Public Class IpHelper

<DllImport("IphlpApi", CharSet:=CharSet.Ansi, SetLastError:=True),
System.Security.SuppressUnmanagedCodeSecurityAttri bute()> _

Public Shared Function GetAdaptersInfo(ByVal pAdapterInfo As IntPtr, ByRef
pOutBufLen As Integer) As Integer

End Function

Structure _IP_ADDR_STRING

Private [Next] As IntPtr

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> _

Friend IpAddress As String

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> _

Friend IpMask As String

Private Context As Integer

End Structure '_IP_ADDR_STRING

<StructLayout(LayoutKind.Sequential)> _

Structure _IP_ADAPTER_INFO

Friend [Next] As IntPtr

Private ComboIndex As Integer

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=260)> _

Friend AdapterName() As Byte

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=132)> _

Friend Description() As Byte

Friend AddressLength As Integer

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> _

Friend Address() As Byte

Private Index As System.UInt32

Private Type As System.UInt32

Private DhcpEnabled As Integer

Private CurrentIpAddress As IntPtr

Friend IpAddressList As _IP_ADDR_STRING

Friend GatewayList As _IP_ADDR_STRING

Friend DhcpServer As _IP_ADDR_STRING

Private HaveWins As Boolean

Friend PrimaryWinsServer As _IP_ADDR_STRING

Friend SecondaryWinsServer As _IP_ADDR_STRING

Private LeaseObtained As Integer

Private LeaseExpires As Integer

End Structure

Public Function GetMacAddress() As ArrayList

Dim pAdapterInfo As IntPtr = IntPtr.Zero

Dim pOutBufLen As Integer = 0

Dim ad As New ArrayList

Dim ipinfo As New _IP_ADAPTER_INFO

Dim ret As Integer = GetAdaptersInfo(pAdapterInfo, pOutBufLen)

pAdapterInfo = Marshal.AllocHGlobal(pOutBufLen)

ret = GetAdaptersInfo(pAdapterInfo, pOutBufLen)

ipinfo = CType(Marshal.PtrToStructure(pAdapterInfo,
GetType(_IP_ADAPTER_INFO)), _IP_ADAPTER_INFO)

ad.Add(ipinfo.Address)

While Not ipinfo.Next.op_Equality(ipinfo.Next, IntPtr.Zero)

ipinfo = CType(Marshal.PtrToStructure(ipinfo.Next,
GetType(_IP_ADAPTER_INFO)), _IP_ADAPTER_INFO)

ad.Add(ipinfo.Address)

End While

Marshal.FreeHGlobal(pAdapterInfo)

Return ad

End Function 'GetMacAddress

End Class

"Marni" <www> wrote in message news:#l**************@tk2msftngp13.phx.gbl...
If anyone can do a declare staement for me it will be very
helpfull.

"Shiva" <sh******@online.excite.com> wrote in message
news:O4****************@TK2MSFTNGP15.phx.gbl...
Hi,
You may want to use WMI (System.Management namespace) to get network adapter info.

If you still want to use the same API, this thread has an example (in C#) by Willy Denoyette. Check it out:
http://www.developersdex.com/csharp/...P11.phx.gbl%3E
HTH.

"Marni" <www> wrote in message news:#1**************@TK2MSFTNGP15.phx.gbl... I need to call the following api
DWORD GetAdaptersInfo(
PIP_ADAPTER_INFO pAdapterInfo,
PULONG pOutBufLen
);

What decalre ststement will be needed for these two pointers?

Nov 21 '05 #5

P: n/a
It must be the one I need. I looked at the sample suggested
by Shiva, but it's too complicated for me to do it in VB.

It looks simple, one api, 2 pointers, that's it. Or am I
just not realising the work involved?

BTW, if it's any easier, I do not need to access all the fields
of the first parameter, only the gateway ip field, that's it.
Nov 21 '05 #6

P: n/a
In case you need the WMI way of getting the gateway IP:

'Imports System.Management

Dim searcher As New ManagementObjectSearcher("SELECT DefaultIPGateway FROM
Win32_NetworkAdapterConfiguration")
Dim gw As ManagementObject
For Each gw In searcher.Get()
If Not (gw("DefaultIPGateway") Is Nothing) Then
Console.WriteLine(CType(gw("DefaultIPGateway"), String())(0))
End If
Next gw

HTH

"Marni" <www> wrote in message news:Of*************@TK2MSFTNGP11.phx.gbl...
It must be the one I need. I looked at the sample suggested
by Shiva, but it's too complicated for me to do it in VB.

It looks simple, one api, 2 pointers, that's it. Or am I
just not realising the work involved?

BTW, if it's any easier, I do not need to access all the fields
of the first parameter, only the gateway ip field, that's it.
Nov 21 '05 #7

P: n/a

"Shiva" <sh******@online.excite.com> wrote in message
news:eB*************@TK2MSFTNGP11.phx.gbl...
In case you need the WMI way of getting the gateway IP:

'Imports System.Management

Dim searcher As New ManagementObjectSearcher("SELECT DefaultIPGateway FROM
Win32_NetworkAdapterConfiguration")
Dim gw As ManagementObject
For Each gw In searcher.Get()
If Not (gw("DefaultIPGateway") Is Nothing) Then
Console.WriteLine(CType(gw("DefaultIPGateway"), String())(0))
End If
Next gw
Do we get a gateway for each adapter? Is there a way to check which
adapter connects to the Internet. I.e. what do I do if I have an ethernet
card, a bluetooth usb device, and a wifi card, but only one of them is
connected to the internet, how can I know which one?

HTH

"Marni" <www> wrote in message news:Of*************@TK2MSFTNGP11.phx.gbl... It must be the one I need. I looked at the sample suggested
by Shiva, but it's too complicated for me to do it in VB.

It looks simple, one api, 2 pointers, that's it. Or am I
just not realising the work involved?

BTW, if it's any easier, I do not need to access all the fields
of the first parameter, only the gateway ip field, that's it.


Nov 21 '05 #8

P: n/a
Thanks a lot
Just a few final points,

where can I find the documentation for command in html delimiters,
such as

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)>

These do not seem to come up on the 'Index' sensitive search.
Is there a list of all the possible commands in html delimiters?

Also, how do I print the result, for example, I wrote this test program:

Imports System.Text
Imports System.Runtime.InteropServices

Module Module1

Sub Main()

Dim a As ArrayList
Dim b As New IpHelper
a = b.GetMacAddress
Dim retval As String
Dim bytes(1000) As Byte
a.CopyTo(bytes)
retval = Encoding.ASCII.GetString(bytes)
MsgBox(retval)
' this did not work, sorry I simply don't know how to use ArrayList
End Sub

End Module

Public Class IpHelper
....

"Shiva" <sh******@online.excite.com> wrote in message
news:eG*************@TK2MSFTNGP10.phx.gbl...
Here is the VB.NET equivalent of Willy Denoyette's C# wrapper class for the GetAdaptersInfo() API. The GetMacAddress() is the method that uses the API
(as appeared in his original post).

Hope this helps.

Public Class IpHelper

<DllImport("IphlpApi", CharSet:=CharSet.Ansi, SetLastError:=True),
System.Security.SuppressUnmanagedCodeSecurityAttri bute()> _

Public Shared Function GetAdaptersInfo(ByVal pAdapterInfo As IntPtr, ByRef
pOutBufLen As Integer) As Integer

End Function

Structure _IP_ADDR_STRING

Private [Next] As IntPtr

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> _

Friend IpAddress As String

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> _

Friend IpMask As String

Private Context As Integer

End Structure '_IP_ADDR_STRING

<StructLayout(LayoutKind.Sequential)> _

Structure _IP_ADAPTER_INFO

Friend [Next] As IntPtr

Private ComboIndex As Integer

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=260)> _

Friend AdapterName() As Byte

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=132)> _

Friend Description() As Byte

Friend AddressLength As Integer

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> _

Friend Address() As Byte

Private Index As System.UInt32

Private Type As System.UInt32

Private DhcpEnabled As Integer

Private CurrentIpAddress As IntPtr

Friend IpAddressList As _IP_ADDR_STRING

Friend GatewayList As _IP_ADDR_STRING

Friend DhcpServer As _IP_ADDR_STRING

Private HaveWins As Boolean

Friend PrimaryWinsServer As _IP_ADDR_STRING

Friend SecondaryWinsServer As _IP_ADDR_STRING

Private LeaseObtained As Integer

Private LeaseExpires As Integer

End Structure

Public Function GetMacAddress() As ArrayList

Dim pAdapterInfo As IntPtr = IntPtr.Zero

Dim pOutBufLen As Integer = 0

Dim ad As New ArrayList

Dim ipinfo As New _IP_ADAPTER_INFO

Dim ret As Integer = GetAdaptersInfo(pAdapterInfo, pOutBufLen)

pAdapterInfo = Marshal.AllocHGlobal(pOutBufLen)

ret = GetAdaptersInfo(pAdapterInfo, pOutBufLen)

ipinfo = CType(Marshal.PtrToStructure(pAdapterInfo,
GetType(_IP_ADAPTER_INFO)), _IP_ADAPTER_INFO)

ad.Add(ipinfo.Address)

While Not ipinfo.Next.op_Equality(ipinfo.Next, IntPtr.Zero)

ipinfo = CType(Marshal.PtrToStructure(ipinfo.Next,
GetType(_IP_ADAPTER_INFO)), _IP_ADAPTER_INFO)

ad.Add(ipinfo.Address)

End While

Marshal.FreeHGlobal(pAdapterInfo)

Return ad

End Function 'GetMacAddress

End Class

"Marni" <www> wrote in message news:#l**************@tk2msftngp13.phx.gbl... If anyone can do a declare staement for me it will be very
helpfull.

"Shiva" <sh******@online.excite.com> wrote in message
news:O4****************@TK2MSFTNGP15.phx.gbl...
Hi,
You may want to use WMI (System.Management namespace) to get network adapter
info.

If you still want to use the same API, this thread has an example (in C#) by
Willy Denoyette. Check it out:

http://www.developersdex.com/csharp/...P11.phx.gbl%3E
HTH.

"Marni" <www> wrote in message

news:#1**************@TK2MSFTNGP15.phx.gbl...
I need to call the following api
DWORD GetAdaptersInfo(
PIP_ADAPTER_INFO pAdapterInfo,
PULONG pOutBufLen
);

What decalre ststement will be needed for these two pointers?



Nov 21 '05 #9

P: n/a
Program doesn't work

!!!
"Shiva" <sh******@online.excite.com> wrote in message
news:eG*************@TK2MSFTNGP10.phx.gbl...
Here is the VB.NET equivalent of Willy Denoyette's C# wrapper class for the GetAdaptersInfo() API. The GetMacAddress() is the method that uses the API
(as appeared in his original post).

Hope this helps.

Public Class IpHelper

<DllImport("IphlpApi", CharSet:=CharSet.Ansi, SetLastError:=True),
System.Security.SuppressUnmanagedCodeSecurityAttri bute()> _

Public Shared Function GetAdaptersInfo(ByVal pAdapterInfo As IntPtr, ByRef
pOutBufLen As Integer) As Integer

End Function

Structure _IP_ADDR_STRING

Private [Next] As IntPtr

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> _

Friend IpAddress As String

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> _

Friend IpMask As String

Private Context As Integer

End Structure '_IP_ADDR_STRING

<StructLayout(LayoutKind.Sequential)> _

Structure _IP_ADAPTER_INFO

Friend [Next] As IntPtr

Private ComboIndex As Integer

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=260)> _

Friend AdapterName() As Byte

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=132)> _

Friend Description() As Byte

Friend AddressLength As Integer

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> _

Friend Address() As Byte

Private Index As System.UInt32

Private Type As System.UInt32

Private DhcpEnabled As Integer

Private CurrentIpAddress As IntPtr

Friend IpAddressList As _IP_ADDR_STRING

Friend GatewayList As _IP_ADDR_STRING

Friend DhcpServer As _IP_ADDR_STRING

Private HaveWins As Boolean

Friend PrimaryWinsServer As _IP_ADDR_STRING

Friend SecondaryWinsServer As _IP_ADDR_STRING

Private LeaseObtained As Integer

Private LeaseExpires As Integer

End Structure

Public Function GetMacAddress() As ArrayList

Dim pAdapterInfo As IntPtr = IntPtr.Zero

Dim pOutBufLen As Integer = 0

Dim ad As New ArrayList

Dim ipinfo As New _IP_ADAPTER_INFO

Dim ret As Integer = GetAdaptersInfo(pAdapterInfo, pOutBufLen)

pAdapterInfo = Marshal.AllocHGlobal(pOutBufLen)

ret = GetAdaptersInfo(pAdapterInfo, pOutBufLen)

ipinfo = CType(Marshal.PtrToStructure(pAdapterInfo,
GetType(_IP_ADAPTER_INFO)), _IP_ADAPTER_INFO)

ad.Add(ipinfo.Address)

While Not ipinfo.Next.op_Equality(ipinfo.Next, IntPtr.Zero)

ipinfo = CType(Marshal.PtrToStructure(ipinfo.Next,
GetType(_IP_ADAPTER_INFO)), _IP_ADAPTER_INFO)

ad.Add(ipinfo.Address)

End While

Marshal.FreeHGlobal(pAdapterInfo)

Return ad

End Function 'GetMacAddress

End Class

"Marni" <www> wrote in message news:#l**************@tk2msftngp13.phx.gbl... If anyone can do a declare staement for me it will be very
helpfull.

"Shiva" <sh******@online.excite.com> wrote in message
news:O4****************@TK2MSFTNGP15.phx.gbl...
Hi,
You may want to use WMI (System.Management namespace) to get network adapter
info.

If you still want to use the same API, this thread has an example (in C#) by
Willy Denoyette. Check it out:

http://www.developersdex.com/csharp/...P11.phx.gbl%3E
HTH.

"Marni" <www> wrote in message

news:#1**************@TK2MSFTNGP15.phx.gbl...
I need to call the following api
DWORD GetAdaptersInfo(
PIP_ADAPTER_INFO pAdapterInfo,
PULONG pOutBufLen
);

What decalre ststement will be needed for these two pointers?



Nov 21 '05 #10

P: n/a
The program works, but only for GetMacAddress.
What do I do to obtain the gateway. I tried

ad.Add(ipinfo.GatewayList)

instead of

'ad.Add(ipinfo.Address)

and

Shared Sub main1()

Dim a As ArrayList

Dim b As New IpHelper

a = b.GetMacAddress

For Each c As _IP_ADDR_STRING In a

MsgBox(c.ToString)

Next

End Sub
strangely, the msgbox is printing the name of the structure
rather than an ip address, i.e
ConsoleApplication1.IpHelper+_IP_ADDR_STRING

Tia

"Marni" <www> wrote in message news:Ov**************@TK2MSFTNGP12.phx.gbl...
Program doesn't work

!!!
"Shiva" <sh******@online.excite.com> wrote in message
news:eG*************@TK2MSFTNGP10.phx.gbl...
Here is the VB.NET equivalent of Willy Denoyette's C# wrapper class for

the
GetAdaptersInfo() API. The GetMacAddress() is the method that uses the API
(as appeared in his original post).

Hope this helps.

Public Class IpHelper

<DllImport("IphlpApi", CharSet:=CharSet.Ansi, SetLastError:=True),
System.Security.SuppressUnmanagedCodeSecurityAttri bute()> _

Public Shared Function GetAdaptersInfo(ByVal pAdapterInfo As IntPtr, ByRef pOutBufLen As Integer) As Integer

End Function

Structure _IP_ADDR_STRING

Private [Next] As IntPtr

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> _

Friend IpAddress As String

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> _

Friend IpMask As String

Private Context As Integer

End Structure '_IP_ADDR_STRING

<StructLayout(LayoutKind.Sequential)> _

Structure _IP_ADAPTER_INFO

Friend [Next] As IntPtr

Private ComboIndex As Integer

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=260)> _

Friend AdapterName() As Byte

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=132)> _

Friend Description() As Byte

Friend AddressLength As Integer

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> _

Friend Address() As Byte

Private Index As System.UInt32

Private Type As System.UInt32

Private DhcpEnabled As Integer

Private CurrentIpAddress As IntPtr

Friend IpAddressList As _IP_ADDR_STRING

Friend GatewayList As _IP_ADDR_STRING

Friend DhcpServer As _IP_ADDR_STRING

Private HaveWins As Boolean

Friend PrimaryWinsServer As _IP_ADDR_STRING

Friend SecondaryWinsServer As _IP_ADDR_STRING

Private LeaseObtained As Integer

Private LeaseExpires As Integer

End Structure

Public Function GetMacAddress() As ArrayList

Dim pAdapterInfo As IntPtr = IntPtr.Zero

Dim pOutBufLen As Integer = 0

Dim ad As New ArrayList

Dim ipinfo As New _IP_ADAPTER_INFO

Dim ret As Integer = GetAdaptersInfo(pAdapterInfo, pOutBufLen)

pAdapterInfo = Marshal.AllocHGlobal(pOutBufLen)

ret = GetAdaptersInfo(pAdapterInfo, pOutBufLen)

ipinfo = CType(Marshal.PtrToStructure(pAdapterInfo,
GetType(_IP_ADAPTER_INFO)), _IP_ADAPTER_INFO)

ad.Add(ipinfo.Address)

While Not ipinfo.Next.op_Equality(ipinfo.Next, IntPtr.Zero)

ipinfo = CType(Marshal.PtrToStructure(ipinfo.Next,
GetType(_IP_ADAPTER_INFO)), _IP_ADAPTER_INFO)

ad.Add(ipinfo.Address)

End While

Marshal.FreeHGlobal(pAdapterInfo)

Return ad

End Function 'GetMacAddress

End Class

"Marni" <www> wrote in message

news:#l**************@tk2msftngp13.phx.gbl...
If anyone can do a declare staement for me it will be very
helpfull.

"Shiva" <sh******@online.excite.com> wrote in message
news:O4****************@TK2MSFTNGP15.phx.gbl...
Hi,
You may want to use WMI (System.Management namespace) to get network

adapter
info.

If you still want to use the same API, this thread has an example (in

C#)
by
Willy Denoyette. Check it out:

http://www.developersdex.com/csharp/...P11.phx.gbl%3E

HTH.

"Marni" <www> wrote in message

news:#1**************@TK2MSFTNGP15.phx.gbl...
I need to call the following api
DWORD GetAdaptersInfo(
PIP_ADAPTER_INFO pAdapterInfo,
PULONG pOutBufLen
);

What decalre ststement will be needed for these two pointers?



Nov 21 '05 #11

P: n/a
In the GetMacAddress() method, instead of ad.Add(ipinfo.Address), have
ad.Add(ipinfo.GatewayList.IpAddress).

It adds the gateway IP (as a string) to the array list. Individual elements
can be accessed from the array list as ad[0], ad[1]

HTH

"Marni" <www> wrote in message news:ON**************@TK2MSFTNGP11.phx.gbl...
The program works, but only for GetMacAddress.
What do I do to obtain the gateway. I tried

ad.Add(ipinfo.GatewayList)

instead of

'ad.Add(ipinfo.Address)

and

Shared Sub main1()

Dim a As ArrayList

Dim b As New IpHelper

a = b.GetMacAddress

For Each c As _IP_ADDR_STRING In a

MsgBox(c.ToString)

Next

End Sub
strangely, the msgbox is printing the name of the structure
rather than an ip address, i.e
ConsoleApplication1.IpHelper+_IP_ADDR_STRING

Tia

"Marni" <www> wrote in message news:Ov**************@TK2MSFTNGP12.phx.gbl...
Program doesn't work

!!!
"Shiva" <sh******@online.excite.com> wrote in message
news:eG*************@TK2MSFTNGP10.phx.gbl...
Here is the VB.NET equivalent of Willy Denoyette's C# wrapper class for

the
GetAdaptersInfo() API. The GetMacAddress() is the method that uses the API
(as appeared in his original post).

Hope this helps.

Public Class IpHelper

<DllImport("IphlpApi", CharSet:=CharSet.Ansi, SetLastError:=True),
System.Security.SuppressUnmanagedCodeSecurityAttri bute()> _

Public Shared Function GetAdaptersInfo(ByVal pAdapterInfo As IntPtr, ByRef pOutBufLen As Integer) As Integer

End Function

Structure _IP_ADDR_STRING

Private [Next] As IntPtr

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> _

Friend IpAddress As String

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> _

Friend IpMask As String

Private Context As Integer

End Structure '_IP_ADDR_STRING

<StructLayout(LayoutKind.Sequential)> _

Structure _IP_ADAPTER_INFO

Friend [Next] As IntPtr

Private ComboIndex As Integer

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=260)> _

Friend AdapterName() As Byte

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=132)> _

Friend Description() As Byte

Friend AddressLength As Integer

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> _

Friend Address() As Byte

Private Index As System.UInt32

Private Type As System.UInt32

Private DhcpEnabled As Integer

Private CurrentIpAddress As IntPtr

Friend IpAddressList As _IP_ADDR_STRING

Friend GatewayList As _IP_ADDR_STRING

Friend DhcpServer As _IP_ADDR_STRING

Private HaveWins As Boolean

Friend PrimaryWinsServer As _IP_ADDR_STRING

Friend SecondaryWinsServer As _IP_ADDR_STRING

Private LeaseObtained As Integer

Private LeaseExpires As Integer

End Structure

Public Function GetMacAddress() As ArrayList

Dim pAdapterInfo As IntPtr = IntPtr.Zero

Dim pOutBufLen As Integer = 0

Dim ad As New ArrayList

Dim ipinfo As New _IP_ADAPTER_INFO

Dim ret As Integer = GetAdaptersInfo(pAdapterInfo, pOutBufLen)

pAdapterInfo = Marshal.AllocHGlobal(pOutBufLen)

ret = GetAdaptersInfo(pAdapterInfo, pOutBufLen)

ipinfo = CType(Marshal.PtrToStructure(pAdapterInfo,
GetType(_IP_ADAPTER_INFO)), _IP_ADAPTER_INFO)

ad.Add(ipinfo.Address)

While Not ipinfo.Next.op_Equality(ipinfo.Next, IntPtr.Zero)

ipinfo = CType(Marshal.PtrToStructure(ipinfo.Next,
GetType(_IP_ADAPTER_INFO)), _IP_ADAPTER_INFO)

ad.Add(ipinfo.Address)

End While

Marshal.FreeHGlobal(pAdapterInfo)

Return ad

End Function 'GetMacAddress

End Class

"Marni" <www> wrote in message

news:#l**************@tk2msftngp13.phx.gbl...
If anyone can do a declare staement for me it will be very
helpfull.

"Shiva" <sh******@online.excite.com> wrote in message
news:O4****************@TK2MSFTNGP15.phx.gbl...
Hi,
You may want to use WMI (System.Management namespace) to get network

adapter
info.

If you still want to use the same API, this thread has an example (in

C#)
by
Willy Denoyette. Check it out:

http://www.developersdex.com/csharp/...P11.phx.gbl%3E

HTH.

"Marni" <www> wrote in message

news:#1**************@TK2MSFTNGP15.phx.gbl...
I need to call the following api
DWORD GetAdaptersInfo(
PIP_ADAPTER_INFO pAdapterInfo,
PULONG pOutBufLen
);

What decalre ststement will be needed for these two pointers?


Nov 21 '05 #12

P: n/a
Hi,
I am not quite sure about what you mean by 'html delimters'. To get help on
MarshalAs, look for MarshalAsAttribute class in the MSDN Help (just have the
Attribute suffix for any attribute name).

To access individual elements in an arraylist, use array-like indexing
syntax. Eg: retVal(0), retVal(1), etc

"Marni" <www> wrote in message news:#0**************@TK2MSFTNGP11.phx.gbl...
Thanks a lot
Just a few final points,

where can I find the documentation for command in html delimiters,
such as

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)>

These do not seem to come up on the 'Index' sensitive search.
Is there a list of all the possible commands in html delimiters?

Also, how do I print the result, for example, I wrote this test program:

Imports System.Text
Imports System.Runtime.InteropServices

Module Module1

Sub Main()

Dim a As ArrayList
Dim b As New IpHelper
a = b.GetMacAddress
Dim retval As String
Dim bytes(1000) As Byte
a.CopyTo(bytes)
retval = Encoding.ASCII.GetString(bytes)
MsgBox(retval)
' this did not work, sorry I simply don't know how to use ArrayList
End Sub

End Module

Public Class IpHelper
....

"Shiva" <sh******@online.excite.com> wrote in message
news:eG*************@TK2MSFTNGP10.phx.gbl...
Here is the VB.NET equivalent of Willy Denoyette's C# wrapper class for the GetAdaptersInfo() API. The GetMacAddress() is the method that uses the API
(as appeared in his original post).

Hope this helps.

Public Class IpHelper

<DllImport("IphlpApi", CharSet:=CharSet.Ansi, SetLastError:=True),
System.Security.SuppressUnmanagedCodeSecurityAttri bute()> _

Public Shared Function GetAdaptersInfo(ByVal pAdapterInfo As IntPtr, ByRef
pOutBufLen As Integer) As Integer

End Function

Structure _IP_ADDR_STRING

Private [Next] As IntPtr

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> _

Friend IpAddress As String

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> _

Friend IpMask As String

Private Context As Integer

End Structure '_IP_ADDR_STRING

<StructLayout(LayoutKind.Sequential)> _

Structure _IP_ADAPTER_INFO

Friend [Next] As IntPtr

Private ComboIndex As Integer

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=260)> _

Friend AdapterName() As Byte

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=132)> _

Friend Description() As Byte

Friend AddressLength As Integer

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> _

Friend Address() As Byte

Private Index As System.UInt32

Private Type As System.UInt32

Private DhcpEnabled As Integer

Private CurrentIpAddress As IntPtr

Friend IpAddressList As _IP_ADDR_STRING

Friend GatewayList As _IP_ADDR_STRING

Friend DhcpServer As _IP_ADDR_STRING

Private HaveWins As Boolean

Friend PrimaryWinsServer As _IP_ADDR_STRING

Friend SecondaryWinsServer As _IP_ADDR_STRING

Private LeaseObtained As Integer

Private LeaseExpires As Integer

End Structure

Public Function GetMacAddress() As ArrayList

Dim pAdapterInfo As IntPtr = IntPtr.Zero

Dim pOutBufLen As Integer = 0

Dim ad As New ArrayList

Dim ipinfo As New _IP_ADAPTER_INFO

Dim ret As Integer = GetAdaptersInfo(pAdapterInfo, pOutBufLen)

pAdapterInfo = Marshal.AllocHGlobal(pOutBufLen)

ret = GetAdaptersInfo(pAdapterInfo, pOutBufLen)

ipinfo = CType(Marshal.PtrToStructure(pAdapterInfo,
GetType(_IP_ADAPTER_INFO)), _IP_ADAPTER_INFO)

ad.Add(ipinfo.Address)

While Not ipinfo.Next.op_Equality(ipinfo.Next, IntPtr.Zero)

ipinfo = CType(Marshal.PtrToStructure(ipinfo.Next,
GetType(_IP_ADAPTER_INFO)), _IP_ADAPTER_INFO)

ad.Add(ipinfo.Address)

End While

Marshal.FreeHGlobal(pAdapterInfo)

Return ad

End Function 'GetMacAddress

End Class

"Marni" <www> wrote in message news:#l**************@tk2msftngp13.phx.gbl... If anyone can do a declare staement for me it will be very
helpfull.

"Shiva" <sh******@online.excite.com> wrote in message
news:O4****************@TK2MSFTNGP15.phx.gbl...
Hi,
You may want to use WMI (System.Management namespace) to get network adapter
info.

If you still want to use the same API, this thread has an example (in C#) by
Willy Denoyette. Check it out:

http://www.developersdex.com/csharp/...P11.phx.gbl%3E
HTH.

"Marni" <www> wrote in message

news:#1**************@TK2MSFTNGP15.phx.gbl...
I need to call the following api
DWORD GetAdaptersInfo(
PIP_ADAPTER_INFO pAdapterInfo,
PULONG pOutBufLen
);

What decalre ststement will be needed for these two pointers?


Nov 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.