Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Is P/Invoke InternetGetConnectedState cached?

McKenzieMcKenzie Posts: 1Member
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.
Sign In or Register to comment.