Solved

Retrieve MAC address using VB4

Posted on 1997-02-05
1
407 Views
Last Modified: 2013-12-23
How do you retrieve the MAC (Ethernet) address of a network card using VB4 (32-bit) under Windows NT 3.51, using API calls etc?

Microsoft have a Knowledge Base article on how to do this, but it uses C programming language (article id Q118623).
0
Comment
Question by:dcowmeadow
1 Comment
 

Accepted Solution

by:
fuesers earned 100 total points
Comment Utility
Hello dcowmeadow,

I have written the little tool you are looking for. At the end of the messsage I put the VB4 Code.
I don't use the Netbios Interface, MS did in KB article. I ask the driver directly via the IoControl.
The drivername you must type in, is the "normal" driver name e.g. elnk3 or ne2000 plus the instance-number -> elnk31 or ne20004
To avoid this, you can e.g. use a for/next statement, until QueryDosDevice returns ok.
Ok, if you have more questions, ask me.
If you can use the source, I can send it to you via email.

Harald
--------------------------------------------
Put this in Modul1.bas
--------------------------------------------
Attribute VB_Name = "Modul1"
Option Explicit

Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Boolean
End Type

Type OVERLAPPED
        Internal As Long
        InternalHigh As Long
        offset As Long
        OffsetHigh As Long
        hEvent As Long
End Type


Declare Function QueryDosDevice Lib "kernel32" Alias "QueryDosDeviceA" (ByVal lpDeviceName As String, ByVal lpTargetPath As String, ByVal ucchMax As Long) As Long
Declare Function DefineDosDevice Lib "kernel32" Alias "DefineDosDeviceA" (ByVal dwFlags As Long, ByVal lpDeviceName As String, ByVal lpTargetPath As String) As Long
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, lpOverlapped As OVERLAPPED) As Long
Declare Function GetLastError Lib "kernel32" () As Long

--------------------------------------------
Put this in main.frm
--------------------------------------------
VERSION 4.00
Begin VB.Form Form1
   Caption         =   "Get Mac Address"
   ClientHeight    =   2115
   ClientLeft      =   5685
   ClientTop       =   4635
   ClientWidth     =   5535
   Height          =   2520
   Left            =   5625
   LinkTopic       =   "Form1"
   ScaleHeight     =   2115
   ScaleWidth      =   5535
   Top             =   4290
   Width           =   5655
   Begin VB.TextBox Text1
      Height          =   285
      Left            =   2040
      TabIndex        =   3
      Text            =   "elnk31"
      Top             =   240
      Width           =   2535
   End
   Begin VB.CommandButton Command1
      Caption         =   "Get Address"
      Height          =   495
      Left            =   120
      TabIndex        =   2
      Top             =   840
      Width           =   1815
   End
   Begin VB.Label Label3
      Caption         =   "drivername (z.B. elnk31)"
      Height          =   255
      Left            =   240
      TabIndex        =   4
      Top             =   240
      Width           =   1815
   End
   Begin VB.Label Label2
      Height          =   255
      Left            =   1560
      TabIndex        =   1
      Top             =   1680
      Width           =   2295
   End
   Begin VB.Label Label1
      Caption         =   "Network Address:"
      Height          =   255
      Left            =   120
      TabIndex        =   0
      Top             =   1680
      Width           =   1335
   End
End
Attribute VB_Name = "Form1"
Attribute VB_Creatable = False
Attribute VB_Exposed = False

Private Sub Command1_Click()
getmac (Text1.Text)
End Sub

Public Function getmac(Device As String) As String

Dim SecAtr As SECURITY_ATTRIBUTES
Dim Ovl As OVERLAPPED
Dim Ausgabe As String
Dim Tmp As String
Dim LinkName As String
Dim DeviceName As String
Dim MACFileName As String
Dim Status As Long
Dim ErrorNumber As Long
Dim CreatedDevice As Boolean
Dim OidCode As Long
Dim hMAC As Long
Dim OidData(4096) As Byte
Dim ReturnedCount As Long
Dim Counter As Long
Dim longtmp As Long

LinkName = String(512, " ")
DeviceName = String(80, " ")
MACFileName = String(80, " ")

