Help with InStr() always finding/not finding string Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Dystopia
Posts: 6
Joined: 08 Apr 2020, 10:02

Help with InStr() always finding/not finding string

03 Jun 2020, 11:51

Hello, I'm trying to lock a script to a mac address (just trying as a small project of mine), and I've sort of hit a roadblock.

The code I have now is this:

Code: Select all

GetMacAddress(){
    tempfile = %A_Temp%\mac.txt
    RunWait, %ComSpec% /c getmac /NH > %tempfile%, , Hide ; ipconfig (slow)
    FileRead, mac, %tempfile%
	MsgBox, %mac%
}

GetMacAddress()

Password = "<Mac address>"
MsgBox, %Password%

If (InStr(mac, Password) > 0){
	MsgBox, found
}
Else {
	ExitApp
}
However, this code always returns false and exits the app. I've tried a number of different codes that should effectively be the same, such as directly placing the mac address as a literal string within InStr(), flipping the needle and haystack around (not sure why, but this always results in a positive, whether I enclosed each in % or not), and basically every thing I could think of that may have been preventing it from working. Any help is appreciated!
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Help with InStr() always finding/not finding string  Topic is solved

03 Jun 2020, 12:06

This should work:

Code: Select all

GetMacAddress() {
Local
    tempfile := A_Temp . "\mac.txt"
    RunWait, %ComSpec% /c getmac /NH > %tempfile%,, Hide ; ipconfig (slow)
    FileRead, mac, %tempfile%
Return mac
}

mac      := GetMacAddress()
Password := "<Mac address>"

If InStr(mac, Password) {
	MsgBox, found
}
Else {
  MsgBox, not found
	ExitApp
}
My Scripts and Functions: V1  V2
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Help with InStr() always finding/not finding string

03 Jun 2020, 12:08

The var mac is not global and not accessible outside the function. (use return mac or use byref )
Give this a try (set password to match what you expect of course.)

Code: Select all

#NoEnv
#SingleInstance, Force
SetWorkingDir %A_ScriptDir%

GetMacAddress(mac)
MsgBox,,mac, %mac%

Password = "<Mac address>"
MsgBox,,Password, %Password%

If (InStr(mac, Password) > 0){
	MsgBox, found
}
Else {
	ExitApp
}

GetMacAddress(ByRef mac){
    tempfile = %A_Temp%\mac.txt
    RunWait, %ComSpec% /c getmac /NH > %tempfile%, , Hide ; ipconfig (slow)
    FileRead, mac, %tempfile%
}
Dystopia
Posts: 6
Joined: 08 Apr 2020, 10:02

Re: Help with InStr() always finding/not finding string

03 Jun 2020, 12:23

Thanks, both of you! Wasn't expecting a response so quickly, and it's working perfectly now. Didn't know that variables declared within a function was only local, but now I do :D

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: peter_ahk and 360 guests