Get the current executable path and working directory under Windows and Linux

Source: Internet
Author: User

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

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.