선언:

C#

[DllImport("kernel32", CharSet = CharSet.Auto)]
public static extern Int32 GetComputerName(String Buffer, ref Int32 BufferLength);


VB.NET

<DllImport("kernel32", CharSet := CharSet.Auto)> _
Public Shared Function GetComputerName(Buffer As StringByRef BufferLength As Int32) As Int32
End Function




사용 예제:

C#

using System;
using System.Runtime.InteropServices;

namespace ApiReference {
    class ApiExample {
        [DllImport("kernel32", CharSet = CharSet.Auto)]
        public static extern Int32 GetComputerName(String Buffer, ref Int32 BufferLength);
        
        public static void Main(string[] args) {
            Int32 bufferLength = 16;
            String comName = new string(' ', bufferLength);
            GetComputerName(comName, ref bufferLength);
            comName = comName.Trim();
            Console.WriteLine("Computer Name:");
            Console.WriteLine("  {0} ({1})", comName, bufferLength);
            Console.ReadKey(true);
        }
    }
}


VB.NET

Imports System
Imports System.Runtime.InteropServices

Namespace ApiReference
    Class ApiExample
        <DllImport("kernel32", CharSet := CharSet.Auto)> _
        Public Shared Function GetComputerName(Buffer As StringByRef BufferLength As Int32) As Int32
        End Function

        Public Shared Sub Main(args As String())
            Dim bufferLength As Int32 = 16
            Dim comName As String = New String(" "C, bufferLength)
            GetComputerName(comName, bufferLength)
            comName = comName.Trim()
            Console.WriteLine("Computer Name:")
            Console.WriteLine("  {0} ({1})", comName, bufferLength)
            Console.ReadKey(True)
        End Sub
    End Class
End Namespace




예제 실행 결과:




매개 변수 설명:

Buffer - 컴퓨터의 이름이 저장될 변수를 입력합니다.

BufferLength - 컴퓨터의 이름이 저장될 변수의 길이가 입력된 변수를 입력합니다. 추가적으로, API 호출 성공 시엔 버퍼에 쓰여진 값으로 설정됩니다.




API 설명:

컴퓨터의 이름을 가져옵니다.




참고:

GetComputerName (MSDN)




비고:

컴퓨터 시작 시에 설정된 NetBIOS 이름을 가져옵니다. 다른 이름, 예를 들면 DNS 같은 이름을 가져오기 위해서는 GetComputerNameEx API를 사용해야 합니다.



'API Reference' 카테고리의 다른 글

37. SYSTEMTIME  (0) 2014.10.30
36. SHGetFolderPath  (0) 2014.10.29
34. GetWindowsDirectory  (0) 2014.10.24
33. GetSystemDirectory  (0) 2014.10.23
32. SendMessage  (1) 2014.10.11

+ Recent posts