Jump to content

Recommended Posts

Posted

Hi community, I am making a script and I want it to show on a GUI the MAC address of my computer, the script runs without errors, but it does not show my MAC address.

Here is the code:

 

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <Process.au3> ; Include for process handling

; Get the MAC address
Local $sMAC = _GetMACAddress()

; Create the GUI
GUICreate("MAC Address", 300, 100)

; Add a label to display the MAC address
GUICtrlCreateLabel("Your MAC address: " & $sMAC, 10, 10, 280, 30)

; Show the GUI
GUISetState()

; Wait for the user to close the window
While 1
    $nMsg = GUIGetMsg()
    If $nMsg = $GUI_EVENT_CLOSE Then Exit
WEnd

Func _GetMACAddress()
    Local $sCommand = "getmac"
    Local $sResult = Run(@ComSpec & " /c " & $sCommand, "", @SW_HIDE, 1) ; Capture standard output
    Local $sOutput = StdoutRead($sResult)
    Return StringTrimRight($sOutput, 2) ; Trim the newline at the end
EndFunc

 

Posted (edited)

Hi @zoel 👋 ,

please try this function instead (an old one of mine):

Func _GetMacOrIpAddress()
    Local Const $oWmiService      = ObjGet('winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2')
    Local Const $oNetworkAdapters = $oWmiService.ExecQuery('select * from Win32_NetworkAdapterConfiguration where IPEnabled = True')

    For $oNetworkAdapter In $oNetworkAdapters
        If $oNetworkAdapter.MACAddress <> '' Then
            Return $oNetworkAdapter.MACAddress
        EndIf
    Next

    Return @IPAddress1 ; fallback
EndFunc

Best regards
Sven

Edited by SOLVE-SMART

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Posted (edited)

@zoel There is numerous issues in your code :

1- not enough space to display the result
2- best to use a fixed space font (to align columns)
3- wrong stream option in run command
4- no need to shell getmac
5- need to wait for process to end
6- undeclared variable (always use Opt("MustDeclareVars", True))

#include <Constants.au3>
#include <GUIConstants.au3>

Opt("MustDeclareVars", True)

; Get the MAC address
Local $sMAC = _GetMACAddress()

; Create the GUI
GUICreate("MAC Address", 700, 150)

; Add a label to display the MAC address
GUICtrlCreateLabel("Your MAC address: " & $sMAC, 10, 10, 680, 130)
GUICtrlSetFont(-1, 0, 0, 0, "FixedSys")

; Show the GUI
GUISetState()

; Wait for the user to close the window
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func _GetMACAddress()
  Local $iPID = Run("getmac", "", @SW_HIDE, $STDERR_MERGED)   ; Capture standard output
  ProcessWaitClose($iPID)
  Local $sOutput = StdoutRead($iPID)
  Return StringTrimRight($sOutput, 2)   ; Trim the newline at the end
EndFunc   ;==>_GetMACAddress

 

Edited by Nine

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...