|
-
Jun 3rd, 2008, 07:04 AM
#1
.NET IsAnyAdapterConnected API to Mobile
Since we don't get many Mobile examples here i decided to make one.
It's a conversion of the IsAnyAdapterConnected API class to mobile standards.
Before i move on i must say that the code has some merged snippets of the intel software networks forum and Microsoft Msdn site.
Also since this is done on an emulator I'd appreciate if someone test it and found out if it works on a real mobile with connected adapter.
So here we go...
Create a mobile application and add a button.
OK, so the idea here is to find if you have adapters and display their info.
For this you will need to create 2 functions. One that will pass the adapter structure and find if we have adapters connected and the other that will retrieve the data.
Code:
Public Class Form1
Private Declare Function GetAdaptersInfo Lib "iphlpapi.dll" (ByVal pAdapterInfo As IntPtr, ByRef OutBufferLen As Integer) As Integer
Private Function GetMyAdaptersInfo(ByRef info As IP_ADAPTER_INFO) As Boolean
Dim outBufLen As Integer = 0
Dim pAdapterInfo As IntPtr = IntPtr.Zero
Dim ret As Integer = 0
Try
' Call GetAdaptersInfo to determine the size of the
' memory buffer needed
ret = GetAdaptersInfo(pAdapterInfo, outBufLen)
' Allocate enough memory to hold the IP_ADAPTER_INFO data.
pAdapterInfo = MarshaLinvoke.AllocHLocal(CType(outBufLen, UInteger))
' Call GetAdapterInfo to populate the buffer at pAdapterInfo
ret = GetAdaptersInfo(pAdapterInfo, outBufLen)
' ret will be zero if successful
If (ret <> 0) Then
' The call to GetAdaptersInfo failed. Set info to null
' and return false.
info = Nothing
Return False
Else
' Success! Create a new instance of the IP_ADAPTER_INFO
' structure with the data from the memory buffer.
info = New IP_ADAPTER_INFO(pAdapterInfo)
End If
Finally
' Free up any allocated memory.
MarshaLinvoke.FreeHLocal(pAdapterInfo)
End Try
' Call to GetAdaptersInfo was successful so return true.
Return True
End Function
Public Function IsAnyAdapterConnected() As Boolean
Dim adapterInfo As IP_ADAPTER_INFO = Nothing
Dim b As Boolean = False
Dim connected As Boolean = False
' Retrieve information about any adapters present.
b = GetMyAdaptersInfo(adapterInfo)
' If the call was successful and we have some info about
' the network adapters, check if there is a current IP address.
If (b _
AndAlso (Not (adapterInfo) Is Nothing)) Then
If (adapterInfo.CurrentIpAddress.IpAddress.String.Length > 0) Then
' Got an IP address. Adapter is connected.
connected = True
End If
End If
Return connected
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim adaptsucces As Boolean = IsAnyAdapterConnected()
End Sub
End Class
continued...
Last edited by sapator; Jun 4th, 2008 at 01:53 AM.
-
Jun 3rd, 2008, 07:06 AM
#2
Re: .NET IsAnyAdapterConnected API to Mobile
Also note that you can get the adapter info in the "adapterInfo" variable that is actually a structure of what's comming next
-
Jun 3rd, 2008, 07:11 AM
#3
Re: .NET IsAnyAdapterConnected API to Mobile
Next we have the class declarations for the adapter.
This is provided as is, borrowed from the intel software networks but it is converted to VB from C# which in return is converted from C++ 
Code:
Public Class IP_ADDR_STRING
Public [Next] As IntPtr
Public IpAddress As IP_ADDRESS_STRING
Public IpMask As IP_ADDRESS_STRING
Public Context As Integer
Public Sub New(ByVal baseAddress As IntPtr)
MyBase.New()
End Sub
End Class
Public Class IP_ADDRESS_STRING
Public [String] As String
Public Sub New(ByVal baseAddress As IntPtr, ByVal offset As Integer)
MyBase.New()
End Sub
End Class
Public Class IP_ADAPTER_INFO
Public [Next] As IntPtr
Public ComboIndex As UInteger
Public AdapterName As String
Public Description As String
Public AddressLength As UInteger
Public Address As Byte()
Public Index As UInteger
Public Type As UInteger
Public DhcpEnabled As UInteger
Public CurrentIpAddress As IP_ADDR_STRING
Public IpAddressList As IP_ADDR_STRING
Public GatewayList As IP_ADDR_STRING
Public DhcpServer As IP_ADDR_STRING
Public HaveWins As UInteger
Public PrimaryWinsServer As IP_ADDR_STRING
Public SecondaryWinsServer As IP_ADDR_STRING
Public LeaseObtained As DateTime
Public LeaseExpires As DateTime
Public Sub New(ByVal baseAddress As IntPtr)
End Sub
End Class
Last edited by sapator; Jun 9th, 2008 at 03:38 AM.
-
Jun 3rd, 2008, 07:20 AM
#4
Re: .NET IsAnyAdapterConnected API to Mobile
Finally you may notice a call to Public Class MarshaLinvoke
Since we need to Allocate enough memory to hold the Adapter buffer info we must create a marshal allocation class.. Msdn to the rescue.
Code:
Public Class MarshaLinvoke
<System.Runtime.InteropServices.DllImport("coredll.dll", SetLastError:=True)> _
Private Shared Function LocalAlloc(ByVal uFlags As Integer, _
ByVal uBytes As Integer) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("coredll.dll", SetLastError:=True)> _
Private Shared Function LocalFree(ByVal hMem As IntPtr) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("coredll.dll", SetLastError:=True)> _
Private Shared Function LocalReAlloc(ByVal hMem As IntPtr, _
ByVal uBytes As Integer, ByVal fuFlags As Integer) As IntPtr
End Function
Private Const LMEM_FIXED As Integer = 0
Private Const LMEM_MOVEABLE As Integer = 2
Private Const LMEM_ZEROINIT As Integer = &H40
Private Const LPTR = (LMEM_FIXED Or LMEM_ZEROINIT)
' Allocates a block of memory using LocalAlloc
Public Shared Function AllocHLocal(ByVal cb As Integer) As IntPtr
Return LocalAlloc(LPTR, cb)
End Function
' Frees memory allocated by AllocHLocal
Public Shared Sub FreeHLocal(ByVal hlocal As IntPtr)
If Not hlocal.Equals(IntPtr.Zero) Then
If Not IntPtr.Zero.Equals(LocalFree(hlocal)) Then
Throw New System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error())
End If
hlocal = IntPtr.Zero
End If
End Sub
' Resizes a block of memory previously allocated with AllocHLocal
Public Shared Function ReAllocHLocal(ByVal pv As IntPtr, _
ByVal cb As Integer) As IntPtr
Dim newMem As IntPtr = LocalReAlloc(pv, cb, LMEM_MOVEABLE)
If newMem.Equals(IntPtr.Zero) Then
Throw New OutOfMemoryException
End If
Return newMem
End Function
' Copies the contents of a managed string to unmanaged memory
Public Shared Function StringToHLocalUni( _
ByVal s As String) As IntPtr
If s Is Nothing Then
Return IntPtr.Zero
Else
Dim nc As Integer = s.Length
Dim len As Integer = 2 * (1 + nc)
Dim hLocal As IntPtr = AllocHLocal(len)
If hLocal.Equals(IntPtr.Zero) Then
Throw New OutOfMemoryException
Else
System.Runtime.InteropServices.Marshal.Copy(s.ToCharArray(), 0, hLocal, s.Length)
Return hLocal
End If
End If
End Function
End Class
-
Jun 3rd, 2008, 07:21 AM
#5
Re: .NET IsAnyAdapterConnected API to Mobile
And that is that. Now we can get the adapters and their info.
Last edited by sapator; Jun 3rd, 2008 at 04:24 PM.
-
Oct 27th, 2008, 06:15 PM
#6
Member
Re: .NET IsAnyAdapterConnected API to Mobile
Well, I've tried this out on an iPaq 111 running Windows CE... and I can tell you for sure--no go.
(Darn shame too, I'm *attempting!* to write some mobile apps of my own... needed the help.)
_____________________________
. Error Code 34: There is no error .
-
Oct 29th, 2008, 04:14 AM
#7
Re: .NET IsAnyAdapterConnected API to Mobile
Well it works perfect on the emulator.So, what can i say?
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|