Enable network adapter and BindAdapter exceptions.

B

BLUE

Enable and Disable in the Network and DialUp Connections manager is actually
setting a bit in the registry for the target adapter and then calling NDIS
to unbind or bind (depending on whether your disabling or enabling the
adapter).


In HKEY_LOCAL_MACHINE\Comm\NdisPower insert this value to disable the
adapter:
<adapterName> REG_DWORD 4 (0x00000004)

Delete the value to enable the adapter


To do a NDIS bind or NDIS unbind you can use these API:

VOID NdisBindProtocolsToAdapter
(
PNDIS_STATUS pStatus,
PWSTR wszAdapterInstanceName,
PWSTR wszProtocolName
);

VOID NdisMRebindProtocolsToAdapter(NDIS_HANDLE MiniportAdapterHandle);

VOID NdisUnbindProtocolsFromAdapter
(
PNDIS_STATUS pStatus,
PWSTR wszAdapterInstanceName,
PWSTR wszProtocolName,
);


I've tried using these API but I get a NotSupportedException
So I've tried using OpenNetCF.Net Adapter class but:

- I do not understand why I get an exception on the first time I try to
bind or unbind
- I do not know how to bind the adapter if it is disabled: the only way is
modifying Adapter source code so that the Name property let me set the Name:
exploring the source I've found that Adapter class needs only the name to
call internal bind functions



using OpenNETCF.Win32;
using OpenNETCF.Net;


[DllImport("Ndis.dll", SetLastError = true)]
private static extern void NdisBindProtocolsToAdapter
(
ref int pStatus,
ref string wszAdapterInstanceName,
ref string wszProtocolName
);

[DllImport("Ndis.dll", SetLastError = true)]
private static extern void NdisUnbindProtocolsFromAdapter
(
ref int pStatus,
ref string wszAdapterInstanceName,
ref string wszProtocolName
);


// DISCOVERY ********************** BUTTON1
AdapterCollection ac = Networking.GetAdapters();

string adapterName;
this.adapter = null;
int myAdapterIndex = -1;

for (int i = 0; i < ac.Count; i++)
{
adapterName = ac.Name;

if (adapterName == wiFiAdapterName)
{
this.adapter = ac;
myAdapterIndex = i;
}
}


// DISABLE ********************** BUTTON2
RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Comm\NdisPower", true);
object obj = rk.GetValue(wiFiAdapterName);
rk.SetValue(wiFiAdapterName, 0x00000004);
rk.Flush();
rk.Close();

try
{
// Using this I get NotSupportedException
/*
int status = 0;
string name = this.adapter.Name;
string nullString = null;
NdisUnbindProtocolsFromAdapter(ref status, ref name, ref nullString);
*/

// Using this I get "DeviceIoControl( IOCTL_NDIS_UNBIND_ADAPTER )"
// exception only the first time I press the button: if I wait some
// seconds Bind and Unbind work!
this.adapter.UnbindAdapter();
}
catch(Exception exc)
{
MessageBox.Show(exc.Message);
}


// ENABLE ********************** BUTTON3
RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Comm\NdisPower", true);
object obj = rk.GetValue(wiFiAdapterName);
rk.DeleteValue(wiFiAdapterName);
rk.Flush();
rk.Close();

try
{
// Using this I get NotSupportedException
/*
int status = 0;
string name = this.adapter.Name;
string nullString = null;
NdisBindProtocolsToAdapter(ref status, ref name, ref nullString);
*/

// THIS CAN BE DONE ONLY IF DISCOVERY WAS MADE WITH ADAPTER ACTIVE
// TO AVOID THIS PROBLEM I CAN MODIFY Adapter.Name PROPERTY TO HAVE A
// SET SECTION: EXPLORING THE SOURCE CODE I FOUND ONLY THE NAME IS
// NEEDED TO CALL INTERNAL API FOR DEVICE BINDING
//
// Using this I get "DeviceIoControl( IOCTL_NDIS_BIND_ADAPTER )"
// exception only the first time I press the button: if I wait some
// seconds Bind and Unbind work!
this.adapter.BindAdapter();
}
catch(Exception exc)
{
MessageBox.Show(exc.Message);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top