C/C++ - 获取可执行程序的绝对路径(Linux、Windows)

引用:https://blog.csdn.net/giveaname/article/details/88973102

简介

在代码中获取可执行程序的绝对路径(不是命令行获取)


Linux

函数1:char *getcwd(char *buf, size_t size);

头文件:unistd.h

说明:获取到可执行程序的绝对路径,存放到buf中,size是指定的buf大小。若size指定的大小比 buf短,会返回NULL。

若不知道buf的长度,可以设置size为0,则getcwd会调用malloc动态给buf分配空间,不过后续要记得调用free释放buf。

不足:使用后发现,该函数返回的是执行可执行程序的绝对路径。

函数2:int readlink(const char * path ,char * buf,size_t bufsiz);

头文件:unistd.h

说明:该将参数path的符号连接内容存到参数buf所指的内存空间,返回的内容不是以NULL作字符串结尾。

返回值即为字段buf的长度。若传入的参数bufsiz小于符号连接的内容长度,过长的内容会被截断。

不足:这个函数并不是直接获取程序的绝对路径,是通过获取当前程序的自身信息拿到的路径。

且得到的路径包括了可执行程序名称,需要再处理。

测试

#include<stdio.h>
#include<string.h>
#include<unistd.h>

int main()
{
    char szBuf[128];
    char szPath[128];
    
    memset(szBuf, 0x00, sizeof( szBuf));    
    memset( szPath, 0x00, sizeof(szPath));
 
    getcwd(szBuf, sizeof(szBuf)-1);
    printf("buf:%s\n", szBuf);
 
    int ret =  readlink("/proc/self/exe", szPath, sizeof(szPath)-1 );
    printf("ret:%d\n", ret);
    printf("path:%s\n", szPath);
 
    return 0;
}

若是在/home/alex/test-getpath路径执行,得到结果

若是在/home路径执行,得到结果

 

Windows

函数1:char * getcwd(char * buf, size_t size);

头文件:#include<direct.h>

说明:用法与Linux类似,获取得到的结果是执行文件的目录。

不足:与Linux类似。

函数2:DWORD WINAPI GEtModuleFileName(_In_opt_ HMODULE hModule, _Out_ LPTSTR lpFilename, _In_ DWORD nSize);

  参数1:应用程序或DLL实例句柄,NULL则为获取当前程序可执行文件路径名

  参数2:接收路径的字符串缓冲区

  参数3:接收路径的字符缓冲区的大小

头文件:#include<windows.h>

说明:该函数类似Linux下的readlink。

不足:也会包含可执行程序名称,需再处理去除。

测试

#include<windows.h>
#include<direct.h>
#include<stdio.h>
 
int main()
{
    char szBuf[512] = { 0 };
    getcwd(szBuf, sizeof(szBuf)-1);
    printf("buf:%s\n", szBuf);
 
 
    char szPath[512] = {0};
    GetModuleFileName(NULL, szPath, sizeof(szPath)-1);
    printf("path:%s\n", szPath);
    getchar();
}

假设生成的可执行程序叫demo.exe,放在:C:\program\test目录,

若是在 C:\program\test执行,输出:

若是在C:\program执行,输出:

 

Windows环境,获取调用动态库的所在路径

说明:一个动态库demo.dll,其中封装了获取动态库的函数getPath(),一个可执行程序linkDemo.exe链接该dll,要获取demo.dll所在路径。

实现

/**demo.dll的内容**/
#include<windows.h>
#include<direct.h>
#include<stdio.h>
#include<tchar.h>
#include"test.h"

int getPath()
{
    char szPath[512] = { 0 };
    GetModuleFileName(NULL, szPath, sizeof(szPath) - 1);
    printf("path:%s\n", szPath);
    return 0;
}
int getLibraryPath()
{
    char szPath[512] = {0};
    HMODULE hMod = GetModuleHandle(_T("demo.dll"));
    GetModuleFileName(hMod, szPath, sizeof(szPath)-1);
    printf("path:%s\n", szPath);
    return 0;
}

测试

#include "test.h"
#include<stdio.h>
 
int main()
{
    printf("begin....\n");
    getLibraryPath();
    getchar();
}

将linkDemo.exe放置在 C:/program/test,将demo.dll放置在E:/library中,执行可分别获得exe和dll的路径:

posted @ 2022-06-09 15:13  Citrusliu  阅读(5702)  评论(0编辑  收藏  举报