【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
展开
 我来答
KL1112
2012-01-14 · TA获得超过532个赞
知道小有建树答主
回答量:454
采纳率:0%
帮助的人:629万
展开全部
'''''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
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式