函數InternetGetConnectedState返回本地系統的網路連接狀態。
文法:
BOOL InternetGetConnectedState(__out LPDWORD lpdwFlags,__in DWORD dwReserved); 參數:
lpdwFlags[out]指向一個變數,該變數接收串連描述內容。該參數在函數返回FLASE時仍可以返回一個有效標記。該參數可以為下列值的一個或多個。
值 |
含義 |
INTERNET_CONNECTION_CONFIGURED0x40 |
Local system has a valid connection to the Internet, but it might or might not be currently connected. |
INTERNET_CONNECTION_LAN 0x02 |
Local system uses a local area network to connect to the Internet. |
INTERNET_CONNECTION_MODEM0x01 |
Local system uses a modem to connect to the Internet. |
INTERNET_CONNECTION_MODEM_BUSY0x08 |
No longer used. |
INTERNET_CONNECTION_OFFLINE 0x20 |
Local system is in offline mode. |
INTERNET_CONNECTION_PROXY0x04 |
Local system uses a proxy server to connect to the Internet. |
INTERNET_RAS_INSTALLED0x10 |
Local system has RAS installed. |
dwReserved[in]保留值。必須為0。 傳回值:當存在一個modem或一個LAN串連時,返回TRUE,當不存在internet串連或所有的串連當前未被啟用時,返回false。當該函數返回false時,程式可以調用
GetLastError來接收錯誤碼。 樣本:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Runtime.InteropServices;namespace ConsoleApplication1{ class Program { private const int INTERNET_CONNECTION_MODEM = 1; private const int INTERNET_CONNECTION_LAN = 2; private const int INTERNET_CONNECTION_PROXY = 4; private const int INTERNET_CONNECTION_MODEM_BUSY = 8; [DllImport("winInet.dll ")] //聲明外部的函數: private static extern bool InternetGetConnectedState( ref int Flag, int dwReserved ); static void Main(string[] args) { int Flag = 0; string netStates = ""; if (!InternetGetConnectedState(ref Flag, 0)) { Console.WriteLine("no!"); } else { if ((Flag & INTERNET_CONNECTION_MODEM) != 0) netStates += " Connect by MODEM /n"; if ((Flag & INTERNET_CONNECTION_LAN) != 0) netStates += "Connect by LAN /n"; if ((Flag & INTERNET_CONNECTION_PROXY) != 0) netStates += "Connect by PROXY /n"; if ((Flag & INTERNET_CONNECTION_MODEM_BUSY) != 0) netStates += " MODEM is busy /n"; } Console.WriteLine(netStates); Console.ReadLine(); } }}
: