複製鏈接
請複製以下鏈接發送給好友

GetWindowsDirectory

鎖定
GetWindowsDirectory是一個函數,用以獲取Windows目錄的完整路徑名。
中文名
GetWindowsDirectory
獲    取
Windows文件夾
參    數
lpBuffer String
説    明
獲取Windows目錄的完整路徑名

GetWindowsDirectory簡介

GetWindowsDirectory函數能獲取Windows目錄的完整路徑名。在這個目錄裏,保存了大多數windows應用程序文件及初始化文件

GetWindowsDirectory返回值

Long類型,複製到lpBuffer的一個字串的長度。如lpBuffer不夠大,不能容下整個字串,就會返回lpBuffer要求的長度,零表示失敗。並且將出錯的信息存儲在GetLastError函數中,用户可以通過調用GetLastError來得到錯誤信息。

GetWindowsDirectory參數表

參數 類型及説明
lpBuffer String,指定一個字串緩衝區,用於裝載Windows目錄名。除非是根目錄,否則目錄中不會有一箇中止用的“\”字符
nSize Long,lpBuffer字串的最大長度

GetWindowsDirectory使用詳解

獲取系統文件夾API函數
獲取Windows文件夾的路徑
UINTGetWindowsDirectory(LPTSTR lpBuffer,UINT uSize)
獲取system32文件夾的路徑
UINTGetSystemDirectory(LPTSTR lpBuffer,UINT uSize)
這兩個函數的使用方法很簡單,和以前一樣有兩種方法來確定緩衝區的長度:
在C++中的使用方法如下:
1、Windows定義了一個文件路徑的最長長度的常量MAX_PATH(值為260),我們可以用它來建立字符串緩衝區;
2、給緩衝區傳入NULL,調用函數成功後將返回緩衝區的長度。
下面來看例子:
wstring getWindowsDirectory()
{
wstring wstr;
UINT size=GetWindowsDirectory(NULL,0);
wchar_t *path=new wchar_t[size];
if(GetWindowsDirectory(path,size)!=0) //函數調用失敗將返回0
{
wstr=path;
}
delete [] path;
return wstr;
}
wstring getSystemDirectory()
{
wstring wstr;
UINT size=GetSystemDirectory(NULL,0);
wchar_t *path=new wchar_t[size];
if(GetSystemDirectory(path,size)!=0)
{
wstr=path;
}
delete [] path;
return wstr;
}
在VB6.0中的調用示例如下:
'獲取Windows文件夾路徑
privateDeclare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long'在form窗體中聲明改函數
Dim SWinDir As String '定義字符變量用來存儲路徑
Dim Retn As Long ‘定義長整型變量存儲路徑的長度
SWinDir = Space(255)’設定一個空串,長度為windows允許的最大長度,也可寫作:SWidir=String(255,0)
Retn = GetWindowsDirectory(SWinDir, Len(SWinDir))‘獲取windows路徑的長度,swindir存儲了路徑
SWinDir = Left(SWinDir, Retn)’去掉空白內容。