GetWindowsDirectory

GetWindowsDirectory是一個函式,用以獲取Windows目錄的完整路徑名。

基本介紹

  • 中文名:GetWindowsDirectory
  • 獲取:Windows資料夾
  • 參數:lpBuffer String
  • 說明:獲取Windows目錄的完整路徑名
簡介,返回值,參數表,使用詳解,

簡介

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

返回值

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

參數表

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

使用詳解

獲取系統資料夾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)’去掉空白內容。

相關詞條

熱門詞條

聯絡我們