passing arguments with shellexecute

chan_hic's Avatar, Join Date: Mar 2010
Newbie Member
-excuse me for my english -

i want to execute a program from an other with shellexecute and with passing multiple arguments, the problem is just the first argument is passed to the other ... what can i do ?

the frist
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{

    FILE * f;
    if(argc>1){


        f = fopen(strcat(argv[1],".txt"), "a");

        if (f != NULL)
        {
            printf("%s",argv[2]);
           fprintf(f, "%s^%s\n",argv[2],argv[3]);
            printf("qsdfs");
        }
        fclose(f);
        }
    else
        perror(argv[1]);
getch();
    return 0;
}
the second

Code:
#include <windows.h>
#include <shellapi.h>


int main ()
{

char **tab;
tab=(char**)malloc(3*sizeof(char*));
tab[0]=(char*)malloc(10*sizeof(char));
tab[1]=(char*)malloc(10*sizeof(char));
tab[2]=(char*)malloc(sizeof(char));

strcpy(tab[0],"films");
strcpy(tab[1],"the titanic");
strcpy(tab[2],"1h 45min");


ShellExecute(NULL,"open","insert.exe",tab,NULL,SW_SHOWDEFAULT);
  return 0;
}
virxen's Avatar, Join Date: Nov 2009
Pro contributor
first of all parameters are separated by space.
so,when you pass "1h 45" as parameter ,you actually pass 2 parameters
1st="1h"
2nd="45"

i changed your code a lit bit.
===================
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
int main(int argc, char **argv)
{

    FILE * f;
    for (int i=1;i<argc;i++) printf("\nparameter %d=%s",i,argv[i]);
    getchar();getchar();
    printf("\n");
    if(argc>1){


        f = fopen(strcat(argv[1],".txt"), "a");

        if (f != NULL)
        {
            printf("%s",argv[2]);
           fprintf(f, "%s^%s\n",argv[2],argv[3]);
            printf("qsdfs");
        }
        fclose(f);
        }
    else
        perror(argv[1]);
getch();
    return 0;
}
now when you run it ,the program tells you which parameters it received.

second change
==============
Code:
#include <windows.h>
#include <shellapi.h>
#include <stdio.h>


int main ()
{

char **tab;
tab=(char**)malloc(3*sizeof(char*));
tab[0]=(char*)malloc(10*sizeof(char));
tab[1]=(char*)malloc(10*sizeof(char));
tab[2]=(char*)malloc(sizeof(char));

strcpy(tab[0],"films");
strcpy(tab[1],"the_titanic");
strcpy(tab[2],"1h_45min");
char tabs[30]="\0";
strcat(tabs,tab[0]);
strcat(tabs," ");
strcat(tabs,tab[1]);
strcat(tabs," ");
strcat(tabs,tab[2]);
printf("\n%s",tabs);
getchar();
getchar();
ShellExecute(NULL,"open","insert.exe",tabs,NULL,SW_SHOWDEFAULT);
  return 0;
}
in this code i put an underscore ("_") instead of space in order to have 1 parameter instead of 2.

then i create one string with your parameters separated by space in variable tabs
which i pass to insert.exe program.
chan_hic like this