コード例 #1
0
        /// <summary>
        /// 通过NetBios获取MAC地址
        /// </summary>
        /// <returns></returns>
        public string GetMacAddressByNetBios()
        {
            string macAddress = "";

            try
            {
                string    addr = "";
                int       cb;
                ASTAT     adapter;
                NCB       Ncb = new NCB();
                char      uRetCode;
                LANA_ENUM lenum;

                Ncb.ncb_command = (byte)NCBCONST.NCBENUM;
                cb             = Marshal.SizeOf(typeof(LANA_ENUM));
                Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
                Ncb.ncb_length = (ushort)cb;
                uRetCode       = Win32API.Netbios(ref Ncb);
                lenum          = (LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(LANA_ENUM));
                Marshal.FreeHGlobal(Ncb.ncb_buffer);
                if (uRetCode != (short)NCBCONST.NRC_GOODRET)
                {
                    return("");
                }

                for (int i = 0; i < lenum.length; i++)
                {
                    Ncb.ncb_command  = (byte)NCBCONST.NCBRESET;
                    Ncb.ncb_lana_num = lenum.lana[i];
                    uRetCode         = Win32API.Netbios(ref Ncb);
                    if (uRetCode != (short)NCBCONST.NRC_GOODRET)
                    {
                        return("");
                    }

                    Ncb.ncb_command     = (byte)NCBCONST.NCBASTAT;
                    Ncb.ncb_lana_num    = lenum.lana[i];
                    Ncb.ncb_callname[0] = (byte)'*';
                    cb             = Marshal.SizeOf(typeof(ADAPTER_STATUS)) + Marshal.SizeOf(typeof(NAME_BUFFER)) * (int)NCBCONST.NUM_NAMEBUF;
                    Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
                    Ncb.ncb_length = (ushort)cb;
                    uRetCode       = Win32API.Netbios(ref Ncb);
                    adapter.adapt  = (ADAPTER_STATUS)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(ADAPTER_STATUS));
                    Marshal.FreeHGlobal(Ncb.ncb_buffer);

                    if (uRetCode == (short)NCBCONST.NRC_GOODRET)
                    {
                        if (i > 0)
                        {
                            addr += ":";
                        }
                        addr = string.Format("{0,2:X}:{1,2:X}:{2,2:X}:{3,2:X}:{4,2:X}:{5,2:X}",
                                             adapter.adapt.adapter_address[0],
                                             adapter.adapt.adapter_address[1],
                                             adapter.adapt.adapter_address[2],
                                             adapter.adapt.adapter_address[3],
                                             adapter.adapt.adapter_address[4],
                                             adapter.adapt.adapter_address[5]);
                    }
                }
                macAddress = addr.Replace(' ', '0');
            }
            catch
            {
            }

            return(macAddress);
        }
コード例 #2
0
 public static extern char Netbios(ref NCB ncb);