Jump to content

One same application limits multiple scripts - (Moved)


Go to solution Solved by Jos,

Recommended Posts

Posted (edited)

This is a script that can open multiple same applications, as long as you find the mutex name, close the application thread opened before, and start a new thread program, you can achieve the purpose of opening multiple applications. (Script source ACN, and not tested, please test yourself) Do you have any other better method code? I want to be able to open each thread and return a process ID. Good luck!

#Include <WinAPI.au3>

Global Const $PROCESS_ALL_ACCESS = 0x000F0000 + 0x00100000
;Global Const $DUPLICATE_CLOSE_SOURCE = 0x00000001
Global Const $MUTEX_ALL_ACCESS = 0x1F0001

$lPID = Run(_WeChatPath())
    ConsoleWrite("PID: " & @TAB & @TAB & @TAB & $lPID & @CR)
$MutexName = "_Mutex_Name"
    ConsoleWrite("MUTEX exists: "& @TAB & @TAB & _MutexExists($MutexName) & @CR)

If _MutexExists($MUTEXname) == 1 Then

    $lhandle = _MutexOpen($MutexName)
        ConsoleWrite("MUTEX handle: " & @TAB & @TAB & $lhandle & @CR)
    $hprocess = _WinAPI_OpenProcess($PROCESS_ALL_ACCESS, False, $lPID , False)
        ConsoleWrite("Process handle: " & @TAB & $hprocess & @CR)

    $ret = DllCall("ntdll.dll", "long", "NtDuplicateObject", "long", $hprocess, "long", $lhandle, _
            "long", 0, "long", 0, "long", 0, "long", 0, "long", $DUPLICATE_CLOSE_SOURCE)
        ConsoleWrite("@error on DllCall #1: "& @TAB & @error & @CR)
        ;ConsoleWrite("Return on DllCall #1: "& @TAB & $ret & @CR)
    $ret = DllCall("ntdll.dll", "long", "NtClose", "long", $lhandle)
        ConsoleWrite("@error on DllCall #2: "& @TAB & @error & @CR)
        ;ConsoleWrite("Return on DllCall #2: "& @TAB & $ret & @CR)

        ConsoleWrite("MUTEX exists: "& @TAB & @TAB & _MutexExists($MUTEXname) & @CR)

    If _MutexExists($MUTEXname) == 1 Then
        ConsoleWrite("You have failed!" & @CR)
    Else
        ConsoleWrite("You have won!" & @CR)
    EndIf
Else
    ConsoleWrite("Mutex name not found, script terminated" & @CR)
EndIf
Func _MutexOpen($MutexName)
    Local $hMutex = DllCall("kernel32.dll", "hwnd", "OpenMutex", "int", $MUTEX_ALL_ACCESS, "int", 0, "str", $MutexName)
    If IsArray($hMutex) And $hMutex[0] Then Return $hMutex[0]
    Return 0
EndFunc

Func _MutexExists($MutexName)
    Local $hMutex = DllCall("Kernel32.dll", "hwnd", "OpenMutex", "int", 0x1F0001, "int", 1, "str", $MutexName)
    Local $aGLE = DllCall("Kernel32.dll", "int", "GetLastError")
    If IsArray($hMutex) And $hMutex[0] Then DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $hMutex[0])
    If IsArray($aGLE) And $aGLE[0] = 127 Then Return 1
    Return 0
EndFunc


 

Edited by AndSoW
Added code tags
  • Developers
Posted

Moved to the appropriate AutoIt General Help and Support forum, as the AutoIt Example Scripts forum very clearly states:

Quote

Share your cool AutoIt scripts, UDFs and applications with others.


Do not post general support questions here, instead use the AutoIt Help and Support forums.

Moderation Team

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Posted
21 hours ago, Jos said:

Gotta love g-translate :)

😁😁😁I am a student from China, my English is not good, but I love learning, especially love AU3! Thank you for your platform and support, let me continue to learn and grow, thank you! In our China (Daniel) is a wonderful person, in your place should be called God!

Posted
On 4/6/2023 at 12:41 AM, argumentum said:

 

So, not tested but has a problem.
What are you trying to accomplish ( regardless of the script you posted ).

i 'm sorry! I have tested the script and achieved form thread exclusion using [_WeChattApp_Instance_Identity-MutexName], which allows for unlimited startup of the same program, but this feature is not effective. Can you help me? I think the script can achieve unlimited startup of the same program, but it can also not close existing programs that have already been opened?thank!

Posted (edited)

Hey,

Dunno how you're confident with msdn, windows native stuff, etc and maybe about programming itself ...

But to make it simple (and i don't get why you asked that for so i just gonna explain how it works without example (don't have time to check the pasted code you provided, and mainly this kind of stuff is usually used for some sus application)).

So in one line :

You can't close the mutex itself. You have to duplicate it and free the copy : windows "kills" the original handle when you free the dup handle.

 

/Regards

NB: maybe this is not what you're trying to accomplish, but it's hard to understand your goal: openning infinite instances of a program limited by a mutex (that's what i explained) ? opening, killing then reopening a same program an infinite number of time ?

Edited by 636C65616E
Posted
1 hour ago, 636C65616E said:

Hey,

Dunno how you're confident with msdn, windows native stuff, etc and maybe about programmation itself ...

But to make it simple (and i don't get why you asked that for so i just gonna explain how it works without example (don't have time to check the pasted code you provided, and mainly this kind of stuff is usually used for some sus application)).

So in one line :

You can't close the mutex itself. You have to duplicate it and free the copy : windows "kills" the original handle when you free the dup handle.

 

/Regards

NB: maybe this is not what you're trying to accomplish, but it's hard to understand your goal: openning infinite instances of a programme limited by a mutex (that's what i explained) ? opening, killing then reopening a same program an infinite number of time ?

thank you!I have already processed it. Thank you for your attention

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
×
×
  • Create New...