65.9K
CodeProject 正在变化。 阅读更多。
Home

如何获取 Windows 目录、计算机名和系统信息详情

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.16/5 (14投票s)

2004 年 5 月 18 日

CPOL
viewsIcon

233883

downloadIcon

4204

这个小程序演示了如何获取 Windows 目录路径名、计算机名和系统信息详情。

Sample Image - SystemUtility.jpg

引言

这个工具可以帮助你理解如何从 Windows 系统(95、98、NT 和 XP)返回计算机系统名/Windows 系统路径和系统信息。

系统库文件

你需要将以下系统 DLL 文件包含到你的应用程序中。

Private Declare Function GetComputerName Lib "kernel32" _
    Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetWindowsDirectory Lib "kernel32.dll" _
    Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

GetTheComputerName 函数

以下 GetTheComputerName 函数从你的系统中返回计算机名。 了解代码如何与系统 DLL 文件一起工作。

Public Function GetTheComputerName() As String

    Dim strComputerName As String ' Variable to return the path of computer name
    
    strComputerName = Space(250)  ' Initialize the buffer to receive the string
    GetComputerName strComputerName, Len(strComputerName)
    strComputerName = Mid(Trim$(strComputerName), 1, Len(Trim$(strComputerName)) - 1)
    GetTheComputerName = strComputerName

End Function

GetTheWindowsDirectory 函数

以下 GetTheWindowsDirectory 函数从你的系统中返回 Windows 系统路径。 了解代码如何与系统 DLL 文件一起工作。

Public Function GetTheWindowsDirectory() As String 
    
    Dim strWindowsDir As String        ' Variable to return the path of Windows Directory
    Dim lngWindowsDirLength As Long    ' Variable to return the length of the path
    
    strWindowsDir = Space(250)         ' Initialize the buffer to receive the string
    lngWindowsDirLength = GetWindowsDirectory
	(strWindowsDir, 250) ' Read the path of the windows directory
    strWindowsDir = Left(strWindowsDir, 
	lngWindowsDirLength) ' Extract the windows path from the buffer
    
    GetTheWindowsDirectory = strWindowsDir 

显示系统信息

以下代码行将在 文本框 中显示所有系统环境变量。

Dim intInc As Integer
Dim strDisplay As String
    
Text1 = ""
' Here we start printing
For intInc = 1 To 35
    strDisplay = strDisplay & Environ(intInc)
    strDisplay = strDisplay & Space(5)
Next intInc
    
Text1 = strDisplay

结论

这个小程序演示了如何从 Windows DLL 文件调用系统函数并将其实现到你的系统中。 通过这个程序,你了解了如何调用 GetComputerName GetWindowsSystemDirectoryPath 信息。 此外,你还了解了使用 Environ 变量进行系统信息的重要性。 希望这对你有所帮助,即使你对系统句柄函数没有任何先验知识。

历史

  • 2004 年 5 月 17 日:初始发布
© . All rights reserved.