帮忙解释一下,为什么要向API GetUserName()函数传递255个空格啊?
sBuffer = Space$(255)
lSize = Len(sBuffer)
Call GetUserName(sBuffer, lSize)
sBuffer = Space$(255)
lSize = Len(sBuffer)
Call GetUserName(sBuffer, lSize)
解决方案 »
- ADO存储图片到sql数据库中,二进制流变量的表示
- VB全局变量问题
- 如何取得右下角任务栏的程序名称、路径?
- 用vba转换excel单元格格式出现问题,请高手指教,在线等待。
- 在2000下用vb6写的程序移植到xp或2003下,程序界面上的控件是不是自动显示为相应系统下控件的显示样式!
- 。。。。。。。。请问这句有什么错误呢。。。。?
- 请julysixth(嘿呀)来接分.谢谢他在http://expert.csdn.net/Expert/topic/2868/2868560.xml?temp=.6964838给我的帮助
- 帮忙查个小错误
- 谁有十六进制转化为二进制的函数?急!!!一定给分!!!
- maskedbox控件显示金额字段时如何右对齐?
- ///数据库加密问题,请教各位////
- 急,请帮忙,如何用VB创建FTP虚拟目录?
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
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
'This project needs a timer
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Timer1.Interval = 100
Timer1.Enabled = True
Dim strTemp As String, strUserName As String
'Create a buffer
strTemp = String(100, Chr$(0))
'Get the temporary path
GetTempPath 100, strTemp
'strip the rest of the buffer
strTemp = Left$(strTemp, InStr(strTemp, Chr$(0)) - 1) 'Create a buffer
strUserName = String(100, Chr$(0))
'Get the username
GetUserName strUserName, 100
'strip the rest of the buffer
strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1) 'Show the temppath and the username
MsgBox "Hello " + strUserName + Chr$(13) + "The temp. Path is " + strTemp
End Sub
Private Sub Timer1_Timer()
Dim Boo As Boolean
'Check if this form is minimized
Boo = IsIconic(Me.hwnd)
'Update the form's caption
Me.Caption = "Form minimized: " + Str$(Boo)
End Sub
------------------------------------------
回答:预先初始化存放用户名的变量nSize的长度.
'
'取当前WINDOWS用户名
'函数:UserName
'参数:
'返回值:当前WINDOWS用户名.
'例子:
Public Function UserName() As String
Dim Cn As String
Dim Ls As Long
Dim res As Long Cn = String$(255, Chr$(0))
Ls = 255
res = GetUserName(Cn, Ls)
If res <> 0 Then
UserName = Mid$(Cn, 1, InStr(Cn, Chr$(0)) - 1)
Else
UserName = ""
End If
End Function