1. Gets the path of the current executable file and the current project directory under Windows. (1) Get the current executable file path:
#include <shlwapi.h> #pragma comment (lib, "Shlwapi.lib") wchar_t Szexepath[max_path] = {0}; Getmodulefilenamew (NULL, Szexepath, sizeof (Szexepath)); PATHREMOVEFILESPECW (Szexepath);
(2) If you want to get the current project path, you can use the following function:
GetCurrentDirectory ()
2. Get the current executable path and the project path under Linux. (1) Get the current executable file path:
#include <limits.h> #include <stdio.h> #include <string.h> #include <unistd.h>size_t Getcurrentexcutablefilepathname (char* processdir,char* processname, size_t len) { char* path_end; if (Readlink ("/proc/self/exe", Processdir,len) <=0) return-1; Path_end = STRRCHR (Processdir, '/'); if (path_end = = NULL) return-1; ++path_end; strcpy (ProcessName, path_end); *path_end = ' + '; Return (size_t) (Path_end-processdir);}
(2) If you want to get the current project path, you can use the following function:
Header file: #include <unistd.h>//definition function: char * GETCWD (char * buf, size_t size);//Function Description: GETCWD () copies the current working directory absolute path to the parameter buf The space size of the indicated memory space, the parameter size is BUF
Getting the current executable path and working directory under Windows and Linux