MFC 獲得當前程式所在目錄

2021-07-04 01:54:08 字數 1614 閱讀 8951

原文**:

mfc獲得當前應用程式目錄的getcurrentdirectory()和getmodulefilename()函式,getcurrentdirectory只是返回當前程序的當前目錄,而並不是程序的映象檔案(.exe)所在的目錄,getcurrentdirectory()適用於xp等系統,,在wince上不能使用,getmodulefilename()適用於wince2.0以後. 

函式說明:

dword winapi getmodulefilename(

hmodule hmodule,

lptstr lpfilename,

dword nsize

);函式功能

此函式得到當前應用程式的執行目錄,還包括應用程式的檔名。 

引數說明

hmodule:要獲取檔名的模組名柄,可以是運用loadliberary得到的控制代碼,null表示當前模組 

lpfilename:輸出引數,存放取得的檔名 

nsize:lpfilename引數的長度

dword getcurrentdirectory(

dword nbufferlength, // size of directory buffer

lptstr lpbuffer // directory buffer 

); 函式功能

找到當前程序的當前目錄 

引數說明

nbufferlength:lpbuffer緩衝區的長度 

lpbuffer:指定乙個預定義字串,用於裝載當前目錄 

返回值 

呼叫成功 返回裝載到lpbuffer的位元組數。

使用方法:

//下面的一段**主要是獲得當前程式的執行目錄(.exe)所在的目錄

getmodulefilename函式

winapi dword getmodulefilename( 

hmodule hmodule,

lpwstr lpfilename, 

dword nsize

);getbuffer和releasebuffer是一套需要配合使用的函式, 與getbuffersetlength相比, 優點是如果分配的空間大於實際儲存的字串(0結尾),releasebuffer會把多餘申請的空間釋放, 歸還給系統。

附加示例:

cstring m_tempfile;

//獲取應用程式的全路徑 

char exefullpath[max_path];  

getmodulefilename(null, (lpwch)exefullpath, max_path);  

//將其格式化為字串  

m_tempfile.format(l"%s", exefullpath); 

//去掉應用程式的全名(15為應用程式檔案全名的長度)  

exefullpath[m_tempfile.getlength()-15]='\0';  

//得到應用程式所在路徑  

m_tempfile.format(l"%s", exefullpath); 

//得到程式中檔案的全路徑  

m_tempfile+="***.exe";

stay hungry, stay foolish!

獲得當前程式路徑

1.返回當前程式檔案全檔名 strcat pbuf,exe 獲取程式全檔名 cstring filename filename pbuf char 轉換為cstring如此簡單 afxmessagebox filename 2.方法2 char path max path max path在api中...

如何獲得當前執行程式所在的路徑

如果是linux,看下面這些 利用proc可以獲得完整的路徑 乙個linux下的例子 include include ifndef path max define path max 1024 endif char get exe name buf rslt 0 return buf int main...

獲取當前程式執行目錄

1.使用 getcwd std string getmodulepath char path nullptr path char malloc ilength sizeof char path getcwd modulepath,ilength if null path free path if f...