获取用户名 [英] Get User Name

查看:104
本文介绍了获取用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好;

在多次尝试获取MS用户名失败后,以下代码在应用于表单上的按钮时有效。 但是,我想要做的是在表单上填充未绑定的文本框,并在将数据写入表格时使其可用。 
我一直在努力让这个工作时间太长,并且已经尝试过如此多的代码,位置和冰雹迭代,我已经失去了数量。 如果您有任何建议我肯定会感激。

After many unsuccessful attempts to get the MS User Name, the below code works when it's applied to a button on a form.  However, what I would like to do is populate an unbound text box on the form, and, have it available when writing data to a table.  I have been working on trying to get this working for way too many hours, and have tried so many iterations of code, placement, and hail-mary's I have lost count.  If you have any suggestions I would surely appreciate it.

谢谢。

_______________________________________

_______________________________________

'声明请致电mpr.dll。

  声明函数WNetGetUser Lib" mpr.dll" _
$
     别名"WNetGetUserA" (ByVal lpName As String,_

      ByVal lpUserName As String,lpnLength As Long)As long



   Const NoError = 0       '功能调用成功了<


   Sub GetUserName()
$


      '返回字符串的缓冲区大小。

      Const lpnLength As Integer = 255



      '获取返回缓冲区空间。

     昏暗状态为整数



      '用于获取用户信息。

      Dim lpName,lpUserName As String



      '将缓冲区大小常量分配给lpUserName。

      lpUserName = Space $(lpnLength + 1)



      '获取使用产品的人的登录名。

      status = WNetGetUser(lpName,lpUserName,lpnLength)



      '查看是否发生了错误。

     如果status = NoError则为
         '此行删除空字符。 C中的字符串为空 -

         '终止了。 Visual Basic中的字符串不是以空值终止的。

         '必须从要使用的C字符串中删除空字符。
         '干净利落在Visual Basic中。

         lpUserName = Left $(lpUserName,InStr(lpUserName,Chr(0)) - 1)

     否则



         '发生错误。

         MsgBox"无法获取名称。"&
        结束

     结束如果



      '显示登录到本机的人的姓名。

      MsgBox"登录此机器的人是:" &安培; lpUserName



  结束Sub

' Declare for call to mpr.dll.
   Declare Function WNetGetUser Lib "mpr.dll" _
      Alias "WNetGetUserA" (ByVal lpName As String, _
      ByVal lpUserName As String, lpnLength As Long) As Long

   Const NoError = 0       'The Function call was successful

   Sub GetUserName()

      ' Buffer size for the return string.
      Const lpnLength As Integer = 255

      ' Get return buffer space.
      Dim status As Integer

      ' For getting user information.
      Dim lpName, lpUserName As String

      ' Assign the buffer size constant to lpUserName.
      lpUserName = Space$(lpnLength + 1)

      ' Get the log-on name of the person using product.
      status = WNetGetUser(lpName, lpUserName, lpnLength)

      ' See whether error occurred.
      If status = NoError Then
         ' This line removes the null character. Strings in C are null-
         ' terminated. Strings in Visual Basic are not null-terminated.
         ' The null character must be removed from the C strings to be used
         ' cleanly in Visual Basic.
         lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
      Else

         ' An error occurred.
         MsgBox "Unable to get the name."
         End
      End If

      ' Display the name of the person logged on to the machine.
      MsgBox "The person logged on this machine is: " & lpUserName

   End Sub

推荐答案

发布的代码适用于32位版本的Office,但也是如此

The posted code works in 32 bit versions of Office, but there again so does

MsgBox"登录此计算机的人是:" &安培; Environ

MsgBox "The person logged on this machine is: " & Environ


(" UserName")
("UserName")

如果您使用的是64位Office,则必须处理Declare行。

If you are using 64 bit Office, you will have to address the Declare line.

如果你想从你的代码中获取代码的值,那么你应该把它变成一个返回值的函数,例如

If you want to get the value from your code to use elsewhere then you should make it into a Function that returns a value e.g.

Option Explicit
Declare Function WNetGetUser Lib "mpr.dll" _
                             Alias "WNetGetUserA" (ByVal lpName As String, _
                                                   ByVal lpUserName As String, lpnLength As Long) As Long

Const NoError = 0        'The Function call was successful

Public Function GetUserName() As String

' Buffer size for the return string.
Const lpnLength As Integer = 255

    ' Get return buffer space.
Dim status As Integer

    ' For getting user information.
Dim lpName, lpUserName As String

    ' Assign the buffer size constant to lpUserName.
    lpUserName = Space


(lpnLength + 1)

'获取使用产品的人的登录名。
status = WNetGetUser(lpName,lpUserName,lpnLength)

'查看是否发生错误。
如果status = NoError则
'此行删除空字符。 C中的字符串为null-
'终止。 Visual Basic中的字符串不以空值终止。
'必须从C字符串中删除空字符才能在Visual Basic中干净地使用
'。
lpUserName =左
(lpnLength + 1) ' Get the log-on name of the person using product. status = WNetGetUser(lpName, lpUserName, lpnLength) ' See whether error occurred. If status = NoError Then ' This line removes the null character. Strings in C are null- ' terminated. Strings in Visual Basic are not null-terminated. ' The null character must be removed from the C strings to be used ' cleanly in Visual Basic. lpUserName = Left


这篇关于获取用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