Getcurrentdirectory, setcurrentdirectory, and getmodulefilename

Source: Internet
Author: User

DWORD getcurrentdirectory (
DWORD nbufferlength, // size of directory Buffer
Lptstr lpbuffer // directory Buffer
);
Function:
Find the current directory of the current process (. DSW folder)
Parameter description:
Parameter type and description
Length of the nbufferlength Buffer
Lpbuffer specifies a predefined string to load the current directory.
Return Value
If the call succeeds, the number of bytes loaded to lpbuffer is returned.
If the length of the nbufferlength is not enough to accommodate the directory, the returned value is the necessary buffer length (at least this length is required), including air stop characters. Zero indicates failure. Use the getlasterror function to obtain error information.

Bool setcurrentdirectory (
Lpctstr lppathname
);
The function sets the path. If the return value of the function is 0, the setting fails. If the return value is not 0, the setting is successful;

Program example:
# Include "stdafx. H"
# Include <windows. h>
# Include <stdio. h>
# Include <conio. h>
# Include <tchar. h>

# Define bufsize max_path

Int _ tmain (INT argc, tchar ** argv, tchar ** envp)
{
Tchar buffer [bufsize];
DWORD dwret;

// If (argc! = 2)
//{
// _ Tprintf (text ("Usage: Test <dir> N "));
// Return 0;
//}
Dwret = getcurrentdirectory (bufsize, buffer); // a total of 45 characters, excluding '/0', the number of characters returned by dwret, And the subscript of the singular array starts from scratch, therefore, [45] indicates 46 characters, including '/0'
Setcurrentdirectory ("C ://");
Dwret = getcurrentdirectory (bufsize, buffer );
If (dwret = 0) // If zero is returned, the current path of the file fails to be obtained. The so-called current path is the folder where. DSW is located.
{
_ Tprintf (text ("getcurrentdirectory failed (% d) N "),
Getlasterror ());
Return 0;
}
If (dwret> bufsize) // if the maximum number of characters is exceeded, an out-of-bounds message is displayed. max_path is 260
{
_ Tprintf (text ("getcurrentdirectory failed (buffer too small, need % d chars)/n"), dwret );
Return 0;
}
If (! Setcurrentdirectory (argv [1])
{
_ Tprintf (text ("setcurrentdirectory failed (% d) N "),
Getlasterror ());
Return 0;
}
_ Tprintf (text ("set current directory to % Sn"), argv [1]);

If (! Setcurrentdirectory (buffer ))
{
_ Tprintf (text ("setcurrentdirectory failed (% d) N "),
Getlasterror ());
Return 0;
}
_ Tprintf (text ("restored previous directory (% s). N"), buffer );

Return 1;
}

Note: In the Program Statement, setcurrentdirectory ("C: //") refers to changing the current path of the process to: C:/. The function is used to set the current path;
If: setcurrentdirectory ("..") is set to the upper-level directory of the current directory;

Getmodulefilename () function:

Use getmodulefilename in VC to obtain the application path: (the path here is the absolute path)

. // Is different from the API function getmodulefilename to obtain the application directory?
--------------------------------------------------------------------------------
The application directory can also be obtained by using. // and getmodulefilename. What is the difference between the two?
--------------------------------------------------------------------------------
Same!
One is the relative path and the other is the absolute path (getmodulefilename is the complete path name of the retrieved module)
--------------------------------------------------------------------------------
. // Obtain the current directory of the application, but the current directory is not necessarily equal to the directory where the application execution file is located. When an application is started, the current directory can be set at will.
Getmodulefilename () obtains the complete path name of the module. For example, you can load C:/Windows/system32/. DLL to obtain the module handle H. You can use getmodulefilename () to obtain the complete path name of the H module.

--------------------------------------------------------------------------------
. // Is generally used in statements containing header files.
The other is that the program works after compilation, for example, opening a custom configuration file.
--------------------------------------------------------------------------------
How to obtain this hanlde?
--------------------------------------------------------------------------------
If you directly load the DLL using loadlibrary () or afxloadlibrary (), the return value of this function is handle;
If you implicitly load the DLL, use getmodulehandle ("DLL file name") to obtain the handle;
The MFC program obtains its own path

In development projects, you often need to know the directory where the current program is located.
One way is to use the installer to write the file path to the Registry during program installation. This method is commonly used in large programs.
The other is to get the path in the program. In this way, the program can get the correct path wherever it moves. This is also the method described in this article.

Method 1:
[Code]
// Obtain the path of the Help File
Cstring strfullname = afxgetapp ()-> m_pszhelpfilepath;
// The result is: X:/xxxx/xxx. HLP.

// Parse the path to get the directory of the current running program
Char Drive [_ max_drive];
Char dir [_ max_dir];

_ Splitpath (strappname, drive, Dir, null, null );
Cstring strpath;
Strpath. Format ("% S % s", drive, DIR );
// Strpath is the directory of the current running program.
[/Code]
In addition, afxgetapp ()-> m_pszappname gets the application name
Afxgetapp ()-> m_pszexename get the program file name, excluding the extension

Method 2:
Get full path
Tchar exefullpath [max_path]; // max_path
Getmodulefilename (null, exefullpath, max_path); // obtain the program module name, full path
That is, the full path of the current running program.
Use method 1 to parse the path to obtain the path of the program.

Getmodulefilename function prototype
DWORD getmodulefilename (
Hmodule, // handle to module. The handle of the module to be obtained. If it is the current module, null
Lptstr lpfilename, // file name obtained by path buffer.
DWORD nsize // size of buffer is generally max_path.
);

A simple example:
Getcurrentdirectory (bufsize, buffer );
Getmodulefilename (null, szpath, sizeof (szpath); // null is the current Module

View the buffer: "C:/Documents ents and settings/all users/desktop/test"
View szpath: "C:/Documents and Settings/all users/desktop/test/debug/test.exe" to obtain the complete path name of the module.

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.