表紙へ

5.コンピュータ名を取得する

 

次のComputerName関数を呼び出すことによりユーザー名を取得できる。

Private Function ComputerName() As String

    Dim Name As String
    Name = Space(255)
    Call GetComputerName(Name, Len(Name))
    ComputerName = Left(Name, InStr(1, Name, Chr(0)) - 1)

End Function

この関数を機能させるためには宣言部に次のようにAPI関数の宣言をする必要がある。

Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
 

 

Microsoft社公式 API関数 GetComputerNameの解説 を見る