Помощь - Поиск - Пользователи - Календарь
Полная версия: Get the time since given process is started.
UoKit.com Форумы > Кликер > UO Pilot
neves
Is there any lua function to get the time since given process is started?
Есть ли какая-нибудь функция lua для получения времени с момента запуска данного процесса?
cirus
Код
--lua
local t = os.clock()

while true do
    log (os.clock() - t)
    wait (100)
end
neves
Цитата(cirus @ 11.5.2020, 22:16) *

Код
--lua
local t = os.clock()

while true do
    log (os.clock() - t)
    wait (100)
end


Thanks for the fast reply, but that's not what I need.
Example:
I bind given script to 'calculator.exe', and I wanna know at what time 'calculator.exe' was started or for how long calcualtor.exe is running.
For what I need this? I just want to make script which checks if process is running for more than 3 hours.
Fors1k
Цитата(neves @ 11.5.2020, 23:22) *
I wanna know at what time 'calculator.exe' was started

Код
--lua
log"clear";log"mode compact"
require"luaposh";PScode('return',{[[#}

log (Get-Process *calc*).StartTime

]]})
Fors1k
Цитата(neves @ 11.5.2020, 23:22) *
I just want to make script which checks if process is running for more than 3 hours.

Код
--lua
log"clear";log"mode compact"
require"luaposh";PScode('return',{[[#}

if(((get-date) - (Get-Process *calc*).StartTime).totalhours -gt 3){
    log 'YES'
}
else{
    log 'NO'
}

]]})

note
-gt Greater than
-lt Less than
neves
Цитата(Fors1k @ 11.5.2020, 22:55) *

Код
--lua
log"clear";log"mode compact"
require"luaposh";PScode('return',{[[#}

if(((get-date) - (Get-Process *calc*).StartTime).totalhours -gt 3){
    log 'YES'
}
else{
    log 'NO'
}

]]})


Awesome! That's working, but is there any way to do it without using posh?
I just found this -> winapi/process.lua and now trying to implement it.
Fors1k
Цитата(neves @ 12.5.2020, 0:05) *

Awesome! That's working, but is there any way to do it without using posh?

With posh you can solve almost all ur needs, without necessary to install any mods.
For example u can remember this issue.
Ask, if u'll need more help with the code.

Anyway, u can continue writing in lua below this luaposh block. Or above it.
cirus
Цитата
but is there any way to do it without using posh?

code
Код
--lua
local PROCESS_ALL_ACCESS = 2097151
local ffi = require("ffi")
local user, kernel = ffi.load('User32'), ffi.load('Kernel32')
ffi.cdef[[
    typedef unsigned short  WORD;
    typedef unsigned long   DWORD;
    typedef DWORD           *LPDWORD;
    typedef struct _FILETIME {DWORD dwLowDateTime; DWORD dwHighDateTime;} FILETIME, *PFILETIME, *LPFILETIME;
    typedef struct _SYSTEMTIME {WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay;
        WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds;} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;
    bool CloseHandle(int hObject);
    int OpenProcess(DWORD dwDesiredAccess, bool bInheritHandle, DWORD dwProcessId);
    bool GetProcessTimes(int hProcess, LPFILETIME lpCreationTime, LPFILETIME lpExitTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime);
    DWORD GetWindowThreadProcessId(int hWnd, LPDWORD lpdwProcessId);
    bool FileTimeToSystemTime(const FILETIME *lpFileTime, LPSYSTEMTIME lpSystemTime);
    bool FileTimeToLocalFileTime(const FILETIME *lpFileTime, LPFILETIME lpLocalFileTime);
]]
local ft, ft2, ft3, ft4, st = ffi.new('FILETIME'), ffi.new('FILETIME'), ffi.new('FILETIME'), ffi.new('FILETIME'), ffi.new('SYSTEMTIME')
local PID = ffi.new('unsigned long[1]')

