One-day Windows API training (83) getmodulefilename Function
During software development, you often need to save the data to the current execution file path or read some configuration information in the current execution file path. In this case, you need to obtain the directory path from the current module to operate the file at a fixed location. To solve this problem, call the API function getmodulefilename to obtain the path of the module.
The getmodulefilename function declaration is as follows:
Winbaseapi
DWORD
Winapi
Getmodulefilenamea (
_ In_opt hmodule,
_ Out_ecount_part (nsize, return + 1) lpch lpfilename,
_ In DWORD nsize
);
Winbaseapi
DWORD
Winapi
Getmodulefilenamew (
_ In_opt hmodule,
_ Out_ecount_part (nsize, return + 1) lpwch lpfilename,
_ In DWORD nsize
);
# Ifdef Unicode
# Define getmodulefilename getmodulefilenamew
# Else
# Define getmodulefilename getmodulefilenamea
# Endif //! Unicode
Hmodule is the module handle, or set to null to indicate the current module.
Lpfilename is the buffer of the storage path.
Nsize indicates the buffer size.
An example of calling a function is as follows:
#001 // obtain the path of the current program.
#002 // Cai junsheng 2007/12/05 QQ: 9073204 Shenzhen
#003 void testgetexepath (void)
#004 {
#005 //
#006 const int nbufsize = 512;
#007 tchar chbuf [nbufsize];
#008 zeromemory (chbuf, nbufsize );
#009
#010 // obtain the path of the current execution file.
#011 if (getmodulefilename (null, chbuf, nbufsize ))
#012 {
#013 // output path with file name.
#014 outputdebugstring (chbuf );
#015 outputdebugstring (_ T ("/R/N "));
#016
#017 // obtain the file path.
#018 tchar * lpstrpath = chbuf;
#019 pathremovefilespec (lpstrpath );
#020 outputdebugstring (lpstrpath );
#021 outputdebugstring (_ T ("/R/N "));
#022}
#023
#024}
The output result is as follows:
G:/work/windows_api/wincpp2/debug/wincpp.exe
G:/work/windows_api/wincpp2/debug