⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 net.txt

📁 网卡自动启动 代码解释 需要的可以看看!
💻 TXT
字号:
    
  #include     <SetupAPI.h>       
  #include     <cfgmgr32.h>                 //         cfgmgr32.h     在Microsoft     Windows     2000     DDK     中.       
                                                                                                    //         要用     CM_Get_DevNode_Status(...)     来查询状态.       
  //---------------------------------------------------------------------------       
  typedef     struct     NetCardStruct       
  {       
                DWORD     Id;                                             //     网卡设备号       
                AnsiString     Name;                 //     网卡名       
                bool     Disabled;                         //     当前是否禁用       
                bool     Changed;                             //     是否更改过       
  }     TNetCardStruct;       
  typedef     TNetCardStruct*     PNetCardStruct;       
  //---------------------------------------------------------------------------       
  //             EnumNetCards     枚举出网卡       
  //---------------------------------------------------------------------------       
  void     __fastcall     EnumNetCards(TList     *NetDeviceList)       
  {       
                AnsiString     DevValue;       
                PNetCardStruct     NetCard;       
                DWORD     Status,     Problem;       
                LPTSTR     Buffer     =     NULL;       
                DWORD     BufSize     =     0;       
                HDEVINFO     hDevInfo     =     0;       
                        if     (INVALID_HANDLE_VALUE     ==     (hDevInfo     =       
                                                                SetupDiGetClassDevs(NULL,NULL,0,DIGCF_PRESENT     &brvbar;DIGCF_ALLCLASSES)))       
                                return;       
                SP_DEVINFO_DATA     DeviceInfoData     =     {sizeof(SP_DEVINFO_DATA)};       
                        HKEY     hKeyClass;       
                        char     DeviceName[200];       
                        for     (DWORD     DeviceId=0;       
                                                SetupDiEnumDeviceInfo(hDevInfo,DeviceId,&DeviceInfoData);     DeviceId++)       
                {       
                                if     (CM_Get_DevNode_Status(&Status,     &Problem,       
                                                                                DeviceInfoData.DevInst,0)     !=     CR_SUCCESS)       
                                            continue;       
      
                                DevValue.SetLength(0);       
                                if     (GetRegistryProperty(hDevInfo,     &DeviceInfoData,     SPDRP_CLASS     ,     &Buffer,     (PULONG)&BufSize))       
                                                DevValue     =     Buffer;       
                                if     (DevValue     ==     "Net")       
                                {       
                                                DevValue.SetLength(0);       
                                                if     (GetRegistryProperty(hDevInfo,     &DeviceInfoData,     SPDRP_ENUMERATOR_NAME     ,     &Buffer,     (PULONG)&BufSize))       
                                                                DevValue     =     Buffer;       
                                                if     (DevValue     !=     "ROOT")       
                                                {       
                                                                NetCard     =     new     TNetCardStruct;       
                                                                NetCard->Id     =     DeviceId;       
                                                                NetCard->Name     =     "<Unknown     Device>";       
                                                                if     (GetRegistryProperty(hDevInfo,     &DeviceInfoData,     SPDRP_DRIVER     ,     &Buffer,     (PULONG)&BufSize))       
                                                                if     (GetRegistryProperty(hDevInfo,     &DeviceInfoData,     SPDRP_DEVICEDESC     ,     &Buffer,     (PULONG)&BufSize))       
                                                                                NetCard->Name     =     Buffer;       
                                                                NetCard->Disabled     =     (Status     &     DN_HAS_PROBLEM)     &&     (CM_PROB_DISABLED     ==     Problem);       
                                                                NetCard->Changed     =     false;       
                                                                NetDeviceList->Add(NetCard);       
                                                }       
                                }       
                }       
  }       
  //---------------------------------------------------------------------------       
  bool     __fastcall     GetRegistryProperty(HDEVINFO     DeviceInfoSet,       
                                                    PSP_DEVINFO_DATA     DeviceInfoData,     ULONG     Property,     PVOID     Buffer,     PULONG     Length)       
  {       
                        while     (!SetupDiGetDeviceRegistryProperty(DeviceInfoSet,       
                                DeviceInfoData,     Property,     NULL,     (BYTE     *)*(TCHAR     **)Buffer,     *Length,     Length))       
                {       
                                if     (GetLastError()     ==     ERROR_INSUFFICIENT_BUFFER)       
                                {       
                                                if     (*(LPTSTR     *)Buffer)     LocalFree(*(LPTSTR     *)Buffer);       
                                                *(LPTSTR     *)Buffer     =     (PCHAR)LocalAlloc(LPTR,*Length);       
                                }       
                                else     return     false;       
                }       
                return     (*(LPTSTR     *)Buffer)[0];       
  }       
  //---------------------------------------------------------------------------       
  //             NetCardStateChange     网卡的启用与禁用       
  //                                                         NetCardPoint     是     PNetCardStruct     的指针.       
  //                                                         Enabled             true     =     启用             false     =     禁用       
  //---------------------------------------------------------------------------       
  bool     __fastcall     NetCardStateChange(void     *     NetCardPoint,     bool     Enabled)       
  {       
                PNetCardStruct     NetCard     =     (PNetCardStruct)NetCardPoint;       
                DWORD     DeviceId     =     NetCard->Id;       
                HDEVINFO     hDevInfo     =     0;       
                        if     (INVALID_HANDLE_VALUE     ==     (hDevInfo     =       
                                                                SetupDiGetClassDevs(NULL,NULL,0,DIGCF_PRESENT     &brvbar;DIGCF_ALLCLASSES)))       
                                return     false;       
                        SP_DEVINFO_DATA     DeviceInfoData     =     {sizeof(SP_DEVINFO_DATA)};       
                DWORD     Status,     Problem;       
                if     (!SetupDiEnumDeviceInfo(hDevInfo,DeviceId,&DeviceInfoData))       
                                return     false;       
      
                if     (CM_Get_DevNode_Status(&Status,     &Problem,       
                                                                DeviceInfoData.DevInst,0)     !=     CR_SUCCESS)       
                                return     false;       
      
                SP_PROPCHANGE_PARAMS     PropChangeParams     =     {sizeof(SP_CLASSINSTALL_HEADER)};       
                PropChangeParams.ClassInstallHeader.InstallFunction     =     DIF_PROPERTYCHANGE;       
                PropChangeParams.Scope     =     DICS_FLAG_GLOBAL;       
                if     (Enabled)       
                {       
                                if     (!((Status     &     DN_HAS_PROBLEM)     &&     (CM_PROB_DISABLED     ==     Problem)))       
                                {       
                                                NetCard->Disabled     =     false;       
                                                return     false;       
                                }       
                                PropChangeParams.StateChange     =     DICS_ENABLE;       
                }       
                else       
                {       
                                if     ((Status     &     DN_HAS_PROBLEM)     &&     (CM_PROB_DISABLED     ==     Problem))       
                                {       
                                                NetCard->Disabled     =     true;       
                                                return     false;       
                                }       
                                if     (!((Status     &     DN_DISABLEABLE)     &&     (CM_PROB_HARDWARE_DISABLED     !=     Problem)))       
                                                return     false;       
                                PropChangeParams.StateChange     =     DICS_DISABLE;       
                }       
      
                if     (!SetupDiSetClassInstallParams(hDevInfo,     &DeviceInfoData,       
                                (SP_CLASSINSTALL_HEADER     *)&PropChangeParams,     sizeof(PropChangeParams)))       
                                return     false;       
                if     (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,     hDevInfo,     &DeviceInfoData))       
                                return     false;       
                if     (CM_Get_DevNode_Status(&Status,     &Problem,       
                                                                DeviceInfoData.DevInst,0)     ==     CR_SUCCESS)       
                                NetCard->Disabled     =     (Status     &     DN_HAS_PROBLEM)     &&     (CM_PROB_DISABLED     ==     Problem);       
                return     true;       
  }       
  //---------------------------------------------------------------------------         

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -