API UINT GetSystemDirectory( LPTSTR lpBuffer, // address of buffer for system directory UINT uSize // size of directory buffer );
Const MAX_PATH = 260Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As LongPublic Function GetWinPath() Dim strFolder As String Dim lngResult As Long strFolder = String(MAX_PATH, 0) lngResult = GetWindowsDirectory(strFolder, MAX_PATH) If lngResult <> 0 Then GetWinPath = Left(strFolder, InStr(strFolder, Chr(0)) - 1) Else GetWinPath = "" End If End Function'在程序中使用方法如下:Private Sub Command1_Click() Call MsgBox("您电脑中 Windows 目录的正确路径是: " & GetWinPath, vbInformation) End Sub
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long Private Sub Form_Load() Dim sSave As String, Ret As Long 'Create a buffer sSave = Space(255) 'Get the system directory Ret = GetSystemDirectory(sSave, 255) 'Remove all unnecessary chr$(0)'s sSave = Left$(sSave, Ret) 'Show the windows directory MsgBox "Windows System directory: " + sSave End Sub
还是用这个吧!'SystemDirectory Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long Private Sub Form_Load() Dim sSave As String, Ret As Long 'Create a buffer sSave = Space(255) 'Get the system directory Ret = GetSystemDirectory(sSave, 255) 'Remove all unnecessary chr$(0)'s sSave = Left$(sSave, Ret) 'Show the windows directory MsgBox "Windows System directory: " + sSave End Sub
Public Sub StartSysInfo() On Error GoTo SysInfoErr
Dim rc As Long Dim SysInfoPath As String
' 试图从注册表中获得系统信息程序的路径及名称... If GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFO, gREGVALSYSINFO, SysInfoPath) Then ' 试图仅从注册表中获得系统信息程序的路径... ElseIf GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFOLOC, gREGVALSYSINFOLOC, SysInfoPath) Then ' 已知32位文件版本的有效位置 If (Dir(SysInfoPath & "\MSINFO32.EXE") <> "") Then SysInfoPath = SysInfoPath & "\MSINFO32.EXE"
' 错误 - 文件不能被找到... Else GoTo SysInfoErr End If ' 错误 - 注册表相应条目不能被找到... Else GoTo SysInfoErr End If
Call Shell(SysInfoPath, vbNormalFocus)
Exit Sub SysInfoErr: MsgBox "此时系统信息不可用", vbOKOnly End Sub
加在上面程序后面 Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String, ByRef KeyVal As String) As Boolean Dim i As Long ' 循环计数器 Dim rc As Long ' 返回代码 Dim hKey As Long ' 打开的注册表关键字句柄 Dim hDepth As Long ' Dim KeyValType As Long ' 注册表关键字数据类型 Dim tmpVal As String ' 注册表关键字值的临时存储器 Dim KeyValSize As Long ' 注册表关键自变量的尺寸 '------------------------------------------------------------ ' 打开 {HKEY_LOCAL_MACHINE...} 下的 RegKey '------------------------------------------------------------ rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey) ' 打开注册表关键字
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' 处理错误...
' 注册表关键字 ROOT 类型... Const HKEY_LOCAL_MACHINE = &H80000002 Const ERROR_SUCCESS = 0 Const REG_SZ = 1 ' 独立的空的终结字符串 Const REG_DWORD = 4 ' 32位数字Const gREGKEYSYSINFOLOC = "SOFTWARE\Microsoft\Shared Tools Location" Const gREGVALSYSINFOLOC = "MSINFO" Const gREGKEYSYSINFO = "SOFTWARE\Microsoft\Shared Tools\MSINFO" Const gREGVALSYSINFO = "PATH"Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As LongPrivate Sub Command1_Click() On Error GoTo SysInfoErr
Dim rc As Long Dim SysInfoPath As String
' 试图从注册表中获得系统信息程序的路径及名称... If GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFO, gREGVALSYSINFO, SysInfoPath) Then ' 试图仅从注册表中获得系统信息程序的路径... ElseIf GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFOLOC, gREGVALSYSINFOLOC, SysInfoPath) Then ' 已知32位文件版本的有效位置 If (Dir(SysInfoPath & "\MSINFO32.EXE") <> "") Then SysInfoPath = SysInfoPath & "\MSINFO32.EXE"
' 错误 - 文件不能被找到... Else GoTo SysInfoErr End If ' 错误 - 注册表相应条目不能被找到... Else GoTo SysInfoErr End If
Call Shell(SysInfoPath, vbNormalFocus)
Exit Sub SysInfoErr: MsgBox "此时系统信息不可用", vbOKOnly End Sub Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String, ByRef KeyVal As String) As Boolean Dim i As Long ' 循环计数器 Dim rc As Long ' 返回代码 Dim hKey As Long ' 打开的注册表关键字句柄 Dim hDepth As Long ' Dim KeyValType As Long ' 注册表关键字数据类型 Dim tmpVal As String ' 注册表关键字值的临时存储器 Dim KeyValSize As Long ' 注册表关键自变量的尺寸 '------------------------------------------------------------ ' 打开 {HKEY_LOCAL_MACHINE...} 下的 RegKey '------------------------------------------------------------ rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey) ' 打开注册表关键字
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' 处理错误...
GetKeyError: ' 错误发生后将其清除... KeyVal = "" ' 设置返回值到空字符串 GetKeyValue = False ' 返回失败 rc = RegCloseKey(hKey) ' 关闭注册表关键字 End Function
系统路径 Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long Private Sub Command1_Click() Dim sSave As String, Ret As Long 'Create a buffer sSave = Space(255) 'Get the system directory Ret = GetSystemDirectory(sSave, 255) 'Remove all unnecessary chr$(0)'s sSave = Left$(sSave, Ret) 'Show the windows directory MsgBox "Windows System directory: " + sSave End Sub
Option Explicit Private Declare Function GetSystemDirectory Lib "kernel32.dll" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long Private Sub Form_Load() Dim Length As Long Dim lpBuffer As String, nSize As Long nSize = &HFF lpBuffer = String$(nSize, Chr(0)) Length = GetSystemDirectory(lpBuffer, nSize) Me.Caption = Left$(lpBuffer, Length) End Sub
to: bygones2001 分数少,你完全可以不来嘛,既然来了看来还是想要这分数的吧? 我们大家到这里来不就是为了提高自己,帮助别人嘛? 嫌分少,就算我给你100分,你能解决得了嘛? 还是少说风凉话了吧!
UINT GetSystemDirectory(
LPTSTR lpBuffer, // address of buffer for system directory
UINT uSize // size of directory buffer
);
Dim strFolder As String
Dim lngResult As Long
strFolder = String(MAX_PATH, 0)
lngResult = GetWindowsDirectory(strFolder, MAX_PATH)
If lngResult <> 0 Then
GetWinPath = Left(strFolder, InStr(strFolder, Chr(0)) - 1)
Else
GetWinPath = ""
End If
End Function'在程序中使用方法如下:Private Sub Command1_Click()
Call MsgBox("您电脑中 Windows 目录的正确路径是: " & GetWinPath, vbInformation)
End Sub
Private Sub Form_Load() Dim sSave As String, Ret As Long
'Create a buffer
sSave = Space(255)
'Get the system directory
Ret = GetSystemDirectory(sSave, 255)
'Remove all unnecessary chr$(0)'s
sSave = Left$(sSave, Ret)
'Show the windows directory
MsgBox "Windows System directory: " + sSave
End Sub
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Sub Form_Load() Dim sSave As String, Ret As Long
'Create a buffer
sSave = Space(255)
'Get the system directory
Ret = GetSystemDirectory(sSave, 255)
'Remove all unnecessary chr$(0)'s
sSave = Left$(sSave, Ret)
'Show the windows directory
MsgBox "Windows System directory: " + sSave
End Sub
On Error GoTo SysInfoErr
Dim rc As Long
Dim SysInfoPath As String
' 试图从注册表中获得系统信息程序的路径及名称...
If GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFO, gREGVALSYSINFO, SysInfoPath) Then
' 试图仅从注册表中获得系统信息程序的路径...
ElseIf GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFOLOC, gREGVALSYSINFOLOC, SysInfoPath) Then
' 已知32位文件版本的有效位置
If (Dir(SysInfoPath & "\MSINFO32.EXE") <> "") Then
SysInfoPath = SysInfoPath & "\MSINFO32.EXE"
' 错误 - 文件不能被找到...
Else
GoTo SysInfoErr
End If
' 错误 - 注册表相应条目不能被找到...
Else
GoTo SysInfoErr
End If
Call Shell(SysInfoPath, vbNormalFocus)
Exit Sub
SysInfoErr:
MsgBox "此时系统信息不可用", vbOKOnly
End Sub
Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String, ByRef KeyVal As String) As Boolean
Dim i As Long ' 循环计数器
Dim rc As Long ' 返回代码
Dim hKey As Long ' 打开的注册表关键字句柄
Dim hDepth As Long '
Dim KeyValType As Long ' 注册表关键字数据类型
Dim tmpVal As String ' 注册表关键字值的临时存储器
Dim KeyValSize As Long ' 注册表关键自变量的尺寸
'------------------------------------------------------------
' 打开 {HKEY_LOCAL_MACHINE...} 下的 RegKey
'------------------------------------------------------------
rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey) ' 打开注册表关键字
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' 处理错误...
tmpVal = String$(1024, 0) ' 分配变量空间
KeyValSize = 1024 ' 标记变量尺寸
'------------------------------------------------------------
' 检索注册表关键字的值...
'------------------------------------------------------------
rc = RegQueryValueEx(hKey, SubKeyRef, 0, _
KeyValType, tmpVal, KeyValSize) ' 获得/创建关键字值
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' 处理错误
If (Asc(Mid(tmpVal, KeyValSize, 1)) = 0) Then ' Win95 外接程序空终结字符串...
tmpVal = Left(tmpVal, KeyValSize - 1) ' Null 被找到,从字符串中分离出来
Else ' WinNT 没有空终结字符串...
tmpVal = Left(tmpVal, KeyValSize) ' Null 没有被找到, 分离字符串
End If
'------------------------------------------------------------
' 决定转换的关键字的值类型...
'------------------------------------------------------------
Select Case KeyValType ' 搜索数据类型...
Case REG_SZ ' 字符串注册关键字数据类型
KeyVal = tmpVal ' 复制字符串的值
Case REG_DWORD ' 四字节的注册表关键字数据类型
For i = Len(tmpVal) To 1 Step -1 ' 将每位进行转换
KeyVal = KeyVal + Hex(Asc(Mid(tmpVal, i, 1))) ' 生成值字符。 By Char。
Next
KeyVal = Format$("&h" + KeyVal) ' 转换四字节的字符为字符串
End Select
GetKeyValue = True ' 返回成功
rc = RegCloseKey(hKey) ' 关闭注册表关键字
Exit Function ' 退出
GetKeyError: ' 错误发生后将其清除...
KeyVal = "" ' 设置返回值到空字符串
GetKeyValue = False ' 返回失败
rc = RegCloseKey(hKey) ' 关闭注册表关键字
End Function
完整的程序如下
Option Explicit' 注册表关键字安全选项...
Const READ_CONTROL = &H20000
Const KEY_QUERY_VALUE = &H1
Const KEY_SET_VALUE = &H2
Const KEY_CREATE_SUB_KEY = &H4
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const KEY_CREATE_LINK = &H20
Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
' 注册表关键字 ROOT 类型...
Const HKEY_LOCAL_MACHINE = &H80000002
Const ERROR_SUCCESS = 0
Const REG_SZ = 1 ' 独立的空的终结字符串
Const REG_DWORD = 4 ' 32位数字Const gREGKEYSYSINFOLOC = "SOFTWARE\Microsoft\Shared Tools Location"
Const gREGVALSYSINFOLOC = "MSINFO"
Const gREGKEYSYSINFO = "SOFTWARE\Microsoft\Shared Tools\MSINFO"
Const gREGVALSYSINFO = "PATH"Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As LongPrivate Sub Command1_Click()
On Error GoTo SysInfoErr
Dim rc As Long
Dim SysInfoPath As String
' 试图从注册表中获得系统信息程序的路径及名称...
If GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFO, gREGVALSYSINFO, SysInfoPath) Then
' 试图仅从注册表中获得系统信息程序的路径...
ElseIf GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFOLOC, gREGVALSYSINFOLOC, SysInfoPath) Then
' 已知32位文件版本的有效位置
If (Dir(SysInfoPath & "\MSINFO32.EXE") <> "") Then
SysInfoPath = SysInfoPath & "\MSINFO32.EXE"
' 错误 - 文件不能被找到...
Else
GoTo SysInfoErr
End If
' 错误 - 注册表相应条目不能被找到...
Else
GoTo SysInfoErr
End If
Call Shell(SysInfoPath, vbNormalFocus)
Exit Sub
SysInfoErr:
MsgBox "此时系统信息不可用", vbOKOnly
End Sub
Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String, ByRef KeyVal As String) As Boolean
Dim i As Long ' 循环计数器
Dim rc As Long ' 返回代码
Dim hKey As Long ' 打开的注册表关键字句柄
Dim hDepth As Long '
Dim KeyValType As Long ' 注册表关键字数据类型
Dim tmpVal As String ' 注册表关键字值的临时存储器
Dim KeyValSize As Long ' 注册表关键自变量的尺寸
'------------------------------------------------------------
' 打开 {HKEY_LOCAL_MACHINE...} 下的 RegKey
'------------------------------------------------------------
rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey) ' 打开注册表关键字
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' 处理错误...
tmpVal = String$(1024, 0) ' 分配变量空间
KeyValSize = 1024 ' 标记变量尺寸
'------------------------------------------------------------
' 检索注册表关键字的值...
'------------------------------------------------------------
rc = RegQueryValueEx(hKey, SubKeyRef, 0, _
KeyValType, tmpVal, KeyValSize) ' 获得/创建关键字值
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' 处理错误
If (Asc(Mid(tmpVal, KeyValSize, 1)) = 0) Then ' Win95 外接程序空终结字符串...
tmpVal = Left(tmpVal, KeyValSize - 1) ' Null 被找到,从字符串中分离出来
Else ' WinNT 没有空终结字符串...
tmpVal = Left(tmpVal, KeyValSize) ' Null 没有被找到, 分离字符串
End If
'------------------------------------------------------------
' 决定转换的关键字的值类型...
'------------------------------------------------------------
Select Case KeyValType ' 搜索数据类型...
Case REG_SZ ' 字符串注册关键字数据类型
KeyVal = tmpVal ' 复制字符串的值
Case REG_DWORD ' 四字节的注册表关键字数据类型
For i = Len(tmpVal) To 1 Step -1 ' 将每位进行转换
KeyVal = KeyVal + Hex(Asc(Mid(tmpVal, i, 1))) ' 生成值字符。 By Char。
Next
KeyVal = Format$("&h" + KeyVal) ' 转换四字节的字符为字符串
End Select
GetKeyValue = True ' 返回成功
rc = RegCloseKey(hKey) ' 关闭注册表关键字
Exit Function ' 退出
GetKeyError: ' 错误发生后将其清除...
KeyVal = "" ' 设置返回值到空字符串
GetKeyValue = False ' 返回失败
rc = RegCloseKey(hKey) ' 关闭注册表关键字
End Function
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Sub Command1_Click()
Dim sSave As String, Ret As Long
'Create a buffer
sSave = Space(255)
'Get the system directory
Ret = GetSystemDirectory(sSave, 255)
'Remove all unnecessary chr$(0)'s
sSave = Left$(sSave, Ret)
'Show the windows directory
MsgBox "Windows System directory: " + sSave
End Sub
Private Declare Function GetSystemDirectory Lib "kernel32.dll" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Sub Form_Load()
Dim Length As Long
Dim lpBuffer As String, nSize As Long
nSize = &HFF
lpBuffer = String$(nSize, Chr(0))
Length = GetSystemDirectory(lpBuffer, nSize)
Me.Caption = Left$(lpBuffer, Length)
End Sub
分数少,你完全可以不来嘛,既然来了看来还是想要这分数的吧?
我们大家到这里来不就是为了提高自己,帮助别人嘛?
嫌分少,就算我给你100分,你能解决得了嘛? 还是少说风凉话了吧!