vc 禁用无线网卡 禁用网卡 禁用有线网卡

首先和普通的方法一样,根据GUID禁用网卡类设备,只是在禁用时枚举第i块网卡,读取其设备信息,读到有"wireless"这个串的就认为它是无线网卡,就对它使用禁用操作,否则就接着枚举第i+1块网卡,直到都枚举完. 
下面给出重要结构. 
// driveDemoDlg.cpp : implementation file 
#include "stdafx.h" 
#include "driveDemo.h" 
#include "driveDemoDlg.h" 
#include < windows.h > 
#include < wincon.h > 
#include < stdlib.h > 
#include < stdio.h > 
#include  
HDEVINFO hDevInfo = NULL; 
class CAboutDlg : public CDialog 

public: 
CAboutDlg(); 
enum { IDD = IDD_ABOUTBOX };
protected: 
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
protected: 
DECLARE_MESSAGE_MAP()}; 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{} 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{CDialog::DoDataExchange(pDX);} 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
END_MESSAGE_MAP() 
CDriveDemoDlg::CDriveDemoDlg(CWnd* pParent /*=NULL*/) 
: CDialog(CDriveDemoDlg::IDD, pParent) 
{m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);} 
void CDriveDemoDlg::DoDataExchange(CDataExchange* pDX) 
{CDialog::DoDataExchange(pDX);} 
BEGIN_MESSAGE_MAP(CDriveDemoDlg, CDialog) 
ON_WM_SYSCOMMAND() 
ON_WM_PAINT() 
ON_WM_QUERYDRAGICON() 
ON_BN_CLICKED(IDC_BUTTON_SEARIALPORT1, OnButtonSearialport1) 
ON_BN_CLICKED(IDC_BUTTON_SEARIALPORT2, OnButtonSearialport2) 
END_MESSAGE_MAP() 
// CDriveDemoDlg message handlers 
BOOL CDriveDemoDlg::OnInitDialog() 

CDialog::OnInitDialog(); 
        ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
ASSERT(IDM_ABOUTBOX < 0xF000); 

CMenu* pSysMenu = GetSystemMenu(FALSE); 
if (pSysMenu != NULL) 

CString strAboutMenu; 
strAboutMenu.LoadString(IDS_ABOUTBOX); 
if (!strAboutMenu.IsEmpty()) 

pSysMenu->AppendMenu(MF_SEPARATOR); 
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 



SetIcon(m_hIcon, TRUE); // Set big icon 
SetIcon(m_hIcon, FALSE); // Set small icon 

return TRUE;  // return TRUE  unless you set the focus to a control 


void CDriveDemoDlg::OnSysCommand(UINT nID, LPARAM lParam) 

if ((nID & 0xFFF0) == IDM_ABOUTBOX) 

CAboutDlg dlgAbout; 
dlgAbout.DoModal(); 

else 

CDialog::OnSysCommand(nID, lParam); 




void CDriveDemoDlg::OnPaint() 

if (IsIconic()) 

CPaintDC dc(this); // device context for painting 

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 

int cxIcon = GetSystemMetrics(SM_CXICON); 
int cyIcon = GetSystemMetrics(SM_CYICON); 
CRect rect; 
GetClientRect(&rect); 
int x = (rect.Width() - cxIcon + 1) / 2; 
int y = (rect.Height() - cyIcon + 1) / 2; 

dc.DrawIcon(x, y, m_hIcon); 

else 

CDialog::OnPaint(); 



HCURSOR CDriveDemoDlg::OnQueryDragIcon() 

return (HCURSOR) m_hIcon; 


void CDriveDemoDlg::OnOK() 

CDialog::OnOK(); 


void CDriveDemoDlg::FormatMSG(DWORD dwError, LPTSTR * lpszMsg) 

BOOL bOk = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM ¦ FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, dwError, 
MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), (LPTSTR)lpszMsg, 0, NULL); 

if (!bOk) 

HMODULE hDll = LoadLibraryEx(_T("netmsg.dll"), 
NULL, 
DONT_RESOLVE_DLL_REFERENCES); 
if (NULL != hDll) 

FormatMessage(FORMAT_MESSAGE_FROM_HMODULE ¦ 
FORMAT_MESSAGE_FROM_SYSTEM, 
hDll, 
dwError, 
MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), 
(LPTSTR)lpszMsg,0, NULL); 
FreeLibrary(hDll); 




//////ChangeStatus 
BOOL CDriveDemoDlg::ChangeStatus(DWORD NewStatus, DWORD SelectedItem, HDEVINFO hDevInfo) 

LPTSTR lpszMsg = NULL; 
HCURSOR hCursor = NULL; 
try 

SP_PROPCHANGE_PARAMS PropChangeParams = {sizeof(SP_CLASSINSTALL_HEADER)}; 
SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)}; 

hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT)); 

// Get a handle to the Selected Item. 
if (!SetupDiEnumDeviceInfo(hDevInfo, SelectedItem, &DeviceInfoData)) 

