DLL 內部獲取自身絕對路徑

2021-10-03 06:47:56 字數 1013 閱讀 9905

常見的獲取方法是通過 getmodulehandle(null),獲取當前exe控制代碼,然後通過getmodulefilename獲取exe路徑。dll在該路徑下加上 「***.dll」,即可。

但是有時候 dll 與 exe 不一定在乙個位置,甚至 dll 並沒有按照指定的目錄關係與 exe 有明確的對應關係,甚至 dll 的最終名稱作為 dll 的編寫者並不清楚(比如重新命名)。

有以下幾種方法:

在dllmain中儲存hmodule

使用如下** getselfmodulehandle

hmodule getselfmodulehandle()

使用 getmodulehandleex

hmodule hmodule = null;

getmodulehandleex(get_module_handle_ex_flag_from_address,

(lpcstr)&***, //可以是任何函式,全域性變數等的位址

&hmodule);

// 更簡單的

hmodule hmodule = null;

getmodulehandleexa(get_module_handle_ex_flag_from_address,

__function__,

&hmodule);

hmodule hmodule = null;

getmodulehandleexw(get_module_handle_ex_flag_from_address,

__functionw__,

&hmodule);

使用 __imagebase

extern "c" image_dos_header __imagebase;

hmodule _hd = (hmodule)&__imagebase;

以上只要得到了 hmodule ,自然可以通過 getmodulefilename 來獲取了。

jeecms獲取絕對路徑

在jeecms後台管理做乙個附件上傳到伺服器上,然後讀取改上傳檔案,半天獲取不到路徑,後來發現有定義好的絕對路徑獲取方法 最好將檔案上傳到u資料夾底下 string path u cms www 201807 26111223imd7.xls file file new file realpathr...

檔案絕對路徑獲取

yourclass.class.getclassloader getresource threadpoolconfig.xml getpath 通過上述方法獲取到的一般是指定檔案的絕對路徑 並且該檔案必須存放在src目錄下 存在的問題 如果絕對路徑中存在空格,getpath取到的值是經過urlenc...

絕對路徑 相對路徑的獲取

呼叫函式 getmodulefilename,返回當前程序的映象檔案 exe 所在的目錄。較為穩妥且移植性較好。函式原型 函式功能 此函式得到當前應用程式的執行目錄,還包括應用程式的檔名。引數 hmodule handle to module 要獲取檔名的模組控制代碼。null表示當前模組 lpfi...