Note: The question was edited and they tagged c++ for batch. But the purpose of the question is an answer in c++. At the time of this reply the tag was in batch, and a solution via batch. I’ll leave you the answer as it may help future visitors.
Reply to Batch
The simplest way in Windows to turn off only the Internet, is to proceed with the removal of gateway pattern.
Disconnect Internet
Following code to collect information from gateway and store it in a text file, then delete it:
@echo off
cd /d "%~dp0"
REM recolher a gateway atual
set dest=0.0.0.0
for /f "tokens=2,3" %%A in ('"route print %dest% | findstr /c:"%dest%" "') do (
REM guardar o IP e eliminar a gateway
echo %%A %%B>%dest%.txt
route delete %dest% >nul
)
exit /b
Internet
Since we kept the information from gateway in a text file, we can connect the Internet to gateway with the definitions of the same at the moment we remove it:
@echo off
cd /d "%~dp0"
REM garantir que o ficheiro com as definições existe
set dest=0.0.0.0
if not exist %dest%.txt exit /b 2
REM restaurar a gateway
for /f "tokens=1,2" %%A in (%dest%.txt) do (route add %dest% mask %%A %%B >nul)
exit /b
Solution credits for the user @and31415 in this reply in their.
So you’re disconnecting the network intrerface, not "the internet". If the same interface is used for local network, the local network is also unavailable.
– Bacco
I know, in my case there is no problem the local network be disabled ok ?
– axell-brendow
But if you want to give two examples, one disconnecting only the Internet and the other disconnecting the Internet and the local network would be nice.
– axell-brendow
@Lucasvirgili is good to pay attention before editing, because you changed the tags and compromised the question. In the meantime an excellent answer came out, only based on the wrong language. Some time from now I remove the comment.
– Bacco
An uncomplicated way is to use the function
system
orCreateProcess
simply calling the programs you use in bat, netsh, etc.– pepper_chico