Shutting down and Restarting PC via C++ in WSL - c++

Here is my program to shutdown the PC in c++, and I use vs code editor and WSL to run this program:
#include<iostream>
#include<stdlib.h>
int main()
{
system("C:\\Windows\\System32\\shutdown /i ");
}
I got this message sh: 1: C:WindowsSystem32shutdown: not found.

Make sure you use the appropriate path. The correct form for WSL via Linux is "/mnt/c/Windows/System32/shutdown.exe" as mentioned by prog-fh and code_fodder.
So this will work: (I haven't tested it in WSL, but the above users did and know better)
std::system("/mnt/c/Windows/System32/shutdown.exe /i");
or for shutdown you can use s as well:
std::system("/mnt/c/Windows/System32/shutdown.exe /s");
Likewise, for restarting, use r:
std::system("/mnt/c/Windows/System32/shutdown.exe /r");

Related

QT Creator Can't read from stdin in debugger

I just updated QT Creator to 4.13.0 and now I can't read from stdin while I'm in the debugger.
A few details...
I am running on macOS.
I am writing C++, but using C stdio library. (for historical reasons)
I am using clang 15.5.0 in c++11 mode.
I am using the 'Run from Terminal' debugger option.
Problem: Type as I might, when my code goes into fgets it never returns.
Simplified Example:
#include <stdio.h>
int main()
{
char buf[1024];
if (fgets(buf,1024, stdin)) // In debugger, this call never returns!
{
fprintf(stderr, "%s", buf);
}
return 0;
}
Note: When I run this outside of the debugger - no problems.
Has anyone run across this and fixed it? Any ideas on how to fix it?
Could be an issue with the terminal start application openTerminal.py inside QtCreator.
I had a similar issue, where the output of my console application appeared correctly in a terminal session when starting in debug mode without the debugger, but when starting using the debugger all system output and system error was written to Application Output widget making it impossible to read any values from std::in.
Solution was:
Manage Kits: Environment System Replace the long path ".../openTerminal.py"
by "/Applications/Utilities/Terminal.app"

"Hello world!" freeze in c++

I tried to compile a simple Hello World in C++ to test that my Visual Studio 2013 on my Windows 10 works well but I've a problem. My program compile but when I run, it freeze. It's an empty Win32 Console Application with a "main.cpp" :
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!";
return 0;
}
If I launch it from Visual Studio in debug (with "Run"), Visual Studio freeze and I need to kill its task.
If I launch it with the executable or with "Run without Debugging", nothing appear, "System" process doesn't want to free the execution and I can't recompile without restart the computer because of that.
I am confused. If somebody have an idea of what's happening, please help me.
Update 1 : I repair my installation. Same problem. Breakpoint doesn't help. It seems to freeze before. And also no trace of a task in the task manager when launched without debugging.
It was my antivirus' fault (in my case, Avast). I made it ignore my Visual Studio projects and it works. Solved by #Blastfurnace.

Confusing output with SIPp remote commands through C++'s system() on Ubuntu

I'm having a bit of trouble with understanding how the system() command is functioning on Ubuntu.
I am creating a C++ program to remotely control SIPp clients (SIPp remotely controlling clients) and have pretty much completed the program, but I'm now running into an issue when I'm testing it.
I had already tested this on my centOS VM, and it was running perfectly, but now that I'm testing it on my Ubuntu VM, it's failing to even execute the commands.
On Ubuntu's terminal, I can run echo + >/dev/udp/127.0.0.1/8888 and the SIPp client will accept the command, However, when I run my program (Which does the same thing) I get a sh: 1: cannot create /dev/udp/127.0.0.1/8888: Directory nonexistent.
#include <stdlib.h>
int main(){
system("echo + >/dev/udp/127.0.0.1/8888");
return 0;//
}
The above code works perfectly fine on centOS and the SIPp client recieves and performs the task, but on Ubuntu it's giving me the Directory nonexistent error.
From what I can see, it's currently trying to read the > as me attempting me to output a file, but escaping the > with \> just produces a compiler error.
warning: unknown escape sequence: '\>' [enabled by default]
system("echo + \>");
^
If anyone can point me in the right direction, I would be one happy camper.
So after searching around forever I realized it was because access to /dev/udp was a bash only function, and system() was interpreted by bin/sh.
Changing my system() call to system("bash -c \"echo + >/dev/udp/localhost/8888\""); solved the problem.
Had a similar Problem and found a different solution, i had to do it in a bashscript and ubuntu threw the same error. This worked for me:
echo "+" | nc -4u -w1 127.0.0.1 8888
Got it from here: http://mikeberggren.com/post/53883822425/ncudp

"windows cannot access the specified device....." error in c++

