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 


I want to stop opening the software many times

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
gavrielsinvani
Cheater
Reputation: 0

Joined: 29 May 2019
Posts: 36

PostPosted: Sat Mar 28, 2020 8:58 am    Post subject: I want to stop opening the software many times Reply with quote

i have lua trianer And I want it to be open only once no multiple.


please help , thanks..
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Sat Mar 28, 2020 10:43 am    Post subject: Reply with quote

Easy, just open it once, right?
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
mindoff
Advanced Cheater
Reputation: 0

Joined: 12 Jun 2016
Posts: 96

PostPosted: Wed Apr 01, 2020 8:57 pm    Post subject: Reply with quote

I know there is a Win32 API called CreateMutex can do this with C++ in my own win32 app.

That can prevent openning multiple instance,keep only one instance.

But I don't know how to do this in lua.

Want to see how lua version written.

Here is my C++ code

Code:

#include <windows.h>

HANDLE win32Mutex = NULL;

...

win32Mutex = CreateMutex(NULL, FALSE, "MyWin32Mutex");
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
   CloseHandle(win32Mutex);
   win32Mutex = NULL;
   MessageBoxA(NULL, "Only 1 instance works", "Only 1 instance works", MB_OK);
   return;//exit app
}


And I think there may be other way to check same instance,something like window title string if you give it an unique name.
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Thu Apr 02, 2020 6:41 am    Post subject: Reply with quote

First, if ported C++ code by @mindoff, maybe something like this in CE Lua:

Code:
function CreateMutex(lpMutexAttributes, bInitialOwner, lpName)
 return  executeCodeLocalEx("kernel32.CreateMutex",lpMutexAttributes, bInitialOwner, lpName)
end

function ReleaseMutex(hMutex)
 return  executeCodeLocalEx("kernel32.ReleaseMutex",hMutex)
end

function GetLastError(SetLastError, flag)
 SetLastError = true
 return  executeCodeLocalEx("kernel32.GetLastError", SetLastError)
end

function CloseHandle(hObject)
 return  executeCodeLocalEx("kernel32.CloseHandle", hObject)
end

ERROR_ALREADY_EXISTS    = 183
win32Mutex = nil
win32Mutex = CreateMutex(nil, false, "MyWin32Mutex")

if (GetLastError() == ERROR_ALREADY_EXISTS)
   CloseHandle(win32Mutex)
   win32Mutex = nil
   showMessage("Only 1 instance works")
   return
end



But the code above seems not to work or need to modify. So, for me, if I want to prevent open multi instances for the same apps, I will use this:

Code:
list = createStringlist()
myprocess = "notepad.exe"

WM_SYSCOMMAND = 0x112
SC_MAXIMIZE = 0xF030
SC_MINIMIZE = 0xF020
SC_CLOSE = 0xF060

function closewindow(hWnd, msg, wParam, lParam)
 return  executeCodeLocalEx("user32.SendMessageA", hWnd, msg, wParam, lParam)
end

function avoidMultiInstances()
 getProcesslist(list)
 local count = 0

 for i=0,list.count-1 do
    if list[i]:find(myprocess) then
       count = count + 1
    end
 end

 if count > 1 then
   --t.Enabled = false
   local w = GetForegroundWindow()
   if w ~= 0 then closewindow(w, WM_SYSCOMMAND, SC_CLOSE, 0) end
   showMessage('Only one instance allowed')
   return
 end
 list.clear()
end

t = createTimer(nil)
t.OnTimer = avoidMultiInstances
t.Interval = 200
t.Enabled = true


Tested and works.

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 470

Joined: 09 May 2003
Posts: 25785
Location: The netherlands

PostPosted: Thu Apr 02, 2020 6:55 am    Post subject: Reply with quote

sharedMemory is easier:
Code:

l=allocateSharedMemoryLocal('MyTrainer',15) --allocates 4096 anyhow
if readStringLocal(l,11)=='DO NOT LOAD' then
  showMessage('already loaded')
  return -- or closeCE()
end
writeStringLocal(l,"DO NOT LOAD")
showMessage('First one')


as long as one process is open with the shared memory object called "MyTrainer" it will exist. Once the last process with it closes, it gets destroyed as well

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
daspamer
Grandmaster Cheater Supreme
Reputation: 54

Joined: 13 Sep 2011
Posts: 1588

PostPosted: Thu Apr 02, 2020 7:24 am    Post subject: Reply with quote

Personally been using createPipe to prevent duplicates for some time, great alternatives tho

Code:
local pipeExists = createPipe('unique_pipe_name');
if (not pipeExists) then
   return showMessage('duplicated');
end

_________________
I'm rusty and getting older, help me re-learn lua.
Back to top
View user's profile Send private message Visit poster's website
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Thu Apr 02, 2020 8:33 am    Post subject: Reply with quote

Dark Byte wrote:
sharedMemory is easier:
Code:

l=allocateSharedMemoryLocal('MyTrainer',15) --allocates 4096 anyhow
if readStringLocal(l,11)=='DO NOT LOAD' then
  showMessage('already loaded')
  return -- or closeCE()
end
writeStringLocal(l,"DO NOT LOAD")
showMessage('First one')


as long as one process is open with the shared memory object called "MyTrainer" it will exist. Once the last process with it closes, it gets destroyed as well


DB this is very slick. So simple. Cool

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
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 Lua Scripting 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 can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites