시스템

네트워크 연결상태 확인

네오류이 2021. 1. 10. 13:08
728x90
반응형

오늘은 C++ 개발에 있어 네트워크 연결 상태를 확인할 때 사용하는 함수 에 대해 설명합니다.

 

아주 간단한 함수 하나만 있으면 확인할 수 있습니다.

 

함수명은 InternetGetConnectedStateEx 입니다.

 

* 소스코드

 

DWORD CDlg::NetworkStatus()

{

DWORD dwFlag;

    TCHAR szName[256];

    return ::InternetGetConnectedStateEx(&dwFlag, szName, 256, 0);

}

 

 

위 함수를 보면 특별한 인자 없이 InternetGetConnectedStateEx 을 던지게 되면 응답값이 true 이면 연결된 상태입니다.

 

응답되는 인자에 여려상태값이 있는데 이 값을 MSDN에서 보면 아래와 같습니다.

 

etrieves the connected state of the specified Internet connection.

Syntax

C++

 

BOOL InternetGetConnectedStateEx( _Out_ LPDWORD lpdwFlags, _Out_ LPTSTR  lpszConnectionName, _In_  DWORD   dwNameLen, _In_  DWORD   dwReserved );

Parameters

lpdwFlags [out]

Pointer to a variable that receives the connection description. This parameter may return a valid flag even when the function returns FALSE. This parameter can be a combination of the following values.

ValueMeaning

INTERNET_CONNECTION_CONFIGURED0x40

Local system has a valid connection to the Internet, but it might or might not be currently connected.

INTERNET_CONNECTION_LAN0x02

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_OFFLINE0x20

Local system is in offline mode.

INTERNET_CONNECTION_PROXY0x04

Local system uses a proxy server to connect to the Internet.

 

lpszConnectionName [out]

Pointer to a string value that receives the connection name.

dwNameLen [in]

Size of the lpszConnectionName string, in TCHARs.

dwReserved [in]

This parameter is reserved and must be NULL.

Return value

Returns TRUE if there is an Internet connection, or FALSE if there is no Internet connection, or if all possible Internet connections are not currently active. For more information, see the Remarks section.

When InternetGetConnectedState returns FALSE, the application can call GetLastError to retrieve the error code.

Remarks

A return value of TRUE from InternetGetConnectedState indicates that at least one connection to the Internet is available. It does not guarantee that a connection to a specific host can be established. Applications should always check for errors returned from API calls that connect to a server.InternetCheckConnection can be called to determine if a connection to a specific destination can be established.

A return value of TRUE indicates that either the modem connection is active, or a LAN connection is active and a proxy is properly configured for the LAN. A return value of FALSE indicates that neither the modem nor the LAN is connected. If FALSE is returned, the INTERNET_CONNECTION_CONFIGURED flag may be set to indicate that autodial is configured to "always dial" but is not currently active. If autodial is not configured, the function returns FALSE.

Like all other aspects of the WinINet API, this function cannot be safely called from within DllMain or the constructors and destructors of global objects.

Note  WinINet does not support server implementations. In addition, it should not be used from a service. For server implementations or services use Microsoft Windows HTTP Services (WinHTTP).

 

Requirements

Minimum supported clientMinimum supported serverHeaderLibraryDLLUnicode and ANSI names

Windows 2000 Professional [desktop apps only]

Windows 2000 Server [desktop apps only]

Wininet.h
Wininet.lib
Wininet.dll

InternetGetConnectedStateExW (Unicode) andInternetGetConnectedStateExA (ANSI)

보통 상세한 인자가 필요할 때도 있지만.. 저 같은 경우는 연결상태만 확인하면 되니 응답값으로만 처리를 하였습니다.

 

728x90
반응형