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.
continued...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




Reply With Quote