Thread: My own filename?

  1. #1

    My own filename?

    How can I get my program to grab a string containing its own pathname so that I can enter it in the Registry? (It has to be an absolute path name--i.e., c:\install\myprog\myprog.exe, not just myprog.exe)

    I tried AfxGetApp()->m_pszAppName and got an invalid pointer (Debug assertion failed when I tried to run it). Even using this->m_pszAppName came up with a null string (I used MessageBox(NULL,this->m_pszAppName,"Filename",MB_ICONINFORMATION) to test).

    This is for a Windows program that is being written from scratch, so it can't be a case of Microsoft deciding that they know better than I do what I want to do. (Well, it can, but it's far less likely than it would be with the AppWizard)

  2. #2
    Davros
    Guest
    The first argument passed to the main function should contain the program path & filename.

    If you're using Borland, then use

    Application->ExeName

  3. #3
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    If you are programming in Windows then the string passed to WinMain does not contain the executable name. I think the function is called GetModuleName().

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  4. #4
    Erm...I'm using Visual C++, not Borland. If I call AfxGetAppName() from InitInstance instead of the constructor, I get the executable file name, but not the full path name, and I need the full path.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Have you tried GetCurrentDirectory() ?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Aug 2001
    Location
    Melbourne, Australia
    Posts
    92
    Here's stuff from MSDN... should be what you're looking for:

    GetModuleFileName
    The GetModuleFileName function retrieves the full path and filename for the executable file containing the specified module.

    Windows 95: The GetModuleFilename function will return long filenames when an application's version number is greater than or equal to 4.00 and the long filename is available. Otherwise, it returns only 8.3 format filenames.

    Code:
    DWORD GetModuleFileName(
      HMODULE hModule,    // handle to module to find filename for
      LPTSTR lpFilename,  // pointer to buffer to receive module path
      DWORD nSize         // size of buffer, in characters
    );
    Parameters
    hModule
    Handle to the module whose executable filename is being requested. If this parameter is NULL, GetModuleFileName returns the path for the file used to create the calling process.
    lpFilename
    Pointer to a buffer that is filled in with the path and filename of the given module.
    nSize
    Specifies the length, in characters, of the lpFilename buffer. If the length of the path and filename exceeds this limit, the string is truncated.
    Return Values
    If the function succeeds, the return value is the length, in characters, of the string copied to the buffer.

    If the function fails, the return value is zero. To get extended error information, call GetLastError.

    Remarks
    If a module is loaded in two processes, its module filename in one process may differ in case from its module filename in the other process.

    QuickInfo
    Windows NT: Requires version 3.1 or later.
    Windows: Requires Windows 95 or later.
    Windows CE: Unsupported.
    Header: Declared in winbase.h.
    Import Library: Use kernel32.lib.
    Unicode: Implemented as Unicode and ANSI versions on Windows NT.

    See Also
    Dynamic-Link Libraries Overview, Dynamic-Link Library Functions, GetModuleHandle, LoadLibrary
    psychobrat at gmail

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