Create Command to run and give an input to Windows Application - c

I want to make a Console Application for Windows Embedded Compact which will take an integer array input from User and that array will be passed as an argument to one of the function in the Application.SO,basically I want to make a Command to execute that application and also the command will contain the Integer array which the user want to pass to the Application. My Programming language is C.
I don't know how to make personalized command to be run from command prompt.
Any help or link for this will be greatly Appreciated.
Thank You.

This is where you start !!!!!!!!!!!!!!!!!
They're called Command Line Arguments
basically you pass it to main() like so:
int main(int argc, char *argv[]){
// argc is atleast 1 (contains the no. of arguments passed)
// argv[0] contains the image name and path
// argv[1] = first argument, argv[2] = second argument and so on..
}

Related

Trying to get an asterisk * as input to main from command line

I'm trying to send input from the command line to my main function. The input is then sent to the functions checkNum etc.
int main(int argc, char *argv[])
{
int x = checkNum(argv[1]);
int y = checkNum(argv[3]);
int o = checkOP(argv[2]);
…
}
It is supposed to be a calculator so for example in the command line when I write:
program.exe 4 + 2
and it will give me the answer 6 (code for this is not included).
The problem is when I want to multiply and I type for example
program.exe 3 * 4
It seems like it creates a pointer (or something, not quite sure) instead of giving me the char pointer to the char '*'.
The question is can I get the input '*' to behave the same way as when I type '+'?
Edit: Writing "*" in the command line works. Is there a way where I only need to type *?
The code is running on Windows, which seems to be part of the problem.
As #JohnBollinger wrote in the comments, you should use
/path/to/program 3 '*' 4
the way it's written at the moment.
But some explanation is clearly required. This is because the shell will parse the command line before passing it to your program. * will expand to any file in the directory (UNIX) or something similar (windows), space separated. This is not what you need. You cannot fix it within your program as it will be too late. (On UNIX you can ensure you are in an empty directory but that probably doesn't help).
Another way around this is to quote the entire argument (and rewrite you program appropriately), i.e.
/path/to/program '3 * 4'
in which case you would need to use strtok_r or strsep to step through the (single) argument passed, separating it on the space(s).
How the shell handles the command-line arguments is outside the scope and control of your program. There is nothing you can put in the program to tell the shell to avoid performing any of its normal command-handling behavior.
I suggest, however, that instead of relying on the shell for word splitting, you make your program expect the whole expression as a single argument, and for it to parse the expression. That will not relieve you of the need for quotes, but it will make the resulting commands look more natural:
program.exe 3+4
program.exe "3 + 4"
program.exe "4*5"
That will also help if you expand your program to handle more complex expressions, such as those containing parentheses (which are also significant to the shell).
You can turn off the shell globbing if you don't want to use single quote (') or double quote (").
Do
# set -o noglob
or
# set -f
(both are equivalent).
to turn off the shell globbing. Now, the shell won't expand any globs, including *.

Passing/Using Terminal Arguments with C

So the purpose of my program is to simulate a chat- one text file contains responses (call it r.txt) and I write my messages to another (call it m.txt). What I'm looking to do is write code for it in a c file using xcode, then call the program in my command terminal (I'm using Mac OSX). My question is- how does one pass multiple arguments to a C program using the terminal?
I see that in main theres 2 variables, int argc and const char* argv[]. So then does C use the array to account for multiple command line arguments? Cause essentially I'd do something like "$(name of the program), file_name_1, file_name_2." How would I reference these in my C file?
The main function is: int main(int argc, const char *argv[]).
The first one argc is the number of elements in the array argv. The first element argv[0] is the name of the program. After that you have the strings of each given parameters.
The command line (shell) separated the parameters (by default) with spaces. So myprog foo bar will result to argv[0]="myprog" argv[1]="foo" argv[2]="bar" (and here argc=3).
Several spaces are not taken in count. If you parameters contain spaces you have to use quotes (i.e. myprog "arg with spaces" other "many if wanted".

Why doesn't getopt() get the first argument when process is started from another process?

When I start a process from another process using CreateProcess, I am unable to get the first argument that was supplied when using getopt().
Just found an hack into it. getopt() parses the first command as the application name. When a process is started from another process using CreateProcess() the first argument received in the child process is not the application name but the first command that was given to it.
Ordinary case from terminal:
.app arg1 arg2. In this case argv[0] is app, argv[1] is arg1....
Starting from process using CreateProcess():
The application name and arguments are provide separately to the function CreateProcess().
Assuming the argument string is "arg1 arg2" trying to access argv[0] gives arg1 other than the application name. After parsing the options using getopt(), trying to get the first argument(argv[optind]) we get arg2. This points out that getopt() ignores the first argument since it is usually the app name. To be able to obtain the first command add any stupid argument before the actual arguments.
e.g. Argument string: "stupid arg1 arg2". This way getopt() easily thinks that stupid is the application name.

how to check if there's a string in the command line

I have this command line:> write_strings "Hello World!" a.txt b.txt dir/a.txt.
all the elements (command, string, file names) go into an array of char pointer. how can I take an element and check if it's a string or a file name?
I don't mean the exact code lines, buts just need the idea. the program should return an error if there's no string.
You can use an API such as stat or access to check if the file pointed to by a path exists. There is no fundamental distinction between filepaths and regular strings when they are passed to your process.
If you're using the standard main(int argc, char *argv[]) convention, you can loop through argv, checking each one to see if it's a file via one of the previously-mentioned system calls.
Every string that can be passed on a command line is a potential pathname, since the only restriction in both cases is that there can't be any NULs.
A program with a command line syntax in which a specific argument might or might not be used as a pathname (depending on some vague definition of "filename-ish strings" or even a file existence test) is a bad design. Each argument should have a meaning defined by its order in the argument list, or by being associated with an option like -m msg or -o outputfile.
A well-behaved unix program will let the user create a file called Hello world! if he wants.
not regarding how meaningful the program might or might not be - you can compare the single characters of your char *argv[] by looping through them via argv[i][j]. If every string includes a ".txt" you do not have a string, which is not a filename in your context

Way to differentiate between cmd line args and drag-onto-icon args in Windows?

I have a Windows GUI app written in C (MinGW) and would like to have the app perform different tasks depending on whether it was launched via the command line with a filename argument or by dragging a file onto the application icon. The way it is now, the following function doesn't differentiate between the two:
int argc;
LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(),&argc);
When a file is dragged onto the application's icon, it assumes it was launched via the command line. The problem with this is that I need additional arguments that must be passed via command line in order to do anything useful. The filename itself is not enough, so the app just quits because it doesn't have enough information to proceed.
What I would like is for the user to be able to drag a file onto the app icon, and have a window come up asking for the required options. If the user launches the app via command line with the required options already supplied, the app would immediately start processing without asking for additional input. Is this possible?
Another issue I am having is that sometimes when a file is dragged onto the app's icon, it crashes. I narrowed it down to anything operating on the argv[] values. It doesn't do this if launched via command line with the same argument. For example, this will crash the app about 20% of the time:
fprintf(stderr,"argv[3] was %ls\n",(LPWSTR)argv[3]);
Why would this only happen when launching via drag-n-drop? I am on Windows 7 x64.
Windows will by default call your program with the file name of the file you dropped on the its icon as the first and only argument. So you will get exactly the same invocation parameters in this case that you get when somebody starts your program from the command line with the full qualified name of the same file as the one and only argument.
You can however add additional arguments to a shortcut icon when you install your application i.e. "path\myapp.exe -gui". That allows you to differentiate between invocations via the icon in general (also applies to double clicking without any parameter) and invocations on the command line where the -gui parameter will usually not be specified.
It's certainly possible. Let's say the user must specify -slow or -fast on the command line. Your code then looks something like:
int main( int argc, char *argv[] ) {
if ( argv contains "-slow" or -"fast" ) {
we were launched fronm the command line
else
we were either launched from an icon, or the user has
not specified -slow or -fast. In either case, pop up
a dialog to get the options
endif
}
I don't think you can, when you drag a file over an icon, the OS executes the program using the file name as argument in the command line, so they are effectively the same.

Resources