c#.net使用WinAPI: GetComputerName - 获取计算机名称

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace GetLocalIpApp
{
    
    class Program
    {
        
        [DllImport("kernel32.dll")]
        static extern bool GetComputerName(IntPtr p, ref int lpnSize);
        static void Main(string[] args)
        {
            string strLocalName = LocalComputerName();
            Console.WriteLine(strLocalName);
            Console.Read();
            
        }
        static string LocalComputerName()
        {
            IntPtr p = Marshal.AllocHGlobal(128);
            int len = 128;
            GetComputerName(p, ref len);
            string strName = Marshal.PtrToStringAnsi(p);
            //释放指针占用的内存
            Marshal.FreeHGlobal(p);
            return strName;
        }
    }
}


你可能感兴趣的:(c#.net使用WinAPI: GetComputerName - 获取计算机名称)