How to run a batch file in order to shutdown PC from C++ service on a Windows 10 machine?

耗尽温柔 提交于 2021-01-20 13:45:20

问题


I have a C++ program that runs as a service on a 64-bit Windows 10 machine. In this program, I want to execute a batch file to remotely switch off Pcs. I tried to use the function system() as follows :

system("cmd.exe /C \"batchfile path"");
system("shutdown /s /f /t 15 /m IPAdress"); 

In the second case, nothing happens. And in the first case, when I put a shutdown in my batch file, nothing happens however when I put a line like :

echo Test >> D:\test\toto.txt

The line is well executed meaning that I can find the file toto.txt in the filepath indicated.

Finally I tried with the command ShellExecuteW() as follows but without any sucess (nothing happens):

ShellExecuteW(NULL, L"runas", L"cmd.exe", L"/S /C \" \"batchfile path"", NULL, 0);

Now I don't know what to do in order to shutdown remote Pcs from my C++ programm.

Can anyone help me please ?

Thanks in advance.

来源:https://stackoverflow.com/questions/65560755/how-to-run-a-batch-file-in-order-to-shutdown-pc-from-c-service-on-a-windows-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!