By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,839 Members | 1,635 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,839 IT Pros & Developers. It's quick & easy.

Enable network adapter and BindAdapter exceptions.

P: n/a
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:
<adapterNameREG_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[i].Name;

if (adapterName == wiFiAdapterName)
{
this.adapter = ac[i];
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);
}


May 9 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.