Indarnav
asked on
amend code.
wrote code for dll that checks status of netwrok adapter and disable it. but it checks for one adapter only. suppose pc has more than one.. kindly suggest what to add so that it checks all available adapter and then disable them.
// HardDisable.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include <setupapi.h>
#include <devguid.h>
#pragma comment (lib, "Setupapi.lib")
extern "C" bool StateChange(DWORD NewState, DWORD SelectedItem,HDEVINFO hDevInfo)
{
SP_PROPCHANGE_PARAMS PropChangeParams = {sizeof(SP_CLASSINSTALL_HEADER)};
SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
//
// Get a handle to the Selected Item.
//
if (!SetupDiEnumDeviceInfo(hDevInfo,SelectedItem,&DeviceInfoData))
return FALSE;
//
// Set the PropChangeParams structure.
//
PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
PropChangeParams.Scope = DICS_FLAG_GLOBAL;
PropChangeParams.StateChange = NewState;
if (!SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData,(SP_CLASSINSTALL_HEADER *)&PropChangeParams,
sizeof(PropChangeParams)))
return FALSE;
//
// Call the ClassInstaller and perform the change.
//
if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &DeviceInfoData))
return FALSE;
return TRUE;
}
extern "C" bool DisableNetAdapter()
{
HDEVINFO hdi;
bool res;
// get a list of all devices of class 'GUID_DEVCLASS_NET'
//LPGUID GUID_DEVCLASS_NET = new GUID();
//GUID_DEVCLASS_NET->Data1 = (unsigned)"4d36e974-e325-11ce-bfc1-08002be10318";
hdi = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT);
if (hdi == INVALID_HANDLE_VALUE)
return FALSE;
res = StateChange(DICS_DISABLE, 0, hdi);
// release the device info list
SetupDiDestroyDeviceInfoList(hdi);
return res;
}
ASKER
i made the changes,,pls confirm..
// HardDisable.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include <setupapi.h>
#include <devguid.h>
#pragma comment (lib, "Setupapi.lib")
extern "C" bool StateChange(DWORD NewState, DWORD SelectedItem,HDEVINFO hDevInfo)
{
SP_PROPCHANGE_PARAMS PropChangeParams = {sizeof(SP_CLASSINSTALL_HEADER)};
SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
//
// Get a handle to the Selected Item.
//
if (!SetupDiEnumDeviceInfo(hDevInfo,SelectedItem,&DeviceInfoData))
return FALSE;
//
// Set the PropChangeParams structure.
//
PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
PropChangeParams.Scope = DICS_FLAG_GLOBAL;
PropChangeParams.StateChange = NewState;
while (!SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData,(SP_CLASSINSTALL_HEADER *)&PropChangeParams,
sizeof(PropChangeParams)))
//return FALSE;
{
//
// Call the ClassInstaller and perform the change.
//
if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &DeviceInfoData))
return FALSE;
return TRUE;
}
extern "C" bool DisableNetAdapter()
{
HDEVINFO hdi;
bool res;
// get a list of all devices of class 'GUID_DEVCLASS_NET'
//LPGUID GUID_DEVCLASS_NET = new GUID();
//GUID_DEVCLASS_NET->Data1 = (unsigned)"4d36e974-e325-11ce-bfc1-08002be10318";
hdi = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT);
if (hdi == INVALID_HANDLE_VALUE)
return FALSE;
res = StateChange(DICS_DISABLE, 0, hdi);
// release the device info list
SetupDiDestroyDeviceInfoList(hdi);
return res;
}
>>>> while (!SetupDiSetClassInstallPa rams
No. Remove the ! . The function returns TRUE while it has devices and FALSE at end or error. You could call GetLastError to verify that last is an error like NO_MORE_DATA (check winerror.h for error codes).
No. Remove the ! . The function returns TRUE while it has devices and FALSE at end or error. You could call GetLastError to verify that last is an error like NO_MORE_DATA (check winerror.h for error codes).
ASKER
return true is written below
if (!SetupDiCallClassInstalle r(DIF_PROP ERTYCHANGE , hDevInfo, &DeviceInfoData))
has to be commented..?? and it will be replaced by getlasterror ??
if (!SetupDiCallClassInstalle
has to be commented..?? and it will be replaced by getlasterror ??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes jkr is right.
I mixed up the two if statements in your StateChange function by copy-pasting the wrong if statment. The while loop must be done for the enumeration (and not for the installing).
I mixed up the two if statements in your StateChange function by copy-pasting the wrong if statment. The while loop must be done for the enumeration (and not for the installing).
ASKER
ok, let me try and come back to you
ASKER
u have confused the whole, just confirm the belwo now..
have i to replace below if statement to while...??only.
if (!SetupDiEnumDeviceInfo(hD evInfo,Sel ectedItem, &DeviceInf oData))
have i to replace below if statement to while...??only.
if (!SetupDiEnumDeviceInfo(hD
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok, it is working fine now. thanks
>>>> return FALSE;
You would need to call that in a loop until all devices were returned. E. g. turn the if to a while like
if (!SetupDiEnumDeviceInfo(hD
return FALSE;
//
// Set the PropChangeParams structure.
//
PropChangeParams.ClassInst
PropChangeParams.Scope = DICS_FLAG_GLOBAL;
PropChangeParams.StateChan
while (SetupDiSetClassInstallPar
sizeof(PropChangeParams)))
{
//
// Call the ClassInstaller and perform the change.
//
if (!SetupDiCallClassInstalle
return FALSE;
}