Sign in to follow this  
Followers 0
Wombat

[SOLVED] get MAC address (W7:yes/wXP:no)

6 posts in this topic

#1 ·  Posted (edited)

The code works on windows 7 but not on XP... why so? :huh:

#include <File.au3>

Global $sIP, $MAs

$Station=InputBox("Workstation", "enter the workstation", "")

_GetMACFromIP($sIP)
Local $text = StringReplace($MAs, "-", "")
IniWrite(@ScriptDir & "\MAC.ini", "mac_addresses", $Station, $text)


Func _GetMACFromIP($sIP)
    Local $MAC,$MACSize
    Local $MAi,$MAr

;Create the struct
$MAC        = DllStructCreate("byte[6]")

;Create a pointer to an int
$MACSize    = DllStructCreate("int")

;*MACSize = 6;
DllStructSetData($MACSize,1,6)

;call inet_addr($sIP)
$MAr = DllCall ("Ws2_32.dll", "int", "inet_addr", _
                "str", $sIP)
$iIP = $MAr[0]

;Make the DllCall
$MAr = DllCall ("iphlpapi.dll", "int", "SendARP", _
                "int", $iIP, _
                "int", 0, _
                "ptr", DllStructGetPtr($MAC), _
                "ptr", DllStructGetPtr($MACSize))

;Format the MAC address into user readble format: 00:00:00:00:00:00
$MAs    = ""
For $MAi = 0 To 5
    If $MAi Then $MAs = $MAs & "-"
        $MAs = $MAs & Hex(DllStructGetData($MAC,1,$MAi+1),2)
Next

;Return the user readble MAC address
    Return $MAs
EndFunc

Resolved by using this code instead:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=MAG.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <WinAPI.au3>
#include <File.au3>


$IPDetails=_IPDetails()
$Station=InputBox("Workstation", "enter the workstation", "")
Local $text = StringReplace($IPDetails[1], ":", "")
IniWrite(@ScriptDir & "\MAC.ini", "mac_addresses", $Station, $text)

Func _IPDetails()
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColItems = $oWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True", "WQL", 0x30), $aReturn[5] = [4]
    If IsObj($oColItems) Then
        For $oObjectItem In $oColItems
            If $oObjectItem.IPAddress(0) == @IPAddress1 Then
                $aReturn[1] = $oObjectItem.MACAddress
                $aReturn[2] = $oObjectItem.DefaultIPGateway(0)
            EndIf
        Next
        Return $aReturn
    EndIf
    Return SetError(1, 0, $aReturn)
EndFunc   ;==>_IPDetails
Edited by Wombat

Just look at us.
Everything is backwards; everything is upside down. Doctors destroy health. Lawyers destroy justice. Universities destroy knowledge. Governments destroy freedom. The major media destroy information and religions destroy spirituality. ~ Michael Ellner


The internet is our one and only hope at a truly free world, do not let them take it from us...

Share this post


Link to post
Share on other sites



anyway.... if you want to use first script

change

$Station=InputBox("Workstation", "enter the workstation", "")

with

$sIP=InputBox("Workstation", "enter the workstation", "")

 


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

 

The code works on windows 7 but not on XP... why so? :huh:

#include <File.au3>

Global $sIP, $MAs

$Station=InputBox("Workstation", "enter the workstation", "")

_GetMACFromIP($sIP)
Local $text = StringReplace($MAs, "-", "")
IniWrite(@ScriptDir & "\MAC.ini", "mac_addresses", $Station, $text)


Func _GetMACFromIP($sIP)
    Local $MAC,$MACSize
    Local $MAi,$MAr

;Create the struct
$MAC        = DllStructCreate("byte[6]")

;Create a pointer to an int
$MACSize    = DllStructCreate("int")

;*MACSize = 6;
DllStructSetData($MACSize,1,6)

;call inet_addr($sIP)
$MAr = DllCall ("Ws2_32.dll", "int", "inet_addr", _
                "str", $sIP)
$iIP = $MAr[0]

;Make the DllCall
$MAr = DllCall ("iphlpapi.dll", "int", "SendARP", _
                "int", $iIP, _
                "int", 0, _
                "ptr", DllStructGetPtr($MAC), _
                "ptr", DllStructGetPtr($MACSize))

;Format the MAC address into user readble format: 00:00:00:00:00:00
$MAs    = ""
For $MAi = 0 To 5
    If $MAi Then $MAs = $MAs & "-"
        $MAs = $MAs & Hex(DllStructGetData($MAC,1,$MAi+1),2)
Next

;Return the user readble MAC address
    Return $MAs
EndFunc

Resolved by using this code instead:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=MAG.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <WinAPI.au3>
#include <File.au3>


$IPDetails=_IPDetails()
$Station=InputBox("Workstation", "enter the workstation", "")
Local $text = StringReplace($IPDetails[1], ":", "")
IniWrite(@ScriptDir & "\MAC.ini", "mac_addresses", $Station, $text)

