473,322 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How can I be sure I'm reading a MAC address hard-coded on a NIC?

MLH
It has been mentioned that a ghosted machine or a machine linked
by cable modem and USB may result in my reading a MAC address
other than one burned onto a NIC in the machine. That being the
case, I would have a problem.

I am using the following code. Blindly, I grabbed it from an old post
in this forum. It worked for me on one machine. I didn't give much
thought to situations in which I might be reading unreliable data.

If you know something about this sort of thing, please glance at the
code I'm using and tell me your thoughts. The following class module
resides in and is launched from within a form in A97...

Private Sub RetrieveMACaddresses()
'This sub demonstrates the use of fGetSecurityCode
Dim theAddress() As String
Dim i As Integer
Dim retVal
retVal = fGetSecurityCode(theAddress)
If retVal = MACOk Then
For i = 0 To UBound(theAddress)
Debug.Print theAddress(i)
Next i
Else
Select Case retVal
Case errMAC16bit
MsgBox "This is a 16 bit Netbios implementation,
function is meant for 32 bit!", vbOKOnly + vbInformation
Case errMACNoNetBios
MsgBox "No network card or NetBios not installed!",
vbOKOnly + vbInformation
Case errMACUnknown
MsgBox "Unknow error in Netbios call!", vbOKOnly +
vbInformation
Case errMACnoMACerror
'The function has already reported a VBA or Jet error,
no MsgBox needed
Case Else
'stumped!?
End Select
End If
End Sub
Then, of course, there's the supporting code in a standard module
that looks like this...

Option Compare Database
Option Explicit

Option Base 0
Public Const errMACNoNetBios As Integer = 1
Public Const errMAC16bit As Integer = 2
Public Const errMACUnknown As Integer = 3
Public Const errMACnoMACerror = 4
Public Const MACOk As Integer = 0

Public Const NCBASTAT As Long = &H33
Public Const NCBNAMSZ As Long = 16
Public Const NCBRESET As Long = &H32
Public Const NCBENUM = &H37 ' NCB ENUMERATE LANA NUMBERS

Public Const MAX_LANA = 254
Public Type LANA_ENUM
Length As Byte
lana(MAX_LANA) As Byte
End Type


'- Hide quoted text -
'- Show quoted text -

Public Type NET_CONTROL_BLOCK 'NCB
ncb_command As Byte
ncb_retcode As Byte
ncb_lsn As Byte
ncb_num As Byte
ncb_buffer As Long
ncb_length As Integer
ncb_callname As String * NCBNAMSZ
ncb_name As String * NCBNAMSZ
ncb_rto As Byte
ncb_sto As Byte
ncb_post As Long
ncb_lana_num As Byte
ncb_cmd_cplt As Byte
ncb_reserve(9) As Byte ' Reserved, must be 0
ncb_event As Long
End Type
Public Type ADAPTER_STATUS
adapter_address(5) As Byte
rev_major As Byte
reserved0 As Byte
adapter_type As Byte
rev_minor As Byte
duration As Integer
frmr_recv As Integer
frmr_xmit As Integer
iframe_recv_err As Integer
xmit_aborts As Integer
xmit_success As Long
recv_success As Long
iframe_xmit_err As Integer
recv_buff_unavail As Integer
t1_timeouts As Integer
ti_timeouts As Integer
Reserved1 As Long
free_ncbs As Integer
max_cfg_ncbs As Integer
max_ncbs As Integer
xmit_buf_unavail As Integer
max_dgram_size As Integer
pending_sess As Integer
max_cfg_sess As Integer
max_sess As Integer
max_sess_pkt_size As Integer
name_count As Integer
End Type
Public Type NAME_BUFFER
name As String * NCBNAMSZ
name_num As Integer
name_flags As Integer
End Type
Public Type ASTAT
adapt As ADAPTER_STATUS
NameBuff(30) As NAME_BUFFER
End Type
Public Declare Function Netbios Lib "netapi32.dll" _
(pncb As NET_CONTROL_BLOCK) As Byte
Function fGetSecurityCode(MACaddress() As String) As Integer
Dim structAST As ASTAT
Dim structLana As LANA_ENUM
Dim structNCB As NET_CONTROL_BLOCK

Dim retVal As Byte
Dim strHex As String
Dim strErr As String
Dim i As Integer, j As Integer
On Error GoTo Err_Handler
structNCB.ncb_command = NCBENUM
structNCB.ncb_buffer = VarPtr(structLana)
structNCB.ncb_length = Len(structLana)
retVal = Netbios(structNCB)
If retVal <> 0 Then
'NCBENUM is not available in the 16 bit dll
'should only occur if you change "netapi32.dll" in the API
declaration
fGetSecurityCode = errMAC16bit
GoTo Done_function
End If
If structLana.Length = 0 Then
'No network card or NetBios not installed
fGetSecurityCode = errMACNoNetBios
GoTo Done_function
Else
ReDim MACaddress(structLana.Length - 1)
For i = 0 To structLana.Length - 1
structNCB.ncb_lana_num = structLana.lana(i)
structNCB.ncb_command = NCBRESET
retVal = Netbios(structNCB)
'All should be Ok if we've come this far, but we'll check
anyway
If retVal <> 0 Then
fGetSecurityCode = errMACUnknown
GoTo Done_function
End If
structNCB.ncb_command = NCBASTAT
structNCB.ncb_buffer = VarPtr(structAST)
structNCB.ncb_lana_num = structLana.lana(i)
structNCB.ncb_length = Len(structAST)
structNCB.ncb_callname = "*"
retVal = Netbios(structNCB)
If retVal = 0 Then
'All's well
fGetSecurityCode = MACOk
For j = 0 To 5
strHex = Hex$(structAST.adapt.adapter_address(j))
If Len(Trim$(strHex)) = 1 Then strHex = "0" +
Trim$(strHex)
MACaddress(i) = MACaddress(i) & strHex
Next j
Else
fGetSecurityCode = errMACUnknown
GoTo Done_function
End If
Next i
End If
Done_function:
Exit Function

