Thread: Getting the filename

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    Getting the filename

    I've searched the board several times, but haven't found anything about getting the name of the program itself.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    Can you be more specific.. getting the name of the program your running? Name of what...?

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    ok. My program is obvious.exe and when you run it, I want to MessageBox obvious.exe. If, however, you rename it to ah.exe, the program should MessageBox ah.exe
    Last edited by neandrake; 12-06-2003 at 02:11 PM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    have you tried to look in argv[0]?

    Code:
    int main(int argc, char* argv[]) {
        printf("%s", argv[0]);
        return 0;
    }

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    #include <stdio.h>
    
    int main (int argc, char *argv[])
    {
        printf("%s", argv[0]);
    
        return 0; 
    }
    gg

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    well i figured you'd guys be smart enough to figure out that I put this in the Windows section for a reason...

    Is there no api function to retrieve this?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    Originally posted by neandrake
    well i figured you'd guys be smart enough to figure out that I put this in the Windows section for a reason...

    Is there no api function to retrieve this?
    I usually view new threads, and don't look in which forum they are.

    Hmm, have you looked in lpCmdLine? Maybe Windows has something similar to argv[0].
    Last edited by glUser3f; 12-06-2003 at 02:28 PM.

  8. #8
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Yes, I have tested lpCmdLine and it doesn't include the name, only arguments/parameters passed to the program.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  9. #9
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    Originally posted by neandrake
    ok. My program is obvious.exe and when you run it, I want to MessageBox obvious.exe. If, however, you rename it to ah.exe, the program should MessageBox ah.exe
    Could you please explain more? what do you mean by "I want to MessageBox obvious.exe?" Do you want to show a message box containing the exe name?

  10. #10
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I was just giving an example. I know how to MessageBox. I'm trying to make a sort of 'install' for my program and I need to know the location (gotten with GetCurrentDirectory()) and I need to get the name of the program (in case someone renames it). If my program was "C:\Program Files\MyProgram\Barfsnort.exe" but the user decided to rename it to Snortbarf.exe, then I have to update settings.

    All I need to know is how to get the name of the program
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  11. #11
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    call the function GetCommandLine().

    Code:
    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
    {
    	MessageBox(NULL, GetCommandLine(), "Program exe: ", MB_OK);
    	return 0;
    }
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  12. #12
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    slick hybrid, very slick

    god I knew it was a one-liner
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  13. #13
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    That call will include command line arguments though, if they exist.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  14. #14
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    damnit

    lool

    hrm

    So I use a string function to remove lpCmdLine from GetCommandLine(), and it should be fine, right?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  15. #15
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Well in a call to GetCommandLine() the exe path is in quotes of its own, so it shouldn't be too hard to separate.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-11-2009, 01:49 AM
  2. Pass Filename to another function
    By awesmesk8er in forum C Programming
    Replies: 9
    Last Post: 10-24-2008, 01:43 PM
  3. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  4. Time as filename
    By rkooij in forum C Programming
    Replies: 8
    Last Post: 03-02-2006, 09:17 AM
  5. Replies: 3
    Last Post: 01-25-2006, 10:30 PM