求在沒連網下,獲取MAC地址,最好是api類,快速的那種
uj5u.com熱心網友回復:
網上方法大把的。 百度搜“vb api獲得MAC地址”http://blog.csdn.net/bodybo/article/details/5768422
最簡單的辦法通過呼叫cmd命令GetMAC,獲得控制臺回傳結果的可以使用命令管道。
uj5u.com熱心網友回復:
不要cmd的,這些我都知道,網上都是在聯網狀態下才能得到,并且為第一網卡,也不要全部都顯示的,想只得到有線網卡uj5u.com熱心網友回復:
我得到接近我想法的代碼有問題,能幫我看一下嘛,
Option Explicit
Private Declare Function GetNetworkParams Lib "iphlpapi.dll" (FixedInfo As Any, pOutBufLen As Long) As Long
'Declare Function GetIfTable Lib "iphlpapi.dll" (ByRef pIfTable As MIB_IFTABLE, ByRef pdwSize As Long, _
' ByVal bOrder As Long) As Long
Private Declare Function GetIfTable Lib "IPHlpApi" (ByRef pIfTable As Any, ByRef pdwSize As Long, ByVal bOrder As Long) As Long
'Declare Function GetIfEntry Lib "iphlpapi.dll" (pIfRow As MIB_IFROW) As Long
Private Declare Function GetIfEntry Lib "iphlpapi.dll" (pIfRow As Any) As Long
Private Type MIB_IFROW '保存結果資訊
wszName(0 To 511) As Byte '介面名稱的Unicode字串,必須為512字節
dwIndex As Long '介面編號
dwType As Long '介面類型,參看IP_ADAPTER_INFO類型的Type成員
dwMtu As Long '最大傳輸單元
dwSpeed As Long '介面速度(字節)
dwPhysAddrLen As Long '由bPhysAddr獲得的物理地址有效長度
bPhysAddr(0 To 7) As Byte '物理地址
dwAdminStatus As Long '介面管理狀態
dwOperStatus As Long '操作狀態,以下值之一:
dwLastChange As Long '操作狀態最后改變的時間
dwInOctets As Long '總共收到(字節)
dwInUcastPkts As Long '總共收到(unicast包)
dwInNUcastPkts As Long '總共收到(non-unicast包),包括廣播包和多點傳送包
dwInDiscards As Long '收到后丟棄包總數(即使沒有錯誤)
dwInErrors As Long '收到出錯包總數
dwInUnknownProtos As Long '收到后因協議不明而丟棄的包總數
dwOutOctets As Long '總共發送(字節)
dwOutUcastPkts As Long '總共發送(unicast包)
dwOutNUcastPkts As Long '總共發送(non-unicast包),包括廣播包和多點傳送包
dwOutDiscards As Long '發送丟棄包總數(即使沒有錯誤)
dwOutErrors As Long '發送出錯包總數
dwOutQLen As Long '發送隊列長度
dwDescrLen As Long 'bDescr部分有效長度
bDescr(0 To 255) As Byte '介面描述
End Type
Private Type MIB_IFTABLE '包含結果表
dwNumEntries As Long '當前網絡介面的總數
MIB_Table(9) As MIB_IFROW '指向一個包含MIB_IFROW類型的指針
End Type
Sub Test()
Dim Net As MIB_IFTABLE
Dim LenIfT As Long
Dim RValue As Long
Dim I As Long
Dim XI As Long
Dim TXX As String
LenIfT = Len(Net)
RValue = GetIfTable(Net, LenIfT, True)
'由Net.dwNumEntries獲得介面數量,然后用for…next循環獲取每個介面資訊:
For I = 1 To Net.dwNumEntries - 1
With Net.MIB_Table(I)
Debug.Print
Debug.Print "----" & Time & "----"
Debug.Print "介面名稱:" & StrConv(.wszName, vbUnicode)
Debug.Print "介面描述:" & StrConv(.bDescr, vbUnicode)
Debug.Print "介面編號:" & .dwIndex
Debug.Print "介面速度:" & .dwSpeed
Debug.Print "介面狀態:" & .dwAdminStatus
Debug.Print "操作狀態:" & .dwOperStatus
Debug.Print "收到字節:" & .dwInOctets
For XI = 1 To .dwPhysAddrLen - 1
TXX = TXX & Format(Hex(.bPhysAddr(XI)), "00")
Next
If TXX <> "" Then Debug.Print "網卡地址:"; TXX
End With
Next
End Sub
Private Sub Command1_Click()
Test
End Sub
輸出
----下午 01:41:29----
介面名稱:
介面描述:VMware Virtual Ethernet Adapter for VMnet8
介面編號:2
介面速度:100000000
介面狀態:1
操作狀態:5
收到字節:39580119
網卡地址:5056C00008
----下午 01:41:29----
介面名稱:
介面描述:VMware Virtual Ethernet Adapter for VMnet1
介面編號:3
介面速度:100000000
介面狀態:1
操作狀態:5
收到字節:39576163
網卡地址:5056C000085056C00001
----下午 01:41:29----
介面名稱:
介面描述:Intel(R) PRO/100 VE Network Connection - 資料包計劃程式微型埠
介面編號:4
介面速度:100000000
介面狀態:1
操作狀態:5
收到字節:577268166
網卡地址:5056C000085056C0000116767C5633
uj5u.com熱心網友回復:
這個代碼RValue 回傳122,得不到如上的資訊,請大家幫助除錯,我的是win7系統,謝謝uj5u.com熱心網友回復:
用wmi沒那么復雜吧?uj5u.com熱心網友回復:
用 C 代碼寫一個 DLL 比較省事。下面是說明和示例代碼。To get the Media Access Control (MAC) address for an ethernet adapter programmatically, use the Netbios() NCBASTAT command and provide a "*" as the name in the NCB.ncb_CallName field. This is demonstrated in the sample code below.
For computers with multiple network adapters you need to enumerate the LANA numbers and perform the NCBASTAT command on each. Even when you have a single network adapter, it is a good idea to enumerate valid LANA numbers first and perform the NCBASTAT on one of the valid LANA numbers. It is considered bad programming to hardcode the LANA number to 0.
In addition, other hardware and software may be assigned their own MAC addresses. For example, a modem can have a MAC address. Also, a RAS client or server can install "dummy" network adapters that correspond to a dialup or serial connection. Normally, these MAC addresses are randomly generated. If an adapter status is called on a LANA that corresponds to one of these adapters when no connection is present, Netbios returns error 0x34 (NRC_ENVNOTDEF) even if a reset was previously performed.
With the NetBEUI and IPX transports, the same information can be obtained at a command prompt by using:
net config workstation
The ID given is the MAC address.
The following code enumerates all LANA numbers, performs a reset (NCBREST), and an adapter status (NCBASTAT).
#include <windows.h>
#include <wincon.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
typedef struct _ASTAT_
{
ADAPTER_STATUS adapt;
NAME_BUFFER NameBuff [30];
}ASTAT, * PASTAT;
ASTAT Adapter;
void main (void)
{
NCB Ncb;
UCHAR uRetCode;
char NetName[50];
LANA_ENUM lenum;
int i;
memset( &Ncb, 0, sizeof(Ncb) );
Ncb.ncb_command = NCBENUM;
Ncb.ncb_buffer = (UCHAR *)&lenum;
Ncb.ncb_length = sizeof(lenum);
uRetCode = Netbios( &Ncb );
printf( "The NCBENUM return code is: 0x%x \n", uRetCode );
for(i=0; i < lenum.length ;i++)
{
memset( &Ncb, 0, sizeof(Ncb) );
Ncb.ncb_command = NCBRESET;
Ncb.ncb_lana_num = lenum.lana[i];
uRetCode = Netbios( &Ncb );
printf( "The NCBRESET on LANA %d return code is: 0x%x \n",
lenum.lana[i], uRetCode );
memset( &Ncb, 0, sizeof (Ncb) );
Ncb.ncb_command = NCBASTAT;
Ncb.ncb_lana_num = lenum.lana[i];
strcpy( Ncb.ncb_callname, "* " );
Ncb.ncb_buffer = (char *) &Adapter;
Ncb.ncb_length = sizeof(Adapter);
uRetCode = Netbios( &Ncb );
printf( "The NCBASTAT on LANA %d return code is: 0x%x \n",
lenum.lana[i], uRetCode );
if ( uRetCode == 0 )
{
printf( "The Ethernet Number on LANA %d is:
%02x%02x%02x%02x%02x%02x\n",
lenum.lana[i],
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] );
}
}
}
uj5u.com熱心網友回復:
都是大俠,謝謝。uj5u.com熱心網友回復:
簡單的方法: dos命令: ipconfig/all 即可不需要聯網, 拔了網線也可以顯示. 有圖為證:
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/109553.html
標籤:API