Err_Handler:
'This error is not caused by any illegal NetBios calls
fGetSecurityCode = errMACnoMACerror
strErr = Str(Err.Number) & ":" & Err.Description
MsgBox Err.Number
Resume Done_function
End Function
Function ReadHardwareSecurityCode()
'************************************************* ******************
' Read hardware security code.
'************************************************* ******************
On Error GoTo Err_ReadHardwareSecurityCode_Click
Dim ThisFN As String, MyMsg As String
ThisFN = "ReadHardwareSecurityCode"

100 Dim theCode() As String
110 Dim i As Integer
120 Dim retVal

130 retVal = fGetSecurityCode(theCode)
140 If retVal = MACOk Then
150 For i = 0 To UBound(theCode) 'UBound returns a Long
containing the largest available subscript for the indicated dimension
of an array.
160 SecureID = CStr(theCode(i))
170 Next i
180 Else
190 Select Case retVal
Case errMAC16bit
MyMsg = "This is a 16 bit Netbios implementation,
function is meant for 32 bit!"
MsgBox MyMsg & MyApp$ & ", rev. " & MY_VERSION$
Case errMACNoNetBios
MyMsg = "No network card or NetBios not
installed!"
MsgBox MyMsg & MyApp$ & ", rev. " & MY_VERSION$
Case errMACUnknown
MyMsg = "Unknow error in Netbios call!"
MsgBox MyMsg & MyApp$ & ", rev. " & MY_VERSION$
Case errMACnoMACerror
'The function has already reported a VBA or Jet error,
no MsgBox needed
Case Else
'stumped!?
200 End Select
210 End If
220 ReadHardwareSecurityCode = retVal

Exit_ReadHardwareSecurityCode_Click:
Exit Function

Err_ReadHardwareSecurityCode_Click:
Dim r As String, k As String, Message3 As String
r = "The following unexpected error occurred in line #" & Erl & "
in FN ReadHardwareSecurityCode, in " & ThisFN & " standard module."
k = CRLF & CRLF & Str$(Err) & ": " & Quote & Error$ & Quote
Message3 = r & k
MsgBox Message3, 48, "Unexpected Error - " & MyApp$ & ", rev. " &
MY_VERSION$
Resume Exit_ReadHardwareSecurityCode_Click

End Function

Nov 13 '05 #1
4 1820
MLH
I think David Fenton has answered this in another
thread. Perhaps all is as simple as allowing ipconfig
to write it to disk for me using ipconfig /all. I really
think it'll work. Gonna give that a try.
Nov 13 '05 #2
MLH
Excuse my brain mishap - that was David
Schofield.
Nov 13 '05 #3
On Sat, 02 Jul 2005 22:11:59 -0400, MLH <CR**@NorthState.net> wrote:
Excuse my brain mishap - that was David
Schofield.

Hi
I think it was both of us.
Don't you ever sleep?
David
Nov 13 '05 #4
MLH
I just looked and you're right.
Hats off to both Davids. Thx.
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Keith Dewell | last post by:
Greetings! My current job has brought me back to working in C++ which I haven't used since school days. The solution to my problem may be trivial but I have struggled with it for the last two...
24
by: Hendrik Schober | last post by:
Hi, I have a 'std::istream' and need to read its whole contents into a string. How can I do this? TIA; Schobi
2
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
3
by: roaher | last post by:
hi I have some trouble reading this macro: #define SMC_inl(r) (*((volatile dword *)(SMC_BASE_ADDRESS+(r)))) also consider that SMC_BASE_ADDRESS is address base of I/O mapped peripherial...
9
by: sushant | last post by:
hello, my question is related with the address of a variable in C. suppose if i am printing the address of a variable, so that address will be virtual address or physical address. and why it'll...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
6
by: Giff | last post by:
Hi I have this problem that I can't solve, I know it shouldn't be hard but I'm not a good coder and I'm going crazy tonight... I have a txt file that goes like this: string1 string2...
7
by: robannexs | last post by:
hi all.. i've got a file of the following format 10000000 records in 10000000 records out 5120000000 bytes (5.1 GB) copied, 628.835 seconds, 8.1 MB/s how am i suppose to get the parameter...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
12
by: Pioneer | last post by:
Hi, I would be installing a desktop application on a standalone PC. How do I make sure that owner should not be ableto copy that and/or give it to other folks. In short, how to build measures to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.