GetModuleFileName
The GetModuleFileName function retrieves the full path and filename for the executable file containing the specified module.返回当前进程已加载可执行或DLL文件的完整路径名(以'\0'终止),是windows的API函数,使用的时候需要包含windows.h的头文件。
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.
DWORD GetModuleFileName(
HMODULE hModule, // handle to module to find filename for应用程序或DLL实例句柄,NULL则为获取当前程序可执行文件路径名
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.
_splitpath, _wsplitpath
Break a path name into components.
void _splitpath( const char *path, char *drive, char *dir, char *fname, char *ext );
void _wsplitpath( const wchar_t *path, wchar_t *drive, wchar_t *dir, wchar_t *fname, wchar_t *ext );
Routine | Required Header | Compatibility |
_splitpath | <stdlib.h> | Win 95, Win NT |
_wsplitpath | <stdlib.h> or <wchar.h> | Win 95, Win NT |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB | Single thread static library, retail version |
LIBCMT.LIB | Multithread static library, retail version |
MSVCRT.LIB | Import library for MSVCRT.DLL, retail version |
Return Value
None
Parameters
path
Full path待处理的完整的文件名路径
drive
Optional drive letter, followed by a colon (:)驱动器盘符
dir
Optional directory path, including trailing slash. Forward slashes ( / ), backslashes ( \ ), or both may be used.中间的路径
fname
Base filename (no extension)文件名
ext
Optional filename extension, including leading period (.)后缀名
Remarks
The _splitpath function breaks a path into its four components. _splitpath automatically handles multibyte-character string arguments as appropriate, recognizing multibyte-character sequences according to the multibyte code page currently in use. _wsplitpath is a wide-character version of _splitpath; the arguments to _wsplitpath are wide-character strings. These functions behave identically otherwise.
只要在这四个参数中传入对应的字符串指针,函数返回时即可获取对应截取的字符串,不想获取的可以直接填入NULL进行忽略,比如我只想截取文件的后缀名,那么这个函数可以如下调用:_splitpath(path, NULL, NULL, NULL, ext);
Generic-Text Routine Mappings
TCHAR.H Routine | _UNICODE & _MBCS Not Defined | _MBCS Defined | _UNICODE Defined |
_tsplitpath | _splitpath | _splitpath | _wsplitpath |
Each argument is stored in a buffer; the manifest constants _MAX_DRIVE, _MAX_DIR, _MAX_FNAME, and _MAX_EXT (defined in STDLIB.H) specify the maximum size necessary for each buffer. The other arguments point to buffers used to store the path elements. After a call to _splitpath is executed, these arguments contain empty strings for components not found in path. You can pass a NULL pointer to _splitpath for any component you don’t need.
_makepath, _wmakepath
Create a path name from components.
void _makepath( char *path, const char *drive, const char *dir, const char *fname, const char *ext );
void _wmakepath( wchar_t *path, const wchar_t *drive, const wchar_t *dir, const wchar_t *fname, const wchar_t *ext );
Routine | Required Header | Compatibility |
_makepath | <stdlib.h> | Win 95, Win NT |
_wmakepath | <stdlib.h> or <wchar.h> | Win 95, Win NT |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB | Single thread static library, retail version |
LIBCMT.LIB | Multithread static library, retail version |
MSVCRT.LIB | Import library for MSVCRT.DLL, retail version |
Return Value
None
Parameters
path
Full path buffer
drive
Drive letter
dir
Directory path
fname
Filename
ext
File extension
Remarks
The _makepath function creates a single path and stores it in path. The path may include a drive letter, directory path, filename, and filename extension. _wmakepath is a wide-character version of _makepath; the arguments to _wmakepath are wide-character strings. _wmakepath and _makepath behave identically otherwise.
Generic-Text Routine Mappings
TCHAR.H Routine | _UNICODE & _MBCS Not Defined | _MBCS Defined | _UNICODE Defined |
_tmakepath | _makepath | _makepath | _wmakepath |
The following arguments point to buffers containing the path elements:
drive
Contains a letter (A, B, and so on) corresponding to the desired drive and an optional trailing colon. _makepath inserts the colon automatically in the composite path if it is missing. If drive is a null character or an empty string, no drive letter and colon appear in the composite path string.
dir
Contains the path of directories, not including the drive designator or the actual filename. The trailing slash is optional, and either a forward slash (/) or a backslash (\) or both may be used in a single dir argument. If a trailing slash (/ or \) is not specified, it is inserted automatically. If dir is a null character or an empty string, no slash is inserted in the composite path string.
fname
Contains the base filename without any extensions. If fname is NULL or points to an empty string, no filename is inserted in the composite path string.
ext
Contains the actual filename extension, with or without a leading period (.). _makepath inserts the period automatically if it does not appear in ext. If ext is a null character or an empty string, no period is inserted in the composite path string.
The path argument must point to an empty buffer large enough to hold the complete path. Although there are no size limits on any of the fields that constitute path, the composite path must be no larger than the _MAX_PATH constant, defined in STDLIB.H. _MAX_PATH may be larger than the current operating-system version will handle.
e.g:
// path.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "path.h" #include <stdlib.h> #include <stdio.h> #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // The one and only application object CWinApp theApp; using namespace std; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs cerr << _T("Fatal Error: MFC initialization failed") << endl; nRetCode = 1; } else { // TODO: code your application's behavior here. char caPath[256]; CString csFullPath; CString csPath; memset(caPath,0,sizeof(caPath)); GetModuleFileName(NULL,caPath,256); csFullPath.Format("%s",caPath); int nPos = csFullPath.ReverseFind(_T('\\')); csPath = csFullPath.Left(nPos+1); printf( "csFullPath: %s\n",csFullPath); printf( "csPath: %s\n",csPath); char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; _splitpath( caPath, drive, dir, fname, ext ); printf("Path extracted with _splitpath:\n" ); printf("Drive: %s\n", drive ); printf("Dir: %s\n", dir ); printf("Filename: %s\n", fname ); printf("Ext: %s\n", ext ); char path_buffer[256]; _makepath(path_buffer, drive, dir, fname, ext); printf( "Path created with _makepath: %s\n\n", path_buffer ); } return nRetCode; }