P/Invoke Internet not working correctly?

Apr 29 2009 5:19 PM
Hello,

I'm working with the compact framework and needed to use P/Invoke functionality for finding whether or not the mobile device is connected to the internet. The connection status always seems to appear as if it is connected (through a LAN) even though the wireless is turned off and has no connection at all. Do you know if the status is cached anywhere or what I'm doing wrong?

Heres the code I have:

[DllImport("wininet.dll", CharSet = CharSet.Auto)]
private extern static bool InternetGetConnectedState(ref InternetConnectionState lpdwFlags, int dwReserved);

[Flags]
enum InternetConnectionState: int
{
INTERNET_CONNECTION_MODEM = 0x01,
INTERNET_CONNECTION_LAN = 0x02,
INTERNET_CONNECTION_PROXY = 0x04,
INTERNET_CONNECTION_RAS_INSTALLED = 0x10,
INTERNET_CONNECTION_OFFLINE = 0x20,
INTERNET_CONNECTION_CONFIGURED = 0x40
}

InternetConnectionState flags = 0;

private void btnConnection_Click(object sender, EventArgs e)
{
bool isConnected = InternetGetConnectedState(ref flags, 0);

bool isConfigured = (flags & InternetConnectionState.INTERNET_CONNECTION_CONFIGURED) != 0;
bool isOffline = (flags & InternetConnectionState.INTERNET_CONNECTION_OFFLINE) != 0;
bool isConnectedUsingModem = (flags & InternetConnectionState.INTERNET_CONNECTION_MODEM) != 0;
bool isConnectedUsingLAN = (flags & InternetConnectionState.INTERNET_CONNECTION_LAN) != 0;
bool isProxyUsed = (flags & InternetConnectionState.INTERNET_CONNECTION_PROXY) != 0;

listBox2.Items.Add(isConnected);
listBox2.Items.Add(isConfigured);
listBox2.Items.Add(isOffline);
listBox2.Items.Add(isConnectedUsingModem);
listBox2.Items.Add(isConnectedUsingLAN);
listBox2.Items.Add(isProxyUsed);
}

and the listbox2 returns:

True
False
False
False
True
False

Best Regards,

McKenzie.