I am MFC guy working on visual studio 2010 create some executables using visual studio!! but on linux and mac my executables are not working as usual windows!!.
So i decided to use "MinGW" compiler to create executables.
Note:-Please give me one suggestion is that," is minGW is best compiler for cross plateform working ??or any thing else is there??"
I successfully install WinGW compiler on my C drive and start working with following program..
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello ";
return 0;
}
I compile it using following command,
g++ -static-libgcc -static-libstdc++ Main.cpp
I found one executable in same folder with name a.exe :).Working fine:)
But after some time i decide to modified same program in following manner like,
int main ()
{
return 0;
}
I compile it with same command but when i execute it using command line it show me error "Access is denied so i goto that folder and run same executable as "Run as Administrator" it show me one messagebox with the message windows cannot access the specified device path or file. you may not have appropriate permissions
---EDIT--
follwing code is NOT WORKING:-
int main ()
{
int k;
return 0;
}
but this program WORKING :-
int main()
{
int k;
k = 0;
return 0;
}
If you are getting this access denied error, then the most likely cause is that the executable file is open in another process, probably the linker or debugger. Try installing Process Explorer and hit Ctrl+F and type in the name of your .exe. This should show the processes that the .exe file open. Kill those processes (or if you are still debugging, then end debugging first). You then should be able to build again.
Note that this has nothing to do with Microsoft APIs, as in any case you're using gcc.
EDIT: If there are no processes holding the .exe then it may be that there is some other kind of permission problem. Does the .exe file exist? Can you delete the file and rebuild? Another thing to try is run Process Monitor and filter for the name of the .exe -- that may show a regular permission denied error, or perhaps another error such as a sharing conflict.
Note:-Please give me one suggestion is that," is minGW is best compiler for cross plateform working ??or any thing else is there??"
No. And there's nothing else out there.
Use whatever compiler is available on target platform, ensure your code compiles on all of them.
Avoid platform-specific and compiler-specific code at all costs (use cross-platform frameworks).
I successfully install WinGW
There are many different versions of mingw provided by different sites. If you install compiler from mingw.org using mingw-get, it'll probably work. If you install mingw from some other site, it may or may not work.
I compile it using following command,
Use a build systems. cmake, qmake or something similar.
it show me error "Access is denied
Launch process monitor and see after which system call it terminates. It is also possible that your antivirus software interferes with your program, or maybe there's some stray dll in your path or something like that.
Check the permissions for the entire folder in which the executable resides. Trying to 'Run as Administrator' doesn't have any effect if the folder doesn't allow the permissions.
It doesn't have anything to do with your code. This is an environmental problem, something is pretty messed up about the permissions your user account has for one or more of the directories on your hard disk. The generic diagnostic is that the default working directory for the program does not permit read or list access.
A possible starting point would be to use Explorer and right-click the directory where MinGW is installed. Use the Security tab and ensure that your user account has all permissions enabled. Further narrow it down to trying to run the program from the command prompt, using different directories as the default directory.

C++ on windows closes a program immediately after launching

I installed minGW and the eclipse CDT, and the console keeps doing something weird. The code of the program is
using namespace std;
#include <iostream>
int main() {
cout << "Hello, windows (8, c++)" << endl;
//system("PAUSE");
return 0;
}
You all know it, its the Hello World program. Now when I run this the Eclipse console displays some stuff about building, and then goes blank. And when I navigate to the HelloWorldProgram.exe in the explorer and run it, a windows flashes up and displays "hello world", but then immediately closes. When I do this on Mac OSX there's no problem, and the windows stays up until I decide to close it. Now I know there's a command
system("PAUSE") //I dont know what I need to import to use this. Could you tell me that too?
Which will give me more or less the same effect, but I'd like to know why Windows does it differently from OSX, and what I can do to fix it (bc this annoys the crap out of me).
Looking forward to your replies!
This happens on Windows because this is just the behavior of the Windows console. You'll have to open up the console manually and then running your program through the console you've opened if you don't want the window to close automatically once the program has executed.
You may want to take a look at these:
What is the Best Practice for Combating the Console Closing Issue?
https://superuser.com/questions/186562/how-can-i-keep-the-terminal-open
Don't use system("pause"), it's wrong for a multitude of reasons (read more about it here).
Put cin.get() before return and the window will stay open until you press enter.
If you want to just run your console program, you should open a console, and run it.
Apparently, the OSX version of Eclipse is configured to open a console, and run the program, and not close it. Maybe you can configure the Win version so, too.
You shouldn't meddle with your program to behave differently on another platform, instead wrap it into something that 'adapts' the behaviour.
Probably, you can tell eclipse to use "cmd /c 'yourprogram.exe && pause'", to open a command window and have it execute your program and then pause.
Just add getch(); before return, and add #include <conio.h>.