Func _IPDetails()
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColItems = $oWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True", "WQL", 0x30), $aReturn[5] = [4]
    If IsObj($oColItems) Then
        For $oObjectItem In $oColItems
            If $oObjectItem.IPAddress(0) == @IPAddress1 Then
                $aReturn[1] = $oObjectItem.MACAddress
                $aReturn[2] = $oObjectItem.DefaultIPGateway(0)
            EndIf
        Next
        Return $aReturn
    EndIf
    Return SetError(1, 0, $aReturn)
EndFunc   ;==>_IPDetails

 

None of these scripts work on windows 8, any suggestion?

Thanks!

Share this post


Link to post
Share on other sites

What is the error you are receiving or are you getting nothing in the mac.ini?


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

The only time I get the mac address is using "localhost" as the machine name. If I use and IP or other computer name it returns nothing.

No errors though.

Edited by kudrow

Share this post


Link to post
Share on other sites

Where in your script are you trying the IP address or computer name? Which one of the 2 scripts you quoted are you using?

Please post your script if you've changed either of those.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • nikink
      By nikink
      Hi all,
      I have a bit of code that works on my old Win10 PC, that fails on my new Win10 PC, and I think the only significant difference is the version of Autoit - old PC has 3.3.12, new has 3.3.14.
      I couldn't find anything mentioned in the change logs though, so perhaps I'm wrong.
      Anyway, the code to replicate my issue is:
      Test('username', 'DOMAIN') ; THIS ERRORS: ;Test('localun', 'DOMAIN') ; THIS ERRORS: ;Test(' ', ' ') ; THIS ERRORS: ;Test('', '') ; THIS ERRORS: ;Test('localun', '') ; THIS ERRORS: ;Test('', 'DOMAIN') Func Test($un, $dom) $compName = 'PCNAME' $FullName = '.' $Description = '.' ; get the WIM object $objWMIService = ObjGet("winmgmts:\\" & $compName & "\root\cimv2") ; get default user full name and description $objAccount = $objWMIService.Get("Win32_UserAccount.Name='" & $un & "',Domain='" & $dom & "'") If IsObj($objAccount) Then $FullName = $objAccount.FullName $Description = $objAccount.Description EndIf ConsoleWrite($FullName & @CRLF) ConsoleWrite($Description & @CRLF) Return EndFunc  
      On my old PC this code will output just . and . for each of those line currently commented out. Which is fine.
      On my new PC any of those commented out lines of code cause an error, and the script won't even compile.
      $objAccount = $objWMIService.Get("Win32_UserAccount.Name='" & $un & "',Domain='" & $dom & "'") $objAccount = $objWMIService^ ERROR I'm very much a newb with the WMI stuff and objects, but it looks like the .Get property is failing when either $un or $dom aren't valid in v3.3.14, whereas in 3.3.12 the .Get would fail to return an object, which is then caught by the If statement.
      Am I on track with this? Is there some new/better way to code the example so that 3.3.14 will compile it?
    • nacerbaaziz
      By nacerbaaziz
      Hi all
      I want a way to get the last key pressed.
      I have a program that works with keyboard shortcuts and I want to  give the permission for the user to edit shortcut keys depending on what suits him
      i  want to make read-only edit box and the program writes the latest shortcut key pressed
      Please help me,
      greetings to all
      And thanks in advance
    • nacerbaaziz
      By nacerbaaziz
      hello guys
      how are you؟
      I hope to be fine.
      I have a question  please
      how do I get the menu item that was pressed without that contains a variable؟
      For example I have a menu  of Favorites and I want the script recognizes the existing path in the pressed item
      i'll repeat to tell the item does not contain a variable
      Is there any solution
      if you want to explain more I could write an example of what I want.
      Greetings to all,
      thanks in advance
    • nacerbaaziz
      By nacerbaaziz
      Hello my friends
      I have an urgent question, please help.
      How do I select all the text in the edit or input
      And how to get the currently selected text
      Please help urgently
      Greetings and thanks for all
    • WoodGrain
      By WoodGrain
      Hi All,
      I've coded the small script below, but it can't seem to get the instance of Windows Media player as it keeps going to @error, I've not used com objects before so any assistance would be appreciate. I already have WMP open and minimised. I retrieved "WMPlayerApp" from the AutoIT info tool, I've included a copy below.
      I'm using these sources:
      https://msdn.microsoft.com/en-us/library/dd564085.aspx
      https://msdn.microsoft.com/en-us/library/dd564018.aspx
      $oWMP = ObjGet("", "WMPlayerApp") If @error Then MsgBox(0, "Can't get WMP", "Couldn't connect to the WMP instance") Exit EndIf $wmpPlayState = $oWMP.playState MsgBox(0, "Play State", $wmpPlayState) $wmpSongName = $oWMP.currentMedia.name MsgBox(0, "Play State", $wmpSongName) I've also seen references to the below, but I want to get an existing open WMP:
      ObjCreate("wmplayer.OCX") and have looked at the WMP.udf but can't see how it will do either of the functions I've coded above.