VC + + 3 main write/read configuration filesInithe function: bool WritePrivateProfileString (LPCTSTR lpappname,lpctstr lpkeyname,lpctstr lpstring,lpctstr lpFileName), write to. ini file; DWORDgetprivateprofilestring(LPCTSTR lpappname,lpctstr lpkeyname,lpctstr lpdefaut,lpstr lpreturnedstring,dword nSize,LPCTSTR lpFileName); Read the. ini file; UINT getprivateprofileint (lpctstr lpappname,lpctstr lpkeyname,int ndefault,lpctstr lpfilename); Reads the shaping value. The meaning of one of the parameters: LPCTSTR a field name in the lpappname-------INI file LPCTSTR lpkeyname-------A key name under Lpappname, that is, the specific variable name inside LPCTSTR lpstring-------is the key value, which is the value of the variable, must be LPCTSTR or CString type LPCTSTR lpfilename-------Full INI file path name LPCTSTR Lpdefaut-------Assign this value to a variable if it does not have its first two parameter values LPSTR lpreturnedstring-------The CString object that receives the value in the INI file, that is, the receive buffer DWORD nSize-------The size of the receive buffer Example : CString strname,strtemp; int nAge; StrName = "Jacky"; NAge = 13; writeprivateprofilestring ("Student", "Name", StrName, "C:\\setting.ini"); Results: (The INI file appears as follows:) [Student] Name=jacky Read: CString SName; getprivateprofilestring ("Student", "Name", "DefaultName", Sname.getbuffer (Max_length), Max_length, "C:\\setting.ini"); Result: SName = "Jacky"; it is important to note that it is necessary to release the GetBuffer function (with the Sname.releasebuffer () function), or else the other sub-functions that use SName later will fail. Reading integers is relatively simple, as follows int result = Getprivateprofileint("Student", "NAge", 0, "C:\\setting.ini") The return value is the result of the read! The last parameter in getprivateprofilestring is the parameter of the profile path, this path can only be an absolute path and cannot be a relative path, but now I need to be my exe file to be able to be with my config file. So I used the getcurrentdirectory function. The original code is as follows: CString server_ip; CString des= ""; ::getcurrentdirectory(max_pathlength,des. GetBuffer (max_pathlength)); Des. ReleaseBuffer (); des+= "\\config.ini"; getprivateprofilestring ("Phonedemo", "Server_ip", "" ", server_ip. GetBufferSetLength (), 15,des); Server_ip. ReleaseBuffer (); Note: When using the CString variable here, after GetBuffer is used, it is important to use the ReleaseBuffer () function before you can do other things like string + operations More information : Get path GetCurrentDirectory only returns the current directory of the current process, not the directory where the process's image file (. exe) resides getcurrentdirectory () is suitable for systems such as XP and cannot be used on wince GetModuleFileName () applies after WinCE2.0 How to use: The following section of code is primarily to obtain the directory where the current program's running directory (. exe) resides { CString path; GetModuleFileName (Null,path. GetBufferSetLength (max_path+1), MAX_PATH); Path. ReleaseBuffer (); int pos = path. Reversefind (' \ \ '); Path = path. Left (POS); } GetModuleFileName function WINAPI DWORD GetModuleFileName ( Hmodule hmodule, LPWStr lpFileName, DWORD nSize ); GetBuffer and ReleaseBuffer are a set of functions that need to be used together, compared to getbuffersetlength , if the allocated space is greater than the actual saved string (0 end) , ReleaseBuffer will release the extra space for the application and return it to the system; However, it is important to note the following: If the string to be saved is ABC (0 end), the GetBuffer parameter should be at least is 3; If the content to be saved does not end with 0, such as reading the file data, the ReleaseBuffer parameter must be a file length if the GetBuffer parameter is larger than the file length (if GetBuffer parameter is file length, there is no problem, the ReleaseBuffer parameter can be the default-1)! GetBufferSetLength is relatively easy to understand, it applies for a specified length of space, even if the length of the final stored string is less than the requested space length, will not release the extra space. _______________________________________________________________________________________________________________ ___dwordGetCurrentDirectory( DWORD Nbufferlength, LPTSTR lpbuffer ); The GetCurrentDirectory function retrieves the current directory for the current process. GetCurrentDirectory returns the current directory of the current process and does not necessarily return the directory of your 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. Parameters Nbufferlength: Receives the string cache length of the saved path, and the cache must have a location where the empty characters are saved. Lpbuffer: Pointer to the cache that receives the string, and the received non-empty string specifies the absolute path of the current directory. DWORD GetModuleFileName ( Hmodule hmodule, LPTSTR lpFileName, DWORD nSize ); The GetModuleFileName function specifies the path of the current process module. It only operates the modules under the current process. If you want to get module information under other processes, you need to use the Getmodulefilenameex function. Parameters Hmodule: The handle to the module, or set to NULL to represent the current module. lpFileName: The buffer where the path is saved. NSize: The size of the buffer. Example: TCHAR Strexepath[_max_path]; GetModuleFileName (Null,strexepath,_max_path); Pathremovefilespec (Strexepath); If the current executing program's location is C:\test.exe,getmodulefilename Strexepath is C:\test.ext, The final strexepath obtained by removing the name function is C:. (NotePathremovefilespecSystem API function calls must contain # include "Shlwapi.h" as the header file) TCHAR Strexepath[_max_path]; GetCurrentDirectory (_max_path, Strexepath); Gets the current system directory, possibly C: or another value. ------------------------------------------------------------------------------------------------------ Shlwapi.dll Shlwapi-shlwapi.dll-dll File Information DLL files: Shlwapi or Shlwapi.dll DLL Name: Microsoft Shell light-weight Utility Library Description: Shlwapi.dll is a UNC and URL-Address dynamic-link library file for registering key values and color settings. Belongs to: Microsoft Windows Shell System DLL File: Yes Common errors: File not Found, Missing file, Exception Errors Security Level (0-5): 0 Spyware: No adware: No Use GetModuleFileName to get application path in VC
. \ \ \ GetModuleFileName get the application directory with API function why not? using. \ \ can also get the application directory , using GetModuleFileName can also be obtained, how are they different? The same! One is a relative path, one is an absolute path . \ \ is the current directory of the application, but the current directory does not necessarily equal the directory where the application executes the file, and when an application is started, the current directory can be arbitrarily set. GetModuleFileName () Gets the full path name of the module , for example, you load the C:\windows\system32\a.dll and get the module handle H, then you can use the GetModuleFileName () Get the full path name of the H module. . \ \ is typically used in statements that contain header files. The other is when the program compiles, for example, to open a custom configuration file. How to get this Hanlde? If you load the DLL directly with LoadLibrary () or AfxLoadLibrary (), the function return value is handle; If you implicitly load the DLL, use GetModuleHandle ("DLL file name") can also get handle; MFC program gets itself path In development projects, it is often necessary to know the directory in which the current program itself resides. One way is to use the installer to write the file path to the registry when the program is installed. In larger programs, this method is more commonly used Another way is to get the path in the program. This way, you can get the correct path for the program to move anywhere. This is also the approach described in this article. Method One: [Code] Get the path to the Help file CString strfullname = AfxGetApp ()->m_pszhelpfilepath; What we got was: X:\XXXX\XXX.hlp. Parse the path to get the directory where the program is currently running 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 where the current running program is obtained [/code] In addition,AfxGetApp ()->m_pszappname gets the application name AfxGetApp ()->m_pszexename Get program file name, not including extension Method Two: Get the full path TCHAR Exefullpath[max_path]; Max_path GetModuleFileName (Null,exefullpath,max_path);//Get program module name, full path The full path of the current running program The path of the program can be obtained by means of the analytic path of method one. GetModuleFileName function prototypes DWORD GetModuleFileName ( Hmodule hmodule,//handle to module. The handle of the module that will be obtained. If it is the current module, NULL LPTSTR lpFileName,//path buffer gets the file name. DWORD nSize)//size of buffer general MAX_PATH Yes |