词条 | GetWindowsDirectory |
释义 | 简介GetWindowsDirectory VB声明 Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long 说明 这个函数能获取Windows目录的完整路径名。在这个目录里,保存了大多数windows应用程序文件及初始化文件 返回值 Long,复制到lpBuffer的一个字串的长度。如lpBuffer不够大,不能容下整个字串,就会返回lpBuffer要求的长度。零表示失败。会设置GetLastError 参数表 参数 类型及说明 lpBuffer String,指定一个字串缓冲区,用于装载Windows目录名。除非是根目录,否则目录中不会有一个中止用的“\\”字符 nSize Long,lpBuffer字串的最大长度 使用详解获取系统文件夹的API函数: 获取Windows文件夹的路径 UINT GetWindowsDirectory(LPTSTR lpBuffer,UINT uSize) 获取systrm32文件夹的路径 UINT GetSystemDirectory(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文件夹路径 private Declare 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)’去掉空白内容。 |
随便看 |
百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。