【vb6 获取 桌面名 问题】
'为什么程序运行到第二个GetUserObjectInformation那里,vb就停止工作了。OptionExplicitPrivateConstUOI_NAMEAsL...
'为什么程序运行到第二个GetUserObjectInformation那里,vb就停止工作了。
Option Explicit
Private Const UOI_NAME As Long = 2
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private Declare Function GetThreadDesktop Lib "User32" (ByVal dwThread As Long) As Long
Private Declare Function GetUserObjectInformation Lib "User32" Alias "GetUserObjectInformationA" (ByVal hObj As Long, ByVal nIndex As Long, pvInfo As Any, ByVal nLength As Long, lpnLengthNeeded As Long) As Long
Private Sub Command1_Click()
Dim CurrentDesktopHandle As Long, CurrentDesktopName As String, lLengthNeeded As Long
Debug.Print "当前线程ID=" & GetCurrentThreadId
CurrentDesktopHandle = GetThreadDesktop(GetCurrentThreadId)
Debug.Print "当前桌面句柄=" & CurrentDesktopHandle
Call GetUserObjectInformation(CurrentDesktopHandle, UOI_NAME, vbNullString, 0, lLengthNeeded)
CurrentDesktopName = Space(lLengthNeeded)
If GetUserObjectInformation(CurrentDesktopHandle, UOI_NAME, CurrentDesktopName, lLengthNeeded, lLengthNeeded) Then
Debug.Print "获取当前桌面名 成功!"
Debug.Print "CurrentDesktopName=" & CurrentDesktopName
Debug.Print "lLengthNeeded=" & lLengthNeeded
Else
Debug.Print "获取当前桌面名 失败!"
Debug.Print "lLengthNeeded=" & lLengthNeeded
End If
End Sub 展开
Option Explicit
Private Const UOI_NAME As Long = 2
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private Declare Function GetThreadDesktop Lib "User32" (ByVal dwThread As Long) As Long
Private Declare Function GetUserObjectInformation Lib "User32" Alias "GetUserObjectInformationA" (ByVal hObj As Long, ByVal nIndex As Long, pvInfo As Any, ByVal nLength As Long, lpnLengthNeeded As Long) As Long
Private Sub Command1_Click()
Dim CurrentDesktopHandle As Long, CurrentDesktopName As String, lLengthNeeded As Long
Debug.Print "当前线程ID=" & GetCurrentThreadId
CurrentDesktopHandle = GetThreadDesktop(GetCurrentThreadId)
Debug.Print "当前桌面句柄=" & CurrentDesktopHandle
Call GetUserObjectInformation(CurrentDesktopHandle, UOI_NAME, vbNullString, 0, lLengthNeeded)
CurrentDesktopName = Space(lLengthNeeded)
If GetUserObjectInformation(CurrentDesktopHandle, UOI_NAME, CurrentDesktopName, lLengthNeeded, lLengthNeeded) Then
Debug.Print "获取当前桌面名 成功!"
Debug.Print "CurrentDesktopName=" & CurrentDesktopName
Debug.Print "lLengthNeeded=" & lLengthNeeded
Else
Debug.Print "获取当前桌面名 失败!"
Debug.Print "lLengthNeeded=" & lLengthNeeded
End If
End Sub 展开
1个回答
展开全部
'''''ByVal CurrentDesktopName
If GetUserObjectInformation(CurrentDesktopHandle, UOI_NAME, ByVal CurrentDesktopName, lLengthNeeded, lLengthNeeded) Then
没必要调用两次GetUserObjectInformation吧。一般字符串长度不会超过255。另外lLengthNeeded的值也有问题,总是多1。
lLengthNeeded = 255: CurrentDesktopName = Space(lLengthNeeded)
If GetUserObjectInformation(CurrentDesktopHandle, UOI_NAME, ByVal CurrentDesktopName, lLengthNeeded, lLengthNeeded) Then
'lLengthNeeded = lLengthNeeded - 1
'CurrentDesktopName = Left(CurrentDesktopName, lLengthNeeded)
CurrentDesktopName = Left(CurrentDesktopName, InStr(CurrentDesktopName, Chr(0)) - 1)
lLengthNeeded = Len(CurrentDesktopName)
Debug.Print "获取当前桌面名 成功!"
Debug.Print "CurrentDesktopName=" & CurrentDesktopName
Debug.Print "lLengthNeeded=" & lLengthNeeded
Else
Debug.Print "获取当前桌面名 失败!"
Debug.Print "lLengthNeeded=" & lLengthNeeded
End If
If GetUserObjectInformation(CurrentDesktopHandle, UOI_NAME, ByVal CurrentDesktopName, lLengthNeeded, lLengthNeeded) Then
没必要调用两次GetUserObjectInformation吧。一般字符串长度不会超过255。另外lLengthNeeded的值也有问题,总是多1。
lLengthNeeded = 255: CurrentDesktopName = Space(lLengthNeeded)
If GetUserObjectInformation(CurrentDesktopHandle, UOI_NAME, ByVal CurrentDesktopName, lLengthNeeded, lLengthNeeded) Then
'lLengthNeeded = lLengthNeeded - 1
'CurrentDesktopName = Left(CurrentDesktopName, lLengthNeeded)
CurrentDesktopName = Left(CurrentDesktopName, InStr(CurrentDesktopName, Chr(0)) - 1)
lLengthNeeded = Len(CurrentDesktopName)
Debug.Print "获取当前桌面名 成功!"
Debug.Print "CurrentDesktopName=" & CurrentDesktopName
Debug.Print "lLengthNeeded=" & lLengthNeeded
Else
Debug.Print "获取当前桌面名 失败!"
Debug.Print "lLengthNeeded=" & lLengthNeeded
End If
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询