Trouble with lpCmdLine

This is a discussion on Trouble with lpCmdLine within the C Programming forums, part of the General Programming Boards category; I've written this code, it looks for a file specified in the commandline and says YES if it exists or ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    Trouble with lpCmdLine

    I've written this code, it looks for a file specified in the commandline and says YES if it exists or NO if it doesn't exist :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char * lpCmdLine, int  nCmdShow)
    
    {
      WIN32_FIND_DATA FindFileData;
      HANDLE hFind;
      HANDLE Find;
      
      hFind = FindFirstFile(lpCmdLine[1], &FindFileData); 
      if (hFind == INVALID_HANDLE_VALUE)
          printf("NO\n");
      else 
          printf ("YES\n"); 
    
      system("PAUSE");	
      return 0;
    }
    It should be working but the compiler says :

    13 [Warning] passing arg 1 of `FindFirstFileA' makes pointer from integer without a cast

    I dont understand it because i'm passing it a char, not an int !
    Someone can help me ?

    I know i should write only lpCmdLine but i need to specify [1] and [2] in my program... so i need it (and i cant use argc because i need WINAPI WinMain coz it has to be an hidden application)

    Thanx a lot
    Last edited by BianConiglio; 05-07-2004 at 07:50 AM.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    > I dont understand it because i'm passing it a char, not an int !
    from the warning message, it looks like the compiler is looking for a pointer. So the compiler hasprobably cast to an int in a vain
    attempt to sort out the problem for you.

    Note that lpCmdLine is "Pointer to a null-terminated string
    specifying the command line for the application, excluding the
    program name. To retrieve the entire command line, use the
    GetCommandLine function.".

    It is not an array of strings like argv in:
    int main (int argc, char * argv[])
    Last edited by DavT; 05-07-2004 at 07:57 AM.
    DavT
    -----------------------------------------------

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    EDIT : I was wrong.. dont read this post


    Quote Originally Posted by DavT
    Note that lpCmdLine is a single string (I presume) not an array of strings
    like argv in: int main (int argc, char * argv[])
    nope ! because i wrote this one and it works

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char * lpCmdLine, int  nCmdShow)
    {
      printf("%s\n",lpCmdLine[1]);
      printf("%s\n",lpCmdLine[2]);
      system("PAUSE");	
      return 0;
    }
    I really dont understand what's wrong in the other code...damn me
    Last edited by BianConiglio; 05-07-2004 at 08:20 AM.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    ... and what did you get out?
    DavT
    -----------------------------------------------

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    EDIT : I was wrong.. dont read this post

    the first code, it is compiled but it crushes if it's executed!

    the second one, works.. "myexe.exe hi world" i see "hi world" printed in the consolle...
    Last edited by BianConiglio; 05-07-2004 at 08:20 AM.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    But in the second code you are passing a char into a function that expects a char*
    so I don't understand why that one should work at all.
    DavT
    -----------------------------------------------

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    Damn... I dunno what i saw... yes u r right it crushes the same as the first one...
    I think I was confused, it was the argv program that worked...

    So do u think there is no way to read more than one param. form lpCmdLine?

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    try this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char * lpCmdLine, int  nCmdShow)
    {
      printf("%s\n",lpCmdLine);
      system("PAUSE");	
      return 0;
    }
    You should get what you wanted to from your second program.
    So now all (!) you have to do is split up your commmand line string using something
    like strtok() or similar and pass the file name correctly into your FindFirstFile() function
    DavT
    -----------------------------------------------

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    good idea about strtok() , i will study on it and post my (I hope) working program

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    oky it is not optimized but i'm on the right way it works, thanx for ur help


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    
    
      
      char separatori[]   = " ";
      char *token;
    
      int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char * lpCmdLine, int  nCmdShow)
    
     {
        token = strtok( lpCmdLine, separatori );
        printf( "%s\n", token );
        token = strtok( NULL, separatori );
        printf( "%s\n", token );
     
        system("PAUSE");	
        return 0;
     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 03:10 PM
  3. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 11:58 AM
  4. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM
  5. 2 questions: WINAPI? lpcmdline?
    By Cheeze-It in forum Windows Programming
    Replies: 2
    Last Post: 12-05-2002, 04:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21