One-day Windows API training (83) getmodulefilename Function

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.