-2

The Batch file code are as following:

:continue
start myfile.exe 
tasklist /FI "IMAGENAME eq myfile.exe" 2>NUL | find /I /N "myfile.exe">NUL
if "%ERRORLEVEL%"=="0" goto :exit
goto :continue
exit:

I tried to write in c and compiled it.

include < stdlib.h >
int main()
{
  system(":continue
start myfile.exe 
tasklist /FI "IMAGENAME eq myfile.exe" 2>NUL | find /I /N "myfile.exe">NUL
if "%ERRORLEVEL%"=="0" goto :exit
goto :continue
:exit");
  return 0;
}

But it is not working. What is going wrong?

Compo
  • 36,585
  • 5
  • 27
  • 39
rohit7373
  • 5
  • 4
  • 4
    "_It is not working_" is the worst error description I can think of. – Uwe Keim Dec 28 '17 at 08:30
  • Your [multiline C string](https://stackoverflow.com/a/797351/107625) seems to be invalid. – Uwe Keim Dec 28 '17 at 08:31
  • no , only single code is working : #include using namespace std; int main() { system("system.exe "); return 0; } – rohit7373 Dec 28 '17 at 08:48
  • Why would you wrap batch in C??? It is almost straight forward rewrite your code in C, instead of wrapping. Regarding "obfuscation", compiling system calls with string arguments will for sure store string data in your compiled .exe which can easily be seen with a `type` command. – LS_ᴅᴇᴠ Dec 28 '17 at 09:12
  • OK SIR. but I don't know HOW to do that. my main purpose is that: Check myfile.exe file is running or not.,if running then exit and if not running then run myfile.exe. sound very simple but one here help me with code. can you SIr please. – rohit7373 Dec 28 '17 at 09:21
  • Yeah... you're going to have to learn C. You can't just paste batch into a C file and call it ported. You have to actually write the equivalent C code. – SomethingDark Dec 28 '17 at 12:19

1 Answers1

2

The system() function is to used to run individual binaries. Your batch script is not an individual binary - it is a list of Batch Command Processor instructions.

The easiest solution I can see is to save your batch script into a .BAT file and then use the system command like this:

system("C:\Windows\cmd.exe /s C:\path\to\your\file.bat");
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
JBRWilkinson
  • 4,821
  • 1
  • 24
  • 36