取得登入帳號

使用API

API名稱:GetUserName
動態連結程式庫: advapi32.dll

VB6入口點:GetUserNameA
VB.NET入口點: GetUserNameW


程式說明

取得目前登入帳號之名稱。


VB6程式碼

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Dim xstr As String
Dim max, rc As Integer

Private Sub Command1_Click()
    xstr = Space$(255)
    max = 255
    rc = GetUserName(xstr, max)
    Text1.Text = (Mid(xstr, 1, max))
End Sub

VB.NET程式碼

Imports System.Runtime.InteropServices
Imports System.Text

Public Class Form1
    <DllImport("advapi32.dll", EntryPoint:="GetUserNameW", SetLastError:=True)>
    Shared Function GetUserName(<MarshalAs(UnmanagedType.LPTStr)> sb As System.Text.StringBuilder,
         ByRef length As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Buffer = New StringBuilder(64)
        Dim nSize As Integer = 64
        GetUserName(Buffer, nSize)
        TextBox1.Text = Buffer.ToString()

    End Sub
End Class

下載