Control cmd prompt using C - c

I'm trying to write a C program to control the windows cmd prompt. For example, opening the cmd prompt, go to a specific directory (ex: C:/Program Files/...), and then run an exe in that folder.
Can this be done with C programming? If so, how? So far, I am only aware of system("cmd.exe") to open up the cmd prompt. How would I further interact with cmd prompt?
Thanks.

This wouldn't be very portable. system calls are often frowned upon, but just to answer your question, the system function does work with the commands you're aiming to use.
For example:
system("notepad.exe my_file.txt");
system("del my_file.txt");
system("pause");
This will open up a file called my_file.txt in notepad, delete it and pause the program.
Again, this is not portable. It's specific to Windows Operating systems.
In fact, I don't even think it's guaranteed to work on all releases of Windows.
(Don't quote me on that.)

On topic: You could start cmd via "CreateProcess" and send key input via window messages ("SendMessage").
I think you should rethink how you want things to be done. The command prompt is not a kind of API base to do things on windows. It's a tool to do things and get information without writing your own program. If you wirte an own program, you should directly use the WinAPI.
To get started you can google "winapi [whatever you want to do]". In your example "winapi start executable" and you will find functions like "CreateProcess" and "ShellExecute".

Perhaps there is a misunderstanding here (if so, I apologize), but the standard way to "further interact with cmd prompt" is via command-line commands, and the standard way "to write a program to control the windows cmd prompt" is via a Batch file. For example, the following Batch file open the cmd prompt (when it is executed via a double click in the Explorer), go to a specific directory (C:/Program Files/) and run an exe in that folder:
#echo off
cd "C:/Program Files/"
nameOfTheProgram.exe

Related

How to start c app from desktop with double-click?

I have written one very simple app in C. I know how to run it through Visual studio, but i want to start it from desktop with a double-click. If I double-click on a .exe file made in folder, it does not start. How can I do this?
If you do not wish to modify your code so that it will wait for user input before closing (and there are many reasons that might be inappropriate), you could create a batch file wrapper:
myprog.bat
myprog.exe
pause
Then either you can double click the batch file or create a shortcut to the batch file then edit the shortcut to set the path to the batch file and the path from which to run (so it can find the exe).
That pretty much emulates how Visual Studio runs your code without terminating the window.
Another method you might consider is a batch file such as:
runner.bat
%1
pause
Then you can drag-and-drop your executable onto the runner.bat icon to run it. The advantage being that you don't have to create a new batch file and/or shortcut for every new executable.
Really though this is not a C question, or even a programming question - it is most likely off topic. If your code is ever required to run to completion unattended in a batch file for example, you would not necessarily want to add any interactivity to the program itself.
Your problem is that double clicking on a console app will open a new console window, run the program and then close the window.
VS studio does a trick where it runs the app in a new console window but keeps it open till you press a key.
You can add that same thing yourself - put a getchar() call at the end
Or you can make a bat file to run the app as per Cliffords answer
1st open the code with visual studio code
Run or Build the program
then u will find an executable file where u have saved your code
Open that executable file
but you must have installed mingw installed in your environment
you must compile it first with mingw or like compiler. and start it with by code:
cmd> yourdirectory(e.g Desktop)/ gcc yourcodefile.c your question is available also internet => how-to-compile-c-program

Journalctl to .txt file from C program

I have a application written in C and i want to control writing of syslogs in txt file from application. On application there is option for START/STOP running, so basically when i press START i want to start writing syslog(journalctl) to /some_folder/debug.txt, and when i press STOP i want to close the file.
This can be done by linux command "journalctl [parameter] > /some_folder/debug.txt", but that is not enough, this must be controlled from application.
Does anyone have idea how this can be done properly, is there some API to control syslogs or i have to do it via system()?
If there is no way, if i have to use system() and journalctl commands are there some suggestion which commands to use?

run cygwin command through batch file

I am running a rsync ssh command in cygwin in windows. Upon this, password is asked and if given, it does the intended task. Now I want to put these all tasks in one batch file, which can be run on one click.
Thanks
Step 1: Set up an authorized_keys file on the server so that you can ssh in from your local machine without using a password (check the security implications of this carefully). You can find instructions for this in many places, including the official documentation.
Step 2: Add Cygwin's bin directory to your Windows PATH environment variable. If you don't want to do it permanently, you could write that into your batch file.
Step 3: Write your one-line batch file using the exact same rsync command line you used in Cygwin. Pathnames should not need translating unless you expect the shell to expand a wild card. If you use any sort of quoting then that might need adjusting.
Step 4: Configure Windows to run programs with one click, instead of double click. Not sure how you do that; I don't use Windows much.

How to launch gvim.exe from cscope-win32 when run from cmd.exe?

For years now my Windows C tagging/scoping solution has almost worked. I can build the filelist, build the tags (via Exuberant Ctags) for vim/gvim navigation, build cscope.out, and tag around within gvim.exe windows I launch by clicking on C source files.
The final piece that eludes me is that I can't seem to get cscope in cmd.exe to launch my editor when I select an item. I made sure gvim.exe is in my Path environment variable. I made sure the CSCOPE_EDITOR environment variable is set (more on that below). But when I select a line item from within cscope the editor is not launched. Instead I get one of the two following failures:
If CSCOPE_EDITOR is either gvim.exe -f or "gvim.exe -f" then I see that printed at the top of the cmd.exe window followed by the line offset and the filename (e.g. "gvim.exe -f +72 myfile.c") and then it quickly returns to cscope without the editor ever popping.
If CSCOPE_EDITOR is gvim.exe (without the -f option) then a gvim.exe process is kicked off (I see it in Windows Task Manager under Processes) but it never comes to the foreground as an application. Furthermore, the cscope window in cmd.exe goes blank and is unresponsive until I manually kill the gvim.exe process that was spawned.
For reference:
cscope -V returns "cscope: version 15.8a" and was downloaded from this site: http://code.google.com/p/cscope-win32/
My version of gvim.exe is 7.3 which the latest available from http://www.vim.org/
Also, I have tagged this post with "C" because, while this is not a C language question, cscope and ctags are primarily used for C programming and thus I think the C tag is relevant. (Thought I'd point that out before someone comes along and removes the tag and says this isn't a C question, since C programmers are the most likely people to have the answer.)
With help from #mattn and #mMontu and a lot of trial-and-error and an ugly but simple hack I finally have this working. Here are the problems I encountered:
cscope-win32 does not handle spaces in the EDITOR or CSCOPE_EDITOR environment variables. I tried every space-escape trick I could think of (single-quotes, double-quotes, backslash escapes) and nothing worked.
When gvim.exe is successfully launched by cscope-win32, if that gvim.exe tries to add the same cscope.out database (via cs add cscope.out) the add command hangs. If the add is part of a vimrc file then the editor will hang during opening (with the gvim.exe and cscope.exe processes starting but the application window never appearing). If the add is done after the editor window opens then the window will hang. This appears related to multiple cscope-win32 processes being attempted in the same process tree, but I have no actual proof of that.
Here's my solution to the problem. Like I said, it is an ugly but simple hack.
Create a wrapper batch file. I named mine gvim_cscope.bat. Make sure it is in a directory that exists in your PATH environment variable. You can verify this in a cmd.exe shell by calling "where gvim_cscope.bat".
Edit the batch file and add your editor command but precede it with the windows start command. For example, my batch file contains the following:
start gvim.exe %*
Create/Update your CSCOPE_EDITOR environment variable to be "gvim_cscope.bat". In case you've never modified a Windows environment variable before, you can get to them (on Windows 7 anyway) via Start -> right-click Computer -> Properties -> Advanced System Settings -> Environment Variables.
That's all I needed to get things working. Open a new cmd.exe window (so the updated environment variable is pulled in) and open cscope.exe and everything works. I am able to pop multiple gvim.exe windows from within cscope.exe, each with its own connection to the cscope.out database. The first problem is avoided by using the wrapper batch file (no more spaces in the command) and the second problem is avoided by using the windows start command so that gvim.exe is started as a separate process.
Thanks for the help #mattn and #mMontu. So nice to have things working right!

How to write a batch file to run applications opening in different command prompts?

I would like to run three servers on my Windows machine from command prompt. For this I would like to write a batch file to run these opening three different command prompts.
To generate 3 new CMD windows that remain open, use the following command syntax:
start "application1" cmd.exe /k dir *.exe
start "application2" cmd.exe /k dir *.xml
start "application3" cmd.exe /k dir *.bat
Where the command after /k is whatever application command line that you want to launch.
Replace the "application#" with some TITLE for the window, that makes sense for you (otherwise it will just say something like C:\Windows\System32\CMD.exe).
The windows will stay open, but that doesn't mean your application will still be running. Like the Dir examples above, it may end but the window will remain.
The full command syntax for START is found here.
The full command syntax for CMD is found here.
Why batch?
If you can, my best suggestion would be to ditch batch and download cygwin in order to use bash instead.
You will find bash much more flexible, used (which means you are far more likely to get support from others), and supported.
I am a windows user and tried to learn batch some time ago, but it was really really frustrating. bash is so much more user friendly.
This should do what you want
start application1
start application2
start application3
This will start 3 applications which will open with 3 different command prompts, whether or not they will keep running or not will depend on the app.

Resources