在windows下利用程序获得网卡状态,(启用,禁止等)。我先谢谢大家了

解决方案 »

  1.   

    这是完整的可运行程序。#include <windows.h>  
    #include <stdio.h>
    #include <tchar.h>    // Make program ansi AND unicode safe
    extern "C"
    {
    #include <setupapi.h> // Used for SetupDiXxx functions
    #include <devguid.h>
    #include <cfgmgr32.h> // Used for CM_Xxxx functions
    }#pragma comment (lib,"setupapi")
    #pragma comment (lib,"cfgmgr32")
    HDEVINFO hDevInfo = 0;// 设备是否处于禁用状态
    BOOL IsDisabled( PSP_DEVINFO_DATA pDevInfoData, HDEVINFO hDevInfo )
    {
        DWORD Status, Problem;

        if (CR_SUCCESS != CM_Get_DevNode_Status(&Status, &Problem, pDevInfoData->DevInst, 0))
        {
            _tprintf("Get_DevNode_Status() failed.\r\n");
            return FALSE;
        }

        return ((Status & DN_HAS_PROBLEM) && (CM_PROB_DISABLED == Problem)) ;
    }int main(int argc, char* argv[])
    {
    hDevInfo = SetupDiGetClassDevs(
    (LPGUID)&GUID_DEVCLASS_NET,
    NULL, 
    0, 
    DIGCF_PRESENT);

    BOOL bRet = FALSE;
    char DeviceName[255];
    SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)}; for (DWORD i=0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
        {
    // 得到设备名
    if ( SetupDiGetDeviceRegistryProperty(hDevInfo, 
    &DeviceInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE) DeviceName, MAX_PATH, NULL) )
    {
    printf("DeviceName = %s\n", DeviceName);
    } bRet = IsDisabled(&DeviceInfoData, hDevInfo);
    printf("Device State = %s\r\n", bRet ? "Disable" : "Enable");

    printf("\r\n");
    }

    getchar();

    return 0;
    }
      

  2.   

    确实如此,但Windows系统是只和网卡设备直接相联的。