VB声明
Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByVal lpVersionInformation As OSVERSIONINFO) As Long
说明
在一个OSVERSIONINFO结构中载入与平台和操作系统有关的版本信息
返回值
Long,非零表示成功,零表示失败
参数表
参数 类型及说明
lpVersionInformation OSVERSIONINFO,用于装载版本信息的结构。在正式调用函数之前,必须先将这个结构的dwOSVersionInfoSize字段设为结构的大小(148)
OSVERSIONINFO
类型定义
Type OSVERSIONINFO ' 148 Bytes
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
说明
This structure contains operating system version information. This structure is used with the GetVersionEx function.
字段表
字段 类型与说明
dwOSVersionInfoSize Long,Size of this data structure, currently at 148 bytes. This field must be set before calling the GetVersionEx function.
dwMajorVersion Long,Specifies the major and minor version number of the operating system.
dwMinorVersion
dwBuildNumber Long,Specifies the build number of the operating system. On Windows 95, build 1000 respresents OEM service release #2.
dwPlatformId Long,Specifies the platform supported by the operating system. May be one of the following:VER_PLATFORM_WIN32s: Win32s on Windows 3.1.VER_PLATFORM_WIN32_WINDOWS: Win32 on Windows 95.VER_PLATFORM_WIN32_NT: Windows NT.
szCSDVersion String,Contains additional information about the operating system
MSDN:
GetVersionEx
The GetVersionEx function obtains extended information about the version of the operating system that is currently running.
BOOL GetVersionEx(
LPOSVERSIONINFO lpVersionInformation // pointer to version
// information structure
);
Parameters
lpVersionInformation
Pointer to an OSVERSIONINFO data structure that the function fills with operating system version information.
Before calling the GetVersionEx function, set the dwOSVersionInfoSize member of the OSVERSIONINFO data structure to sizeof(OSVERSIONINFO).
Windows NT 5.0 and later: This member can be a pointer to an OSVERSIONINFOEX structure. Set the dwOSVersionInfoSize member to sizeof(OSVERSIONINFOEX) to identify the type of structure.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, callGetLastError. The function fails if you specify an invalid value for the dwOSVersionInfoSize member of the OSVERSIONINFO or OSVERSIONINFOEX structure.
Remarks
When using the GetVersionEx function to determine whether your application is running on a particular version of the operating system, check for version numbers that are greater than or equal to the desired version numbers. This ensures that the test succeeds for later versions of the operating system. For example, if your application requires Windows 98, use the following test:
Identifying the current operating system is usually not the best way to determine whether a particular operating system feature is present. This is because the operating system may have had new features added in a redistributable DLL. Rather than using GetVersionEx to determine the operating system platform or version number, test for the presence of the feature itself.
To determine the best way to test for a feature, refer to the documentation for the feature of interest. The following list discusses some common techniques for feature detection:
You can test for the presence of the functions associated with a feature. To test for the presence of a function in a system DLL, call theLoadLibrary function to load the DLL. Then call theGetProcAddress function to determine whether the function of interest is present in the DLL. Use the pointer returned by GetProcAddress to call the function. Note that even if the function is present, it may be a stub that just returns an error code such as ERROR_CALL_NOT_IMPLEMENTED.
You can determine the presence of some features by using the GetSystemMetrics function. For example, you can detect multiple display monitors by calling GetSystemMetrics(SM_CMONITORS).
There are several versions of the redistributable DLLs that implement shell and common control features. For information about determining which versions are present on the system your application is running on, see the topicShell Versions.
Windows CE: The value of the dwPlatformID member of the OSVERSIONINFO structure will be VER_PLATFORM_WIN32_CE.
QuickInfo
Windows NT: Requires version 3.5 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winbase.h.
Import Library: Use kernel32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT.
OSVERSIONINFO
The OSVERSIONINFO data structure contains operating system version information. The information includes major and minor version numbers, a build number, a platform identifier, and descriptive text about the operating system. This structure is used with the GetVersionEx function.
Members
dwOSVersionInfoSize
Specifies the size, in bytes, of this data structure. Set this member to sizeof(OSVERSIONINFO) before calling the GetVersionEx function.
dwMajorVersion
Identifies the major version number of the operating system. For example, for Windows NT version 3.51, the major version number is 3; and for Windows NT version 4.0, the major version number is 4.
dwMinorVersion
Identifies the minor version number of the operating system. For example, for Windows NT version 3.51, the minor version number is 51; and for Windows NT version 4.0, the minor version number is 0.
dwBuildNumber
Windows NT: Identifies the build number of the operating system.
Windows 95: Identifies the build number of the operating system in the low-order word. The high-order word contains the major and minor version numbers.
dwPlatformId
Identifies the operating system platform. This member can be one of the following values: Value Platform
VER_PLATFORM_WIN32s Win32s on Windows 3.1.
VER_PLATFORM_WIN32_WINDOWS Win32 on Windows 95 or Windows 98.
For Windows 95, dwMinorVersion is zero.
For Windows 98, dwMinorVersion is greater than zero.
VER_PLATFORM_WIN32_NT Win32 on Windows NT.
szCSDVersion
Windows NT: Contains a null-terminated string, such as "Service Pack 3", that indicates the latest Service Pack installed on the system. If no Service Pack has been installed, the string is empty.
Windows 95: Contains a null-terminated string that provides arbitrary additional information about the operating system.
QuickInfo
Windows NT: Requires version 3.5 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winbase.h.
Unicode: Defined as Unicode and ANSI structures.
goodname008(卢培培,LPP Software)的方法太麻烦了
试试这个:
Private Declare Function SHGetFolderPath Lib "shfolder.dll" Alias "SHGetFolderPathA" (ByVal hwndOwner As Long, _
ByVal nFolder As Long, ByVal hToken As Long, ByVal dwReserved As Long, ByVal lpszPath As String) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Const CSIDL_DESKTOP = &H0
Private Const SHGFP_TYPE_CURRENT = &H0
Function GetMyFolderPath() As String
Dim sBuf As String
sBuf = Space$(255)
SHGetFolderPath GetDesktopWindow, CSIDL_DESKTOP, 0&, SHGFP_TYPE_CURRENT, sBuf
sBuf = Trim$(sBuf)
sBuf = Left$(sBuf, Len(sBuf) - 1)
GetMyFolderPath = sBuf
End Function
Private Sub Command1_Click()
Dim X As String
X = GetMyFolderPath
MsgBox X
End Sub
Option Explicit
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128 ' Maintenance string for PSS usage
osName As String ' 我自己加的, 操作系统的名称
End Type
Private Sub Command1_Click()
Dim v As OSVERSIONINFO
v = GetWindowsVersion()
MsgBox v.osName
End Sub
' 获得 Windows 操作系统的版本
' OSVERSIONINFO 结构中的 osName 返回操作系统的名称
Private Function GetWindowsVersion() As OSVERSIONINFO
Dim ver As OSVERSIONINFO
ver.dwOSVersionInfoSize = 148
GetVersionEx ver
With ver
Select Case .dwPlatformId
Case 1
Select Case .dwMinorVersion
Case 0
.osName = "Windows 95"
Case 10
.osName = "Windows 98"
Case 90
.osName = "Windows Mellinnium"
End Select
Case 2
Select Case .dwMajorVersion
Case 3
.osName = "Windows NT 3.51"
Case 4
.osName = "Windows NT 4.0"
Case 5
If .dwMinorVersion = 0 Then
.osName = "Windows 2000"
Else
.osName = "Windows XP"
End If
End Select
Case Else
.osName = "Failed"
End Select
End With
GetWindowsVersion = ver
End Function
VB声明
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
说明
取得当前用户的名字
返回值
Long,TRUE(非零)表示成功,否则返回零。会设置GetLastError
参数表
参数 类型及说明
lpBuffer String,一个字串缓冲区,预先初始化成由nSize指定的长度。它将用于容纳用户名
nSize Long,初始化成lpBuffer的长度。返回以后,它会包含载入lpBuffer的字符数量
示例
Dim s$, cnt&, dl&
cnt& = 199
s$ = String$(200,0)
dl& = GetUserName(s$, cnt)
Debug.Print Left$(s$, cnt); cnt
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字串的最大长度