472,993 Members | 3,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 software developers and data experts.

Enable network adapter and BindAdapter exceptions.

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
0 4232

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: genetic.error | last post by:
I installed VS.NET 2003 on Windows 2000 Pro SP3. Since doing so, I have no internet connection. Also, I have no access to my home network. I've done everything I can think of to correct the...
3
by: hitchiker | last post by:
hello i have a little difficult problem searchengines could not help me yet i have 2 zigbee (standard for wireless personal area networking, such as bluetooth) cards, connected to the pc's via...
1
by: Misha | last post by:
How do i get the name of the network card in C#?
1
by: Sushrut | last post by:
Hi, Is there a way to get the netcfginstanceid of a network adapter thru C#? I couldnt find any available classes that might do the trick. The netcfginstanceid is available in the registry key...
0
by: 4AspNet | last post by:
Hello. I need to disable and then enable Local Network Connection (Ethernet). Here is my code: IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();...
8
by: rongchaua | last post by:
Hi all, i would like now to disable and enable network adapter programmatically with c#. I have searched but found nothing useful. There's no topic about this problem. Has someone done with this...
11
by: weird0 | last post by:
I am a beginner to socket programming to in c#. I know the concepts as to how to program them. I get the above exception when i tried writing the code for the server side on the line...
4
by: =?Utf-8?B?TWFydGlu?= | last post by:
In Device Manager under Network Adapters, nearly all (about 20) items have a yellow warning symbol. The first item, Microsoft 6to4 Adapter says driver is up-to-date BUT says "This device is not...
0
by: Eran.Yasso | last post by:
Hi all, The following code sets/disables network adpter's status. Since I have no idea where to put this code and I wish to share the community with it. I did rverse engineering from VBS to C#....
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.