完美实现笔记本在有线网卡连通时自动关闭wifi,有线网卡未连通时自动开启wifi
2017-11-15 21:12
429 查看
参考1:
https://www.raymond.cc/blog/auto-disable-wireless-when-lan-connected/2/>
对其中提到的第四种方法比较感兴趣,绿色环保,可自己设置,且通过事件触发计划任务感觉耗资源比较少吧.
但是通过netsh.exe开关无线网卡会弹出黑窗口. 考虑 到 win+x (mblctr.exe) 开关无线的方式感觉比较舒服,于是搜索了一下相关资料,发现他实际调用的
WlanSetInterface + wlan_intf_opcode_radio_state 实现了wifi
的开关.
参考2:
http://blog.csdn.net/lincyang/article/details/36179875
wif开关程序完整代码如下:
根据参考1第四中方法创建计划任务,调用上述开关程序.Ok,任务完成.
以下是截取参考1中的第四种方法:
4. Create a Scheduled Task to Auto Switch
Using this method relies on your computer’s LAN adapter sending an event to the Windows
Event Viewer when it disconnects and reconnects. With this information you can then create tasks to disable or re-enable the Wifi adapter depending on the connection status of the wired connection. This does not work on Windows XP because it doesn’t have
the option to trigger a task when a specific event is logged.
1. Go to Control Panel -> Administrative Tools -> Event Viewer. When Event Viewer is open, go to Windows Logs -> System.
2. Disconnect and reconnect your Ethernet cable a few times to generate some events. Press F5 to refresh the window and click on the newest events, hopefully you will find a “Port is up”, “link up” or similar description in the lower General tab to indicate
the LAN has connected, and another one when the LAN was disconnected.
3. Make a note of the name in the Source column and the Event ID for the connect and disconnect events you found. On our Dell laptop we got a connect Event ID of 121 and disconnect of 83 with the Source name of “yukonw7”, yours will likely be different though.
If you can only find a Source event for Services Control Manager and not one that looks like the LAN adapter name, this method will not work because an event needs to be generated by the Ethernet adapter itself.
4. Go to Control Panel -> Administrative Tools -> Task Scheduler, then click on Task Scheduler Library -> Create Basic Task.
5. Give the Task a name, such as “Undock Laptop” or “Switch to Wifi” etc. Click Next, select “When a specific event is logged”, and click Next again.
6. In the Log drop down select System, find the source name from step 3 in the drop down list or type it in, then enter the Event ID for the disconnect event. Click Next, make sure Start a program is selected, click Next again.
7. In the Program/script box enter C:\Windows\System32\Netsh.exe or browse for it. Enter the following into the Add arguments box:
interface set interface “Wireless Network Connection” enable
This is assuming your WiFi connection is called “Wireless Network Connection”. To check go to Control Panel -> Network and Sharing Center -> Change adapter settings, and change the name if it’s different. Click Next, review the task making sure it’s correct,
then click Finish.
8. Now you have a task that enables the WiFi on a LAN disconnect, you obviously have to do the opposite to disable the WiFi on LAN reconnect. Repeat steps 4 – 7 again, but change the name in step 5 to “Dock Laptop” or “Switch to LAN” etc, enter the connect
Event ID in step 6, and on the argument line in step 7, swap the word enable for disable.
Open the Network Connections window and you should notice the wireless connection disable itself when the Ethernet cable is inserted and vice versa. This worked flawlessly on our Dell laptops, but didn’t work on an older MSI laptop because it wouldn’t generate
a specific event on connecting/disconnecting the Ethernet cable.
Read More: https://www.raymond.cc/blog/auto-disable-wireless-when-lan-connected/2/
https://www.raymond.cc/blog/auto-disable-wireless-when-lan-connected/2/>
对其中提到的第四种方法比较感兴趣,绿色环保,可自己设置,且通过事件触发计划任务感觉耗资源比较少吧.
但是通过netsh.exe开关无线网卡会弹出黑窗口. 考虑 到 win+x (mblctr.exe) 开关无线的方式感觉比较舒服,于是搜索了一下相关资料,发现他实际调用的
WlanSetInterface + wlan_intf_opcode_radio_state 实现了wifi
的开关.
参考2:
http://blog.csdn.net/lincyang/article/details/36179875
wif开关程序完整代码如下:
#include <windows.h> #include <stdio.h> #include <shellapi.h> #include <wlanapi.h> // Need to link with shell32.lib #pragma comment(lib, "shell32.lib") #pragma comment(lib, "wlanapi.lib") BOOL wifiOnOff(int on) { DWORD dwResult = 0; DWORD dwMaxClient = 2; DWORD dwCurVersion = 0; HANDLE hClient = NULL; PWLAN_INTERFACE_INFO_LIST pIfList = NULL; PWLAN_INTERFACE_INFO pIfInfo = NULL; dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient); if (dwResult != ERROR_SUCCESS) { wprintf(L"WlanOpenHandle failed with error: %u\n", dwResult); return false; } dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList); if (dwResult != ERROR_SUCCESS) { wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult); return false; } WLAN_PHY_RADIO_STATE state; state.dwPhyIndex = 0; if (on) state.dot11SoftwareRadioState = dot11_radio_state_on; else state.dot11SoftwareRadioState = dot11_radio_state_off; PVOID pData = &state; dwResult = WlanSetInterface(hClient, &pIfList->InterfaceInfo[0].InterfaceGuid, wlan_intf_opcode_radio_state, sizeof(WLAN_PHY_RADIO_STATE), pData, NULL); if (dwResult == ERROR_SUCCESS) { wprintf(L"set state success!\n"); } else { wprintf(L"set state failed!err is %d\n", dwResult); } } int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine,//命令行参数 1开wifi,0关wifi _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); int p = 0; swscanf_s(lpCmdLine, L"%d", &p); wifiOnOff(p); return 0; }
根据参考1第四中方法创建计划任务,调用上述开关程序.Ok,任务完成.
以下是截取参考1中的第四种方法:
4. Create a Scheduled Task to Auto Switch
Using this method relies on your computer’s LAN adapter sending an event to the Windows
Event Viewer when it disconnects and reconnects. With this information you can then create tasks to disable or re-enable the Wifi adapter depending on the connection status of the wired connection. This does not work on Windows XP because it doesn’t have
the option to trigger a task when a specific event is logged.
1. Go to Control Panel -> Administrative Tools -> Event Viewer. When Event Viewer is open, go to Windows Logs -> System.
2. Disconnect and reconnect your Ethernet cable a few times to generate some events. Press F5 to refresh the window and click on the newest events, hopefully you will find a “Port is up”, “link up” or similar description in the lower General tab to indicate
the LAN has connected, and another one when the LAN was disconnected.
3. Make a note of the name in the Source column and the Event ID for the connect and disconnect events you found. On our Dell laptop we got a connect Event ID of 121 and disconnect of 83 with the Source name of “yukonw7”, yours will likely be different though.
If you can only find a Source event for Services Control Manager and not one that looks like the LAN adapter name, this method will not work because an event needs to be generated by the Ethernet adapter itself.
4. Go to Control Panel -> Administrative Tools -> Task Scheduler, then click on Task Scheduler Library -> Create Basic Task.
5. Give the Task a name, such as “Undock Laptop” or “Switch to Wifi” etc. Click Next, select “When a specific event is logged”, and click Next again.
6. In the Log drop down select System, find the source name from step 3 in the drop down list or type it in, then enter the Event ID for the disconnect event. Click Next, make sure Start a program is selected, click Next again.
7. In the Program/script box enter C:\Windows\System32\Netsh.exe or browse for it. Enter the following into the Add arguments box:
interface set interface “Wireless Network Connection” enable
This is assuming your WiFi connection is called “Wireless Network Connection”. To check go to Control Panel -> Network and Sharing Center -> Change adapter settings, and change the name if it’s different. Click Next, review the task making sure it’s correct,
then click Finish.
8. Now you have a task that enables the WiFi on a LAN disconnect, you obviously have to do the opposite to disable the WiFi on LAN reconnect. Repeat steps 4 – 7 again, but change the name in step 5 to “Dock Laptop” or “Switch to LAN” etc, enter the connect
Event ID in step 6, and on the argument line in step 7, swap the word enable for disable.
Open the Network Connections window and you should notice the wireless connection disable itself when the Ethernet cable is inserted and vice versa. This worked flawlessly on our Dell laptops, but didn’t work on an older MSI laptop because it wouldn’t generate
a specific event on connecting/disconnecting the Ethernet cable.
Read More: https://www.raymond.cc/blog/auto-disable-wireless-when-lan-connected/2/
相关文章推荐
- 怎样用笔记本的无线网卡做一个AP 实现笔记本做wifi热点供手机、平板电脑wifi上网而不用无线路由器
- 系统杂类----笔记本wifi 无线功能开启以及关闭
- openwrt 下调试实现wifi 开启/关闭及设置指示灯
- Centos 7开启网卡打开DHCP自动获取IP关闭防火墙
- 如何实现网站的自动开启和关闭(IIS)
- 在spring MVC和hibernate项目中如何实现hibernate的session在请求发起时开启数据渲染后自动关闭
- 奥运期间如何实现网站的自动开启和关闭(IIS) 推荐
- 昭阳 E43A 的笔记本电脑 开启或者关闭笔记本自带无线网卡
- Android 开启或关闭WIFI实现
- Windows 7 虚拟无线网卡(microserof virtual wifi miniport adapter)功能的开启和关闭
- java static block与runtimehook实现的Wiremock服务器的自动开启与关闭
- C#实现一次只能打开一个客户端以及关闭自动重启的问题
- Android相机实时自动对焦的完美实现
- C# 实现Smart Device WIFI 的打开/关闭
- Ubuntu 12.04 or 14.04 下USB存储设备自动挂载和自动打开功能的开启和关闭
- Mobile WiFi的开启和关闭代码实现
- (新增一键开启关闭程序)将win7电脑变身WiFi热点(非connectify,APwifi)一种稳定,便捷,网速好的方法,让iphone共享上网!
- 自动修改IP地址及网卡地址(批处理文件实现)
- java封装FFmpeg命令,支持原生ffmpeg全部命令,实现FFmpeg多进程处理与多线程输出控制(开启、关闭、查询),rtsp/rtmp推流、拉流
- ubuntu14.04快捷键控制笔记本触摸板的开启与关闭