[System.Runtime.InteropServices.DllImport("Kernel32")]
static extern unsafe bool GetComputerName(byte* lpBuffer, long* nSize);
or
[System.Runtime.InteropServices.DllImport("Kernel32", CharSet=CharSet.Auto)]
public static extern bool GetComputerName(StringBuilder buffer, ref uint size);
Declare Function GetComputerName Lib "kernel32" (ByVal lpBuffer As String, ByRef nMax As Integer) As Boolean
None.
The GetComputerName function retrieves the NetBIOS name of the local computer. This name is established at system startup, when the system reads it from the registry.
Make sure to go in to the build properties of your project and check "Allow Unsafe Code". Second version doesn't recquest this.
using System.Management;
using System.Runtime.InteropServices;
class Class1
{
[System.Runtime.InteropServices.DllImport("Kernel32")]
static extern unsafe bool GetComputerName(byte* lpBuffer, long* nSize);
[STAThread]
static void Main(string[] args)
{
byte[] buffor = new byte[512];
long size = buffor.Length;
unsafe
{
long* pSize = &size;
fixed (byte* pBuffor = buffor)
{
GetComputerName(pBuffor, pSize);
}
}
System.Text.Encoding textEnc = new System.Text.ASCIIEncoding();
System.Console.WriteLine("Computer name: {0}", textEnc.GetString(buffor));
}
}
Dim buff As String = Space(255)
Dim max As Integer = 255
Dim rc As Integer
rc = GetComputerName(buff,max)
' Now, max contains the total number of characters written to the buffer
MessageBox.Show( Mid(buff,1,max) )
System.Environment.MachineName will get you the netbios name.
<DllImport("Kernel32", CharSet:=CharSet.Unicode, EntryPoint:="GetComputerNameA", SetLastError:=True)> _
Private Function GetCompName(ByVal Puffer As StringBuilder, ByRef Laenge As IntPtr) As Integer
End Function
Private Declare Function GetComputerNameAlt Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As StringBuilder, ByVal nSize As Integer) As Integer
Declare Unicode Function GetCompName2 Lib "Kernel32" Alias "GetComputerNameA" (ByVal Puffer As StringBuilder, ByRef Laenge As IntPtr) As Integer
Sub Main()
Dim CompName As New StringBuilder()
Dim Ret As Integer
'Ret = GetComputerNameAlt(CompName, 100)
Ret = GetCompName2(CompName, 100)
Dim CompNameBytes() As Byte = Encoding.Unicode.GetBytes(CompName.ToString)
Dim CompNameStr As String = Encoding.Default.GetString(CompNameBytes)
Console.WriteLine("Der Computer heißt: {0}", CompNameStr)
Console.ReadLine()
End Sub