Link to home
Start Free TrialLog in
Avatar of Indarnav
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; 
}

Open in new window

Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

>>>>    if (!SetupDiEnumDeviceInfo(hDevInfo,SelectedItem,&DeviceInfoData))
>>>>          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(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)))
    {
     //
     // Call the ClassInstaller and perform the change.
     //
     if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &DeviceInfoData))
        return FALSE;
    }
Avatar of Indarnav
Indarnav

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; 
} 
 

Open in new window

>>>> while (!SetupDiSetClassInstallParams

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).
return true is written below

if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &DeviceInfoData))

has to be commented..?? and it will be replaced by getlasterror ??
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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).
ok, let me try and come back to you
u have confused the whole, just confirm the belwo now..

have  i to replace below if statement to while...??only.

if (!SetupDiEnumDeviceInfo(hDevInfo,SelectedItem,&DeviceInfoData))
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok, it is working fine now. thanks