Solved

Retrieve MAC address using VB4

Posted on 1997-02-05
1
454 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
fuesers earned 100 total points
ID: 1558898
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

Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Downtime reduced, data recovered by utilizing an Experts Exchange Business Account Challenge The United States Marine Corps employs more than 200,000 active-duty Marines with operations in four continents, all requiring complex networking system…
This is the first one of a series of articles I’ll be writing to address technical issues that are always referred to as network problems. The network boundaries have changed, therefore having an understanding of how each piece in the network  puzzl…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 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