FormatMSG(GetLastError(), &lpszMsg); 
throw lpszMsg; 


// Set the PropChangeParams structure. 
PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE; 
PropChangeParams.Scope = DICS_FLAG_GLOBAL; 
PropChang

eParams.StateChange = NewStatus; 

if (!SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER *)&PropChangeParams, 
sizeof(PropChangeParams))) 

FormatMSG(GetLastError(), &lpszMsg); 
throw lpszMsg; 


// Call the ClassInstaller and perform the change. 
if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,hDevInfo,&DeviceInfoData)) 

FormatMSG(GetLastError(), &lpszMsg); 
throw lpszMsg; 


SetCursor(hCursor); 
return TRUE; 

catch (TCHAR * pszError) 

SetCursor(hCursor); 
::MessageBox(NULL,pszError,_T("提示"),MB_OK); 
if (NULL != lpszMsg) 

LocalFree((HLOCAL)lpszMsg); 

return FALSE; 



// This Function can stop/start the wireless. 
// Parameters description:The nStatus has three state.-1:do nothing 
// 0 :stop the wireless,1:start the wireless. 
// nIndex is the parameter to said if stop the Wireless. 
// if you want to stop the Wireless,you can call ControlDisk(0, 4). 
BOOL CDriveDemoDlg::ControlDisk(int nStatus, int nIndex) 

if (-1 == nStatus) 

return FALSE; 


LPTSTR lpszMsg = NULL; 
try 

TCHAR * GUIDString = NULL; 
GUID guid; 
ZeroMemory(&guid, sizeof(GUID)); 
GUIDString = _T("4d36e972-e325-11ce-bfc1-08002be10318"); 
UuidFromString((unsigned char *)GUIDString, &guid); 
hDevInfo = SetupDiGetClassDevs(&guid,NULL,NULL,DIGCF_PRESENT); 
if (INVALID_HANDLE_VALUE == hDevInfo) 

FormatMSG(GetLastError(), &lpszMsg); 
throw lpszMsg; 


DWORD i;int Icount=0; 
SP_DEVINFO_DATA DeviceInfoData; 
ZeroMemory(&DeviceInfoData, sizeof(SP_DEVINFO_DATA)); 
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); 

for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); ++i) 

//Get DeviceInfo            
DWORD DataT; 
LPTSTR buffer = NULL; 
DWORD buffersize = 0; 
DWORD Status, Problem; 
BOOL bDisabled = FALSE; 
BOOL bwireless=FALSE; 
            while (!SetupDiGetDeviceRegistryProperty( 
                      hDevInfo, 
                    &DeviceInfoData, 
                      SPDRP_DEVICEDESC, //Read the names of the designated card 
                    &DataT, 
                    (PBYTE)buffer, 
                    buffersize, 
                        &buffersize) 



if ( GetLastError() == ERROR_INSUFFICIENT_BUFFER ) 
{                
if (buffer) LocalFree(buffer); 
buffer=(char*)LocalAlloc(LPTR,buffersize);              

else 
{ // Insert error handling here. 
AfxMessageBox("读取网卡错误"); 



CString strEthernet = buffer ; 
char *str=strupr(buffer); 
char *str2="WIRELESS";//wireless sign 
char *p; 
                        p=strstr(str,str2);//compare 
if (p!=NULL) bwireless=TRUE;//ok,found. 
if (int i= CM_Get_DevNode_Status(&Status, &Problem, DeviceInfoData.DevInst,0) != CR_SUCCESS) 
{  FormatMSG(GetLastError(), &lpszMsg); 
throw lpszMsg; 

if (!((Status & DN_HAS_PROBLEM) && (CM_PROB_DISABLED == Problem))) 
{  bDisabled = FALSE;} 
if ((Status & DN_HAS_PROBLEM) && (CM_PROB_DISABLED == Problem)) 
{  bDisabled = TRUE; } 
if( (bDisabled==FALSE) && (0 == nStatus) && (bwireless) ) 
{ ChangeStatus(DICS_DISABLE, i, hDevInfo);// 禁用 
bwireless=FALSE; 

if( (bDisabled==TRUE) && (1 == nStatus) && (bwireless) ) 
{ ChangeStatus(DICS_ENABLE, i, hDevInfo);// 启用 
bwireless=FALSE; 



return SetupDiDestroyDeviceInfoList(hDevInfo); 

catch (TCHAR * pszError) 

::MessageBox(NULL,pszError,_T("提示"),MB_OK); 
if (NULL != lpszMsg) 

LocalFree((HLOCAL)lpszMsg); 

return FALSE; 

return FALSE; 

// 禁用 
void CDriveDemoDlg::OnButtonSearialport1()  

ControlDisk(0,4);



// 启用 
void CDriveDemoDlg::OnButtonSearialport2() 

ControlDisk(1,4);
}

你可能感兴趣的:(无线网卡)