Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Using [io.popen] to get process' actual location/folder...

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Tutorials -> LUA Tutorials
View previous topic :: View next topic  
Author Message
paul44
Advanced Cheater
Reputation: 1

Joined: 20 Jul 2017
Posts: 92

PostPosted: Sat Nov 21, 2020 4:12 am    Post subject: Using [io.popen] to get process' actual location/folder... Reply with quote

I've spent some considerable time (revisiting several times) to get this working. 2 main issues: a) redirect input from external command to lua (var) environment b) openDialog using a specific subfolder.

a) an exercise on how to redirect external command's 'stdout' to a lua variable
b) the main goal is to avoid any "user_environment invasion" (such as file_creation/deletion) altogether... (unless explicitly permitted by user)


[code][ENABLE]

{$lua}
getLuaEngine().MenuItem5.doClick()

local sGame = "cheatengine-x86_64.exe"
--local sGame = "explorer.exe"
print(sGame .. '\n')

-- notice that in this command, vertical bar (|) must be 'escaped'...!
cmd = "echo off & for /F \"tokens=1,2 delims= \" %a in ('tasklist /FI \"IMAGENAME eq " .. sGame .. "\" ^| find /I \"" .. sGame .. "\"')"
print(cmd .. '\n')

-- %a = processname ~ %b = processID
local sep = "~"
--local cmd_b = ""
local cmd_b = " do echo %a" .. sep .. "%b"

-- you can combine several commands into 1 cmd string
-- you can NOT use [[ ]] for this purpose...!
local f = assert(io.popen(cmd .. cmd_b,'r'))
--local s = assert(f:read('*a')) -- reports all info
local s = assert(f:read('*l')) -- gets last line (ignoring LFs ?)
print(s .. '\n')

-- in case of multi-line results, split them up (done here by using %a & %b)...
local t = {}
for str in string.gmatch(s, "([^".. sep .."]+)") do table.insert(t, str) end
print(t[1])
print(t[2] .. '\n')

-- notice that in this command, vertical bar (|) may NOT be 'escaped'...!
-- cmd = "wmic process where \"ProcessID="..t[2].."\" get ExecutablePath" .. " | find /I \"" .. t[1] .. "\""
-- variation... ~ also notice the use of '@'-echo...!
cmd = "wmic process where \"ProcessID="..t[2].."\" get ExecutablePath" .. " | for /f \"delims=\" %a in ('find /I \"" .. t[1] .. "\"') do @echo %a"
print(cmd .. '\n')

f = assert(io.popen(cmd,'r'))
s = assert(f:read('*a')) -- using '*l' will take last line and removes " ending empty line/CRLF"...
f:close()


-- adding text/info prior to using 'match'...
print('*** { ' .. s .. '\\..\\autorun' .. ' } *** \n')
-- adding text/info extract full folder path (minus exe_name)...
s = s:match("(.*[/\\])")
-- after using 'match'...
print(s .. 'autorun \n')

-- any additional text is shown (added?) on newline...?!
-- DOS trick: adding '\..\' to a full_path returns folder_path only...
-- (you can use [cd /d "your_path"] to change to that folder for example)
print(s .. '..\\ \n')

-- launch Dialog window to select a file...
function openMyDialog(sender)
load_dialog = createOpenDialog(self)
load_dialog.filename = s -- will open dialog in preferred subfolder... in combo with match...!
load_dialog.File = ''
load_dialog.Filter = 'Executable files|*.EXE'
load_dialog.InitialDir = os.getenv('USERPROFILE') -- s:match("(.*[/\\])") ~ this property always seems to be ignored...
load_dialog.execute()

local path = load_dialog.FileName

print('['..path..']')
print(path:match("[^\\]*$"))
print(os.getenv('USERPROFILE'))
end

openMyDialog()



[DISABLE]
[/code]

some sidenotes:
1. InitialDir: never worked for me...
2. os.getenv('USERPROFILE'): i've seen several examples using '%USERPROFILE%' instead; not working for me...

ps: just copy/paste this stuff in a blank script; or download this table (and more): [ https://fearlessrevolution.com/viewtopic.php?p=164917&sid=5aa4e802fcab33bad91284a965e17be9#p164917 ]
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 157

Joined: 25 Jan 2006
Posts: 8103
Location: 127.0.0.1

PostPosted: Sat Nov 21, 2020 2:14 pm    Post subject: Reply with quote

WMIC is not reliable at all and is often disabled on prebuilt machines from certain companies (like HP, Dell, etc.). I wouldn't suggest using it/relying on it to obtain information about the system at all.

With what is currently available with CE, you can do the following to get a processes path:

Code:

function getProcessPath(name)
    local p = getProcesslist();
    for k, v in pairs(p) do
        if (string.lower(v) == name) then
            local m = enumModules(k);
            if (m == nil or #m == 0) then
                return nil;
            end
            return m[1].PathToFile;
        end
    end
    return nil;
end

print(getProcessPath('discord.exe'));

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
paul44
Advanced Cheater
Reputation: 1

Joined: 20 Jul 2017
Posts: 92

PostPosted: Wed Nov 25, 2020 2:30 am    Post subject: as stated... Reply with quote

^ as stated in my post, this "exercise" is to inform just what one can/could do to collect certain/external info. If you would search for this type of info, you'll find very little to none related to lua (and even less in combo with Windows for that matter).
The command itself is actually not that important; in fact this is a follow up on my research to get access to a particular registry setting (not part of the 'getSettings' section)...

Now, as for your suggestion, using 'enumModules()': I did had a look at this fn as it was used/suggested in some other situation, but could hardly find any info/details on what kind/type of info it can produce (yep, I even wound up on some ms api pages... hooray, if you catch my drift). I've added this to my Todo_list...
And thx for the feedback.


btw: I got the wmic instruction @Stackoverflow; and i'm sure you'll take my word for it, that you'll find the same discussion on whether or not to use wmic (or ps/andwhatnot) for this purpose.
but you are right about 'wmic' though, as win10 - at some point - will tell you that it is (or will be, can't remember) depreciated
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Tutorials -> LUA Tutorials All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites