main() wrapper to start wmain() program to compile it from commandline - c

For example i have a program thats "main" function is defined as wmain.
int wmain( int argc, wchar_t *argv[] ) {
wchar_t* lpModulePath = NULL;
wchar_t* lpFunctionName = NULL;
lpModulePath = argv[1];
lpFunctionName = argv[2];
}
and of course uses wchar_t types. How can i write a function
int main( int argc, char *argv[] )
that converts the parameters passed as char to wchar_t and then calls wmain by itself?
Thank you

On Windows you can use GetCommandLineW() and CommandLineToArgvW():
int main(int argc, char* argv[])
{
wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
int ret = wmain(argc, wargv);
LocalFree(wargv);
return ret;
}
On Linux I'm afraid you'd have to allocate the array and convert strings in loop.

Related

How to call another function that utilizes argc, char *argv[] in main without passing things to it?

say there is a function in my code:
int getwords(int argc, char *argv[])
and I want to call this function in main().
How do I call this in main without erroring out?
`void main(void)
getwords();`
If you write main to provide access to argc and argv:
int main(int argv, char **argv) {
// ...
}
Then you can pass those to another function when you call it:
void foo(int argc, char **argv) {
// ...
}
int main(int argc, char **argv) {
foo(argc, argv);
}
argc will be copied, but argv is a pointer, so the pointer is copied
but not the data it points to.
if for some special reason you do not want to use parameters you can use global variables available in all program.
int gargc;
char **gargv;
int main(int argc, char *argv[])
{
gargc = argc;
gargv = argv;
getopt();
}

getting trouble with running program in c on linux

why I am not succeed
int main(char* name,int arg0,int arg1)
{
name = "/u/e2014/Desktop/os/Prog.c";
arg0 = 0;
arg1 = 1;
char my_args[3];
my_args[0] = arg0;
my_args[1] = arg1;
my_args[2] = NULL;
execl(name,m_args);
return(0);
}
I want that my program will execute the program in the path "name".
Right now its do nothing.
I am not understand where is my mistake?
I program in C on linux, and compile it with gcc
Thanks a lot!!
gcc has 3 different signature for main function
int main(void);
int main(int argc, char* argv[]);
int main(int argc, char *argv[], char *envp[]);
Your main function doesn't match either of these. therefore compiler error.
For your case you can use the 2nd signature with a small modification.
#include <stdlib.h>
int main(int argc, char **argv)
{
char *path;
int int1, int2;
path = argv[1];
int1 = atoi(argv[2]);
int2 = atoi(argv[3]);
}
First you are passing wrong parameter in int main(). main() has at least 3 args only.
int main(int argc, char*argv[], char *envp[]);
To execute your program you should use execvp() because you passing arrey of char* not command-line arguments via a variable-argument.
difference between execl and execv?
**L vs V: whether you want to pass the parameters to the exec'ed program as
L: individual parameters in the call (variable argument list): execl(), execle(), execlp(), and execlpe()
V: as an array of char* execv(), execve(), execvp(), and execvpe()**
#include <stdio.h>
#include <unistd.h>
int main(int argc,char*argv[])
{
char *name = "/root/a.out";
char *arg0 = "0";
char *arg1 = "1";
char *my_args[4];
my_args[0] = name;
my_args[1] = arg0;
my_args[2] = arg1;
my_args[3] = NULL;
execvp(my_args[0],my_args);
return(0);
}

Execute external program with specific parameters from windows c/c++ code

I want to call Program1 from Program2 with exact same parameters which I called Program2 with.
In Linux, I can do it like this:
int main(char argc, char* argv[]){
execv("./Program1", argv);
}
In windows, I tried CreateProcess
but as the first post says there is potential issue: "argv[0] Doesn't Contain the Module Name as Expected". I do want to send proper argv[0] to Program1. What should I do?
argv[0] is the name of the program itself.
You should do :
int main(char argc, char **argv)
{
char* argvForProgram1[] = { "./Program1", 0 }
execv(argvForProgram1[0], argvForProgram1);
}
or to keep your previous args :
int main(char argc, char **argv)
{
char** argvForProgram1 = argv;
argvForProgram1[0] = "./Program1";
execv(argvForProgram1[0], argvForProgram1);
}
Using execve is better too because you keep the environment:
int main(char argc, char **argv, char **envp)
{
char** argvForProgram1 = argv;
argvForProgram1[0] = "./Program1";
execve(argvForProgram1[0], argvForProgram1, envp);
}

Passing command line arguments to app written in C

Here I wrote a little app which is able to read command line arguments
int main (int argc, const char * argv[])
{
int c;
while ((c = getopt (argc, argv, "Il:o:vh?")) != -1)
{
switch(c)
{
case 'I':
printf("I");
break;
}
}
return 0;
}
The problem is that when I try to compile it the compiler prints
warning: passing argument 2 of ‘getopt’ from incompatible pointer type
and program crash.
What I miss ?
The argv argument to main should have type char *[], not const char *[] so that it can be converted to the char *const [] that getopt expects. In fact, char *[] or equivalent is mandated by the C standard for hosted implementations.
int main (int argc, const char * argv[])
should be
//no const
int main (int argc, char * argv[])

Modify struct from function?

I want to modify file in the items struct from parse_commandline().
I can without problems modify items->file from main() by using strncpy, but not from parse_commandline(). I need to modify parse_commandline() so it can recieve information about items from main(), by i don't know how?
typedef struct {
int pack_01;
int pack_02;
char file[100];
} items;
static items character_y = { 1, 1 }
parse_commandline(int argc, char *argv[])
{
/* PARSE COMMANDLINE ARGUMENTS */
}
int main(int argc, char* argv[])
{
items *ptr_items = &character_y;
parse_commandline(argc,argv);
return 0;
}
The way to do this is pass a pointer to items to the parse_commandline function and let the function update the structure based on the arguments.
parse_commandline(int argc, char *argv[], items* pItems) {
pItem->pack_01 = 42;
...
};
int main(int argc, char* argv[]) {
items items;
parse_commandline(argc, argv, &items);
...
}
Passing a structure to a function in C is no different from passing any other variable. In your case, you should do it by reference so that you can modify the caller's structure:
void parse_commandline(int argc, char *argv[], items *theItems)

Resources