Solved

amend code.

Posted on 2009-04-06
10
370 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:Indarnav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24075061
>>>>    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;
    }
0
 

Author Comment

by:Indarnav
ID: 24075185
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

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24076177
>>>> 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).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Indarnav
ID: 24076283
return true is written below

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

has to be commented..?? and it will be replaced by getlasterror ??
0
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 24078884
'SetupDiCallClassInstaller()' is the wrong place for that loop, you want to use 'SetupDiEnumDeviceInfo()' for that - just as in http://support.microsoft.com/kb/259695 ("How to enumerate hardware devices by using SetupDi calls"). E.g.
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)}; 
 
       DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
       for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
           &DeviceInfoData);i++)
       {
   // 
    // 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; 
} 

Open in new window

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24079755
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).
0
 

Author Comment

by:Indarnav
ID: 24083438
ok, let me try and come back to you
0
 

Author Comment

by:Indarnav
ID: 24114429
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))
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 200 total points
ID: 24114586
>>>> u have confused the whole, just confirm the belwo now..
Sorry. Below should be the correct code.

It is a while with a positive check on the enumeration and within the loop a negative check on the installing.

The for loop you used should be ok as well though I didn't see a definition of the loop counter i.
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. 
    // 
    while (SetupDiEnumDeviceInfo(hDevInfo,SelectedItem,&DeviceInfoData)) 
    {
     // 
     // 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; 
} 

Open in new window

0
 

Author Comment

by:Indarnav
ID: 24114855
ok, it is working fine now. thanks
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
is twain_32.dll cmpatible with windows 10 ? 10 232
Dynamically allocate memory 9 70
max float value 3 59
Why  my code (program) build with old compiler? 11 108
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question