Reputation: 1627
I need to check in my Compact Framework app if it has internet connection.
So looking around I found the InternetGetConnectedState
method, but in my case every time I check if I have internet connection it returns true, even when I am offline.
Here is the code:
[DllImport("wininet.dll", CharSet = CharSet.Auto)]
private extern static bool InternetGetConnectedState(ref InternetConnectionState_e lpdwFlags, int dwReserved);
[Flags]enum InternetConnectionState_e : int
{
INTERNET_CONNECTION_MODEM = 0x01,
INTERNET_CONNECTION_LAN = 0x02,
INTERNET_CONNECTION_PROXY = 0x04,
INTERNET_RAS_INSTALLED = 0x10,
INTERNET_CONNECTION_OFFLINE = 0x20,
INTERNET_CONNECTION_CONFIGURED = 0x40
}
public Form1()
{
InitializeComponent();
verify();
}
private void verify()
{
// In function for checking internet
InternetConnectionState_e flags = 0;
bool isConnected = InternetGetConnectedState(ref flags, 0);
textBox1.Text = "Con: " + isConnected.ToString();
textBox1.Text += "\r\nDescr: " + flags.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
verify();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
I am doing something wrong?
Upvotes: 4
Views: 14124
Reputation:
It looks like your P/Invoke internetgetconnectedstate signature might be a little off.
Copying from the link above, the signature appears to look for an int
as the first parameter:
[DllImport("wininet.dll", SetLastError=true)]
extern static bool InternetGetConnectedState( out int lpdwFlags Description, int dwReserved );
[Flags]
enum ConnectionStates
{
Modem = 0x1,
LAN = 0x2,
Proxy = 0x4,
RasInstalled = 0x10,
Offline = 0x20,
Configured = 0x40,
}
The example code usage appears simple enough:
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication2
{
internal class Program
{
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetGetConnectedState(out int lpdwFlags, int dwReserved);
private static void Main(string[] args)
{
int flags;
bool isConnected = InternetGetConnectedState(out flags, 0);
Console.WriteLine(string.Format("Is connected :{0} Flags:{1}", isConnected, flags));
}
}
}
What errors are you getting? You could always wrap the InternetGetConnectedState
method in a try...catch
block:
private static void Main(string[] args)
{
int flags;
bool isConnected = false;
try
{
isConnected = InternetGetConnectedState(out flags, 0);
} catch (Exception err)
{
Console.WriteLine(err.Message);
}
Console.WriteLine(string.Format("Is connected :{0} Flags:{1}", isConnected, flags));
}
}
Hope that helps.
UPDATE Based on your flags = 18
results:
flags
is defined as an int
, so 18 is a Decimal representation.
In Binary, this would be 0001 0010
, which would map to LAN (0x2) | RasInstalled (0x10)
.
So, I'm guessing you are connected via a Local Area Network, but it does not guarantee the LAN has Internet Access. To test that, you would need to try to browse to a known good site.
RAS I am not sure about. Remote Access Service?
Keep in mind: A device cradled/docked in the charger could register as Connected
, too.
Upvotes: 4