Return Value:
- 0: Connected
- 1: Disconnected
- -1: Not available
Source Code:
int QueryNetworkAdapterStatus(string guidName) { string str = "\\\\.\\"; str += guidName;HANDLE m_hNetworkCardHandle = ::CreateFileA( str.c_str(), 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, INVALID_HANDLE_VALUE );if (m_hNetworkCardHandle == INVALID_HANDLE_VALUE) return -1;ULONG ConnectedState = 0; ULONG BytesWritten = 0; ULONG OidCode = OID_GEN_MEDIA_CONNECT_STATUS;if (0 != ::DeviceIoControl(m_hNetworkCardHandle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &OidCode, sizeof(OidCode), &ConnectedState, sizeof(ConnectedState), &BytesWritten, NULL)) { ::CloseHandle(m_hNetworkCardHandle); return ConnectedState; } else { ::CloseHandle(m_hNetworkCardHandle); return -1; } } |