SecAtr.nLength = 0
SecAtr.lpSecurityDescriptor = 0
SecAtr.bInheritHandle = False

Ovl.Internal = 0
Ovl.InternalHigh = 0
Ovl.offset = 0
Ovl.OffsetHigh = 0
Ovl.hEvent = 0
       
Const ERROR_FILE_NOT_FOUND = 2&
Const DDD_RAW_TARGET_PATH = &H1
Const DDD_REMOVE_DEFINITION = &H2
Const DDD_EXACT_MATCH_ON_REMOVE = &H4
Const GENERIC_READ = &H80000000
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const OPEN_EXISTING = 3
Const INVALID_HANDLE_VALUE = -1
Const OID_802_3_CURRENT_ADDRESS = &H1010102
Const OID_802_3_MY_TEST = &H10177
Const IOCTL_NDIS_QUERY_GLOBAL_STATS = &H170002

Const DEVICE_PREFIX = "\\.\"


getmac = ""
Status = QueryDosDevice(Device, LinkName, 512)
If Status = 0 Then
    ErrorNumber = GetLastError()
    If ErrorNumber = ERROR_FILE_NOT_FOUND Then
        DeviceName = "\Device\" + Device
        Status = DefineDosDevice(DDD_RAW_TARGET_PATH, Device, DeviceName)
        If Status = 0 Then
            Status = MsgBox("DefineDosDevice returned an error creating the device", vbOKOnly, "Error", 0, 0)
            Exit Function
        End If
        CreatedDevice = True
    Else
        Status = MsgBox("QueryDosDevice returned an error", vbOKOnly, "Error", 0, 0)
        Exit Function
    End If
Else
    CreatedDevice = False
End If
   
MACFileName = DEVICE_PREFIX + Device
   
hMAC = CreateFile(MACFileName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, SecAtr, OPEN_EXISTING, 0, INVALID_HANDLE_VALUE)
If hMAC <> INVALID_HANDLE_VALUE Then
   
    OidCode = OID_802_3_CURRENT_ADDRESS
    Status = DeviceIoControl(hMAC, IOCTL_NDIS_QUERY_GLOBAL_STATS, OidCode, 4, OidData(0), 4096, ReturnedCount, Ovl)
    If Status <> 0 Then
        If ReturnedCount = 6 Then
            Ausgabe = ""
            Tmp = Hex(OidData(0))
            Ausgabe = Ausgabe + Tmp + "."
            Tmp = Hex(OidData(1))
            Ausgabe = Ausgabe + Tmp + "."
            Tmp = Hex(OidData(2))
            Ausgabe = Ausgabe + Tmp + "."
            Tmp = Hex(OidData(3))
            Ausgabe = Ausgabe + Tmp + "."
            Tmp = Hex(OidData(4))
            Ausgabe = Ausgabe + Tmp + "."
            Tmp = Hex(OidData(5))
            Ausgabe = Ausgabe + Tmp
            Label2.Caption = Ausgabe
        Else
            Status = MsgBox("DeviceIoControl returned an invalid count", vbOKOnly, "Error", 0, 0)
        End If
    Else
        Status = MsgBox("DeviceIoControl returned an error", vbOKOnly, "Error", 0, 0)
    End If
   
    Status = CloseHandle(hMAC)
   
Else
    ErrorNumber = GetLastError()
    Ausgabe = "CreateFile returned an error = " + Str(ErrorNumber)
    Status = MsgBox(Ausgabe, vbOKOnly, "Error", 0, 0)
End If



If CreatedDevice = True Then
    Status = DefineDosDevice(DDD_RAW_TARGET_PATH Or DDD_REMOVE_DEFINITION Or DDD_EXACT_MATCH_ON_REMOVE, Device, DeviceName)
    If Status = 0 Then
        Status = MsgBox("DefineDosDevice returned an error removing the device", vbOKOnly, "Error", 0, 0)
        Exit Function
    End If
End If


End Function


0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Have you ever set up your wireless router at home or in the office to find that you little pop-up bubble in the bottom right-hand corner of Windows read "IP Conflict - One of more computers on the network have been assigned the following IP address"…
Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now