Gets the current process directory GetCurrentDirectory () and gets the current run module path name GetModuleFileName ()

Source: Internet
Author: User

GetCurrentDirectory obtained is the Active Directory of the current process (Resource Manager), which can be modified with setcurrentdirectory.

Transfer from http://m.blog.csdn.net/blog/scuthanman/10094793

The GetModuleFileName function can get the absolute pathname of the current execution module , regardless of the process in which the module is called.

Instead, GetCurrentDirectory gets the current directory where the current process resides, and does not have a relationship with the location where the module is executed, and returns the directory where the main process resides.

GetCurrentDirectory function prototypes
DWORD GetCurrentDirectory (  DWORD nbufferlength,  LPTSTR lpbuffer);
Parametersnbufferlength: Receives the string cache length of the saved path, and the cache must have a location to save the end of the empty character. Lpbuffer: Pointer to the cache that receives the string, and the non-empty string that is received specifies the absolute path of the current directory.

The GetCurrentDirectory function retrieves the current directory for the current process.

    TCHAR Path[max_path] = {0};    GetCurrentDirectory (MAX_PATH, PATH);
GetCurrentDirectory returns the current directory of the current process and does not necessarily return the directory of your EXE application. If you call the Open File dialog box in your application and you select a file, the directory where the file is located becomes the current directory of the current process.

#include"stdafx.h"#include<Windows.h>int_tmain (intARGC, _tchar*argv[]) {TCHAR Path[max_path]= {0};    GetCurrentDirectory (MAX_PATH, PATH); wprintf (L"%s\n", path);    TCHAR Pathm[max_path];    GetModuleFileName (NULL, Pathm, MAX_PATH); wprintf (L"%s\n", Pathm); System ("Pause"); return 0;}

2.

DWORD GetModuleFileName (Hmodule hmodule, LPTSTR lpFileName, DWORD nSize);

Function parameter Description:
Hmodule hmodule loads a handle to a program instance. If the parameter is NULL, the function returns the full path of the current application.
lpFileName LPTSTR is a pointer to a memory block that you store the returned name as an output parameter
NSize DWORD, the maximum value loaded into the buffer lpfilename
function return value:
If the return is successful, the path of the corresponding module is returned in the lpFileName buffer, and if the nsize is too small, which returns the corresponding string content only by the buffer size that is set.
If the function fails, the return value is 0 and the GetLastError exception code is returned.
The required header files are:
Include Windows.h

The following example is wrong because the pre-process directory is not deterministic

    if(_access ("Uny.ini",0) ==0)    {        //first check if the current path has a configuration filestrcpy (&scfgfile[0], default_cfg_file); }    Else{Getsystemdirectorya (scfgfile,sizeof(Scfgfile)-1); strcat (&scfgfile[0],"\\"); strcat (&scfgfile[0], default_cfg_file); }                     

should use

    0  };     = GetModuleFileName (NULL, Module_path, MAX_PATH);     = Module_path;     if (0 ! = Dw_ret    )        {= dll_path.find_last_of ('\ \') ;         if (-1 ! = Pos)        {            = Dll_path.substr (01);        }    }

Gets the directory where the EXE resides, and does not change depending on the process directory.

Be aware that the directory of the process can be modified with setcurrentdirectory.

Gets the current process directory GetCurrentDirectory () and gets the current run module path name GetModuleFileName ()

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.