Find networked computers MAC address?

ryodoan

Well-known member
Joined
Jul 16, 2004
Messages
65
Location
Ohio
Programming Experience
3-5
I am working on developing a simple Wake on Lan program of my own and a feature I have seen and liked in a couple programs I downloaded before deciding to write my own is the ability to find another computer's MAC address if it is turned on and connected to the network.

One method I have found is if you go to cmd.exe and type out:

ping <computer-name>
arp -a

It will display the pinged comptuers MAC address in a table.

Instead of running the CMD and trying to then parse some text I wanted a way to do this all from inside my program.

I found a windows API that is supposed to give me access to the ARP table, getIpNetTable and even found the function declaration for it:
VB.NET:
Private Declare Function GetIpNetTable Lib "Iphlpapi.dll" (pIpNetTable As Byte, pdwSize As Long, ByVal bOrder As Long) As Long

when I try to call on the above function with the code:

VB.NET:
        Dim Ret As Long
        Dim test As Byte = 0&
        GetIpNetTable(test, Ret, True)

I get the following error:
PInvokeStackImbalance was detected
Message: A call to PInvoke function 'CafUtilsDev!CafUtilsDev.Form1::GetIpNetTable' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
The problem arises in the fact that for the life of me I can not figure out how to use this function. I have found several example programs but they are all written in either VB 6.0 or VC 6.0 neither of which I have enough experience to translate to VB 2005.

Searching the windows MSDN showed me that they know the API exists but was not very helpful to me in figuring out how to actually write a program that uses it...


Here are some links that I have already found:

MSDN for getIpNetTable:
http://msdn2.microsoft.com/en-us/library/aa365956.aspx

A VB 6.0 program that uses getIpNetTable (I tried to translate it into vb 2005 on my own and failed):
http://www.ex-designz.net/apidetail.asp?api_id=443

Thanks for your time and I hope to hear back from you.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,603
Location
Norway
Programming Experience
10+
ApiViewer could help you get declarations. The Essential P/Invoke article at Code Project is also interesting reading when dealing with C++ documentation and .Net conversions.

If you need to take a VB6 to VB.Net route you could use Help for Visual Basic 6.0 Users. Basically the problem with Long in that declaration is it were a 32bit integer in classic VB, VB.Net 32bit is Integer/Int32.

Much easier is to use WMI, start up WMI Code Creator, do a few mouse clicks and you have working code to get MACAddress from Win32_NetworkAdapter class targeting a remote computer.
 

ryodoan

Well-known member
Joined
Jul 16, 2004
Messages
65
Location
Ohio
Programming Experience
3-5
Thanks for your reply, but I ran into some problems in implementing your suggestions also so I decided to go with the low tech, less elegant way of doing this.

VB.NET:
    Public Function getRemoteMac(ByVal CompOrIP As String) As String
        'system.Net.Dns.

        'Error: No computer was passed into the function
        If CompOrIP = "" Then
            Return "No computer or IP address specified."
        End If

        Dim psi As New ProcessStartInfo("cmd.exe")
        psi.WindowStyle = ProcessWindowStyle.Hidden
        psi.Arguments = "/c ""ping " & CompOrIP & " -n 1 > C:\output.txt&&echo " _
                            & CompOrIP & " > C:\output.txt&&arp -a >> C:\output.txt"""

        Dim proc As New System.Diagnostics.Process
        proc.Start(psi)

Basically, I use CMD.exe to ping the target computer then immediately check the arp table through CMD and output the result to a text file which I then parse for the MAC address of the pinged computer.

The text parsing is rather straightforward but if anyone stumbles accross this post and wants more information I can post it. Basically I use the fact that I know the computers IP address, search the text file for the IP address, then grab the MAC address that follows that IP address.

Its not very elegant but it seems to work so far.

I didnt use the WMI because it wanted me to put in a username / password for the remote computer, which even when I had a correct username / password didnt work. I didnt use the API function call to windows because I ran out of patiences trying to interpret other peoples code.
 
Top Bottom