Unplugged ethernet cable vs. ip address conflict

  • Thread starter Thread starter Rob Bonzer
  • Start date Start date
R

Rob Bonzer

I've searched around looking for answers but haven't found any that work
yet.

I'm trying to distinguish between having the ethernet cable unplugged vs.
having an ip address conflict. I've used GetIfTable, but it will return
MIB_IF_OPER_STATUS_NON_OPERATIONAL and have an ip address of 0.0.0.0 if its
unplugged OR address conflict. It returns MIB_IF_OPER_STATUS_OPERATIONAL if
its all working. I've never seen it return MIB_IF_OPER_STATUS_DISCONNECTED.

Other code snippets that I've seen posted here look like they just try and
see if the interface is operational and ip address is not empty. I'm not
sure that would work either.

Has anyone figured out how to do this (in C/C++ code)?

Rob
 
Rob,

Actually the MIB_IF_OPER_STATUS_DISCONNECTED return status should work.

Anyway, if it does not work, you can quiry Ndis directly though DeviceIoControl(..., IOCTL_NDIS_QUERY_GLOBAL_STATS, &oidCode, ...).
Where oidCode should be set to something like OID_GEN_MEDIA_CONNECT_STATUS (search msdn/DDK for more info:
http://msdn.microsoft.com/library/en-us/wceddk40/html/cxgrfGeneralObjectIdentifiers.asp).
Or OID_GEN_HARDWARE_STATUS or OID_GEN_MEDIA_IN_USE may help you as well.

The code may look like:
HANDLE hDevice = INVALID_HANDLE_VALUE;

// Build the path to the device
char szDevPath[MAX_ADAPTER_NAME_LENGTH + 8];
wsprintf( szDevPath, "\\\\.\\%s", &pCurAdapter->AdapterName[0] );

// Open the device for reading
hDevice = CreateFile( szDevPath, 0, FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES) NULL,
_EXISTING, 0, (HANDLE) INVALID_HANDLE_VALUE);

if (hDevice == INVALID_HANDLE_VALUE)
return false;

// Query the device for its type
NDIS_OID OidCode = OID_GEN_MEDIA_CONNECT_STATUS;
ULONG ulOidMediaConnectStatus=0;
ULONG ulBytesReturned=0;
bool bResult = (bool) DeviceIoControl( hDevice,
IOCTL_NDIS_QUERY_GLOBAL_STATS,
&OidCode,
sizeof(NDIS_OID),
&ulOidMediaConnectStatus,
sizeof(ULONG),
&ulBytesReturned, NULL );

// Close the device
CloseHandle(hDevice); // What was the result?
if (ulOidMediaConnectStatus == NdisMediaStateConnected) ; // Connected! else if (ulOidMediaConnectStatus ==
NdisMediaStateDisconnected)
; // Disconnected!
 
You are the MASTER! Many thanks. Works like a charm.

Rob


KM said:
Rob,

Actually the MIB_IF_OPER_STATUS_DISCONNECTED return status should work.

Anyway, if it does not work, you can quiry Ndis directly though
DeviceIoControl(..., IOCTL_NDIS_QUERY_GLOBAL_STATS, &oidCode, ...).
Where oidCode should be set to something like OID_GEN_MEDIA_CONNECT_STATUS
(search msdn/DDK for more info:
http://msdn.microsoft.com/library/en-us/wceddk40/html/cxgrfGeneralObjectIdentifiers.asp).
Or OID_GEN_HARDWARE_STATUS or OID_GEN_MEDIA_IN_USE may help you as well.

The code may look like:
HANDLE hDevice = INVALID_HANDLE_VALUE;

// Build the path to the device
char szDevPath[MAX_ADAPTER_NAME_LENGTH + 8];
wsprintf( szDevPath, "\\\\.\\%s", &pCurAdapter->AdapterName[0] );

// Open the device for reading
hDevice = CreateFile( szDevPath, 0, FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES) NULL,
_EXISTING, 0, (HANDLE) INVALID_HANDLE_VALUE);

if (hDevice == INVALID_HANDLE_VALUE)
return false;

// Query the device for its type
NDIS_OID OidCode = OID_GEN_MEDIA_CONNECT_STATUS;
ULONG ulOidMediaConnectStatus=0;
ULONG ulBytesReturned=0;
bool bResult = (bool) DeviceIoControl( hDevice,
IOCTL_NDIS_QUERY_GLOBAL_STATS,
&OidCode,
sizeof(NDIS_OID),
&ulOidMediaConnectStatus,
sizeof(ULONG),
&ulBytesReturned, NULL );

// Close the device
CloseHandle(hDevice); // What was the result?
if (ulOidMediaConnectStatus == NdisMediaStateConnected) ; //
Connected! else if (ulOidMediaConnectStatus ==
 
Back
Top