January 26, 2023, 09:05:55 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Enable/Disable network adapter

Started by sapero, May 26, 2007, 04:17:05 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

sapero

May 26, 2007, 04:17:05 PM Last Edit: May 26, 2007, 04:31:06 PM by sapero
I've searched long time for this. I always had an example in the DDK, but never found it. It is located in \src\setup\devcon\ :)
Note: this example will work on Windows XP, but on win98 will generate exception while copying return value from SetupDiGetClassDevsEx (invalid ESP)

#define UNICODE
#include "windows.inc"
#include "setupapi.inc"

sub main()
{
// enumerate adapters
ShutDownNetworkAdapter(0, 0);
string strBuf;

// choose one, probably the first one
print();
print("select adapter to shut down (index or name) >",);
strBuf = readln();
int index = StrToNum(strBuf);

BOOL ok = false;
if (index)
{
ok = ShutDownNetworkAdapter(index+0, true); // false: power it up
}
else
{
ok = ShutDownNetworkAdapter(s2w(strBuf), true); // false: power it up
}

if (ok)
print("succeeded !")
else
print("failed !");

system("pause");
return 0;
}


// wszAdapterName - name of the adapter to shut down, or one based index (integer).
// If zero, this function will only enumerate adapters
// off - if true, adapter will be powered off, else on.

sub ShutDownNetworkAdapter(wstring *wszAdapterName, BOOL off),BOOL
{
BOOL ok = false;
HDEVINFO  DeviceInfoSet;

while (1)
{
DeviceInfoSet = SetupDiCreateDeviceInfoList(NULL, NULL);
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
break;

// create network interfaces class guid
CLSID network;
// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}
CLSIDFromString(L"{4D36E972-E325-11CE-BFC1-08002BE10318}", &network);

DeviceInfoSet = SetupDiGetClassDevsEx(&network, NULL, NULL, DIGCF_PRESENT, DeviceInfoSet, NULL, NULL);
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
break;

dwstring wszBuffer[MAX_PATH];
SP_DEVINFO_DATA DeviceInfoData;
DeviceInfoData.cbSize = sizeof(DeviceInfoData);
int   index = 0;
DWORD type, cb;

while (SetupDiEnumDeviceInfo(DeviceInfoSet, index, &DeviceInfoData))
{
index++;
// query adapter name
if (SetupDiGetDeviceRegistryProperty(DeviceInfoSet, &DeviceInfoData, SPDRP_DEVICEDESC, &type, wszBuffer, MAX_PATH*2, &cb))
{
BOOL matched = false;
if ((wszAdapterName > 0) && (wszAdapterName < 65536))
{
// compare by index
matched = (wszAdapterName == index);
}
else if (wszAdapterName > 65535)
{
// compare strings
matched = !wcscmp(wszBuffer, wszAdapterName);
}
if (matched)
{
SP_PROPCHANGE_PARAMS params;

params.ClassInstallHeader.cbSize          = sizeof(params.ClassInstallHeader);
params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;

params.StateChange = DICS_ENABLE + (off != 0);

params.Scope       = DICS_FLAG_CONFIGSPECIFIC;
params.HwProfile   = 0;

if (SetupDiSetClassInstallParams(DeviceInfoSet, &DeviceInfoData, &params, sizeof(params))
&& SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, DeviceInfoSet, &DeviceInfoData))
{
ok = true;
break;
}
}
else
{
// display the name
print(using("### &", index, w2s(wszBuffer)));
}
}
}
break;
}
if (DeviceInfoSet != INVALID_HANDLE_VALUE)
SetupDiDestroyDeviceInfoList(DeviceInfoSet);

return ok;
}

Techno

Separo,

I can't compiled it in Aurora

Error report :

Compiling...
Enum Network Adapters.src
Unable to Open - windows.inc
Unable to Open - setupapi.inc
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (32) Warning: undeclared function 'system'
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (44) unknown type
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (48) Warning: undeclared function 'SetupDiCreateDeviceInfoList'
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (50) undefined variable - INVALID_HANDLE_VALUE
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (50) invalid argument to IF statement
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (53) unknown type
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (55) Warning: undeclared function 'CLSIDFromString'
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (55) undefined variable - network
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (55) Invalid & operation
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (57) Warning: undeclared function 'SetupDiGetClassDevsEx'
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (57) undefined variable - DIGCF_PRESENT
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (57) undefined variable - network
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (57) Invalid & operation
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (59) undefined variable - INVALID_HANDLE_VALUE
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (59) invalid argument to IF statement
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (62) unknown type
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (63) undefined variable - DeviceInfoData
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (63) invalid use of dot operator, unknown type
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (68) Warning: undeclared function 'SetupDiEnumDeviceInfo' - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (68) undefined variable - DeviceInfoData - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (68) Invalid & operation - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (72) Warning: undeclared function 'SetupDiGetDeviceRegistryProperty' - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (72) illegal operand - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (72) undefined variable - SPDRP_DEVICEDESC - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (72) undefined variable - DeviceInfoData - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (72) Invalid & operation - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (82) Warning: undeclared function 'wcscmp'
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (86) unknown type
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (88) undefined variable - params
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (88) invalid use of dot operator, unknown type
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (88) invalid use of dot operator, unknown type
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (89) undefined variable - params
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (89) invalid use of dot operator, unknown type
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (89) invalid use of dot operator, unknown type
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (91) undefined variable - params
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (91) invalid use of dot operator, unknown type
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (93) undefined variable - params
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (93) invalid use of dot operator, unknown type
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (94) undefined variable - params
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (94) invalid use of dot operator, unknown type
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (98) Warning: undeclared function 'SetupDiSetClassInstallParams' - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (98) undefined variable - params - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (98) undefined variable - params - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (98) variable expected - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (98) unknown identifier - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (98) undefined variable - params - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (98) Invalid & operation - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (98) undefined variable - DeviceInfoData - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (98) Invalid & operation - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (98) Warning: undeclared function 'SetupDiCallClassInstaller' - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (98) undefined variable - DeviceInfoData - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (98) Invalid & operation - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (98) undefined variable - DIF_PROPERTYCHANGE - {
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (113) undefined variable - INVALID_HANDLE_VALUE
File: C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src (113) invalid argument to IF statement
Error(s) in compiling "C:\CPanel\Cheetah\Cheetah\cheatSeek\Enum Network Adapters.src"



sapero

Hi Techno, this is a common problem, the IDE nonstop changes its current directory if you use any open/save dialog. This current dir is inherited by the parser so it cannot find the correct files.
Just try to re-save your source code using "save as" from main menu, or restart the IDE. It compiles fine!
Another option is to change the default current directory for the IDE in shortcut (to adev.exe). I've set it to /include directory and have less errors.