function GetProcessTime(handle)
    if handle == 0 then return 0 end
    user.GetWindowThreadProcessId(handle, PID)
    local process = kernel.OpenProcess(PROCESS_ALL_ACCESS, 1, PID[0])
    if process > 0 then
        kernel.GetProcessTimes(process, ft, ft2, ft3, ft4)
        kernel.FileTimeToLocalFileTime(ft, ft);
        kernel.FileTimeToSystemTime(ft, st);
        local datetime = { year = st.wYear, month = st.wMonth, day = st.wDay, hour = st.wHour, min = st.wMinute, sec = st.wSecond}
        kernel.CloseHandle(process);
        return os.time() - os.time(datetime)
    end
    return -1
end



log 'clear' log 'mode compact'
local t = GetProcessTime(workwindow())     -- function call
log (tostring(t) .. ' seconds')
neves
Цитата(Fors1k @ 11.5.2020, 23:19) *

With posh you can solve almost all ur needs, without necessary to install any mods.
For example u can remember this issue.
Ask, if u'll need more help with the code.

Anyway, u can continue writing in lua below this luaposh block. Or above it.

Posh solution is very simple and clean! Just wanted to see ffi/winapi solution because I already had them imported in my script.
About this issue. Other problems popped up and sadly its not finished. :/

Цитата(cirus @ 11.5.2020, 23:59) *

code
Код
--lua
local PROCESS_ALL_ACCESS = 2097151
local ffi = require("ffi")
local user, kernel = ffi.load('User32'), ffi.load('Kernel32')
ffi.cdef[[
    typedef unsigned short  WORD;
    typedef unsigned long   DWORD;
    typedef DWORD           *LPDWORD;
    typedef struct _FILETIME {DWORD dwLowDateTime; DWORD dwHighDateTime;} FILETIME, *PFILETIME, *LPFILETIME;
    typedef struct _SYSTEMTIME {WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay;
        WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds;} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;
    bool CloseHandle(int hObject);
    int OpenProcess(DWORD dwDesiredAccess, bool bInheritHandle, DWORD dwProcessId);
    bool GetProcessTimes(int hProcess, LPFILETIME lpCreationTime, LPFILETIME lpExitTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime);
    DWORD GetWindowThreadProcessId(int hWnd, LPDWORD lpdwProcessId);
    bool FileTimeToSystemTime(const FILETIME *lpFileTime, LPSYSTEMTIME lpSystemTime);
    bool FileTimeToLocalFileTime(const FILETIME *lpFileTime, LPFILETIME lpLocalFileTime);
]]
local ft, ft2, ft3, ft4, st = ffi.new('FILETIME'), ffi.new('FILETIME'), ffi.new('FILETIME'), ffi.new('FILETIME'), ffi.new('SYSTEMTIME')
local PID = ffi.new('unsigned long[1]')

function GetProcessTime(handle)
    if handle == 0 then return 0 end
    user.GetWindowThreadProcessId(handle, PID)
    local process = kernel.OpenProcess(PROCESS_ALL_ACCESS, 1, PID[0])
    if process > 0 then
        kernel.GetProcessTimes(process, ft, ft2, ft3, ft4)
        kernel.FileTimeToLocalFileTime(ft, ft);
        kernel.FileTimeToSystemTime(ft, st);
        local datetime = { year = st.wYear, month = st.wMonth, day = st.wDay, hour = st.wHour, min = st.wMinute, sec = st.wSecond}
        kernel.CloseHandle(process);
        return os.time() - os.time(datetime)
    end
    return -1
end
log 'clear' log 'mode compact'
local t = GetProcessTime(workwindow())     -- function call
log (tostring(t) .. ' seconds')


Works perfect! Thank you.
Это текстовая версия — только основной контент. Для просмотра полной версии этой страницы, пожалуйста, нажмите сюда.
Русская версия Invision Power Board © 2001-2024 Invision Power Services, Inc.