Is there a better way to lay out a command in C ++ than system ()?

I have a Windows C ++ service that sometimes crashes when I call the system () function to issue a command. I ran the exact command text on the Windows command prompt and it works fine, but for some reason it fails when system () starts.

To make matters worse, I cannot get any information about why system () is not working. It does not seem to throw an exception because I am doing a catch (...) and they will not catch anything. My service just stops working. I know this is a system call () that fails because I put the registration information before and after the call, and something after it just writes nothing.

So, is there any other way so that I can execute the command? At least something that will give me some information if things go wrong, or at least let me handle the exception or something else.

+3
source share
5 answers

I believe it system()is technically part of the C standard library and therefore will not throw exceptions. You should be able to check the return code or the ERRNO variable to get some information about what happened. This MSDN link contains some information about possible return codes in Windows.

system() fail , , .

, , .

EDIT: - , , , . , , , , system() , "C:\MARKER.TXT" - , , - .

+1

catch() (, ). . , ; , .

+1

fork/exec, , , .

0

, , .

( ), , , , .

services.msc . , . , , .

Another thing to pay attention to is the path inside the service. Use getenv ("PATH") and see if the path you are relying on is missing.

Hope this helps ...

0
source

I ended up using CreateProcess. So far it has worked.

0
source

Source: https://habr.com/ru/post/1772288/


All Articles