-
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)
-
The first argument passed to the main function should contain the program path & filename.
If you're using Borland, then use
Application->ExeName
-
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
-
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.
-
Have you tried GetCurrentDirectory() ?
-
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