passing arguments with shellexecute

Discussion in 'C' started by chan_hic, Mar 7, 2010.

  1. chan_hic

    chan_hic New Member

    Joined:
    Mar 7, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    -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 :shout:?

    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;
    }
     
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    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;
        [COLOR=Red]for (int i=1;i<argc;i++) printf("\nparameter %d=%s",i,argv[i]);
        getchar();getchar();[/COLOR]
        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[COLOR=Red]_[/COLOR]titanic");
    strcpy(tab[2],"1h[COLOR=Red]_[/COLOR]45min");
    [COLOR=Red]char tabs[30]="\0";
    strcat(tabs,tab[0]);
    strcat(tabs," ");
    strcat(tabs,tab[1]);
    strcat(tabs," ");
    strcat(tabs,tab[2]);
    printf("\n%s",tabs);[/COLOR]
    [COLOR=Red]getchar();
    getchar();[/COLOR]
    ShellExecute(NULL,"open","insert.exe",[COLOR=Red]tabs,[/COLOR]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.
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice