SingleInstance Force with admin rights...?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Seven0528
Posts: 266
Joined: 23 Jan 2023, 04:52
Location: South Korea Languages: EN, JA, KO
Contact:

SingleInstance Force with admin rights...?

Post by Seven0528 » 24 Jan 2023, 11:45


SingleInstance_v1.ahk
SingleInstance_v2.ahk

Code: Select all

#Requires AutoHotkey v1.1
#Include <SingleInstance_v1>
#SingleInstance Off
SingleInstance.Force()
#Persistent

Msgbox % A_ScriptDir
return

Code: Select all

#Requires AutoHotkey v2.0
#Include <SingleInstance_v2>
#SingleInstance Off
SingleInstance().Force()
Persistent

Msgbox A_ScriptDir
return

Code: Select all

Class SingleInstance ; v1.1
{
    static ShellMessageFunc := ObjBindMethod(SingleInstance,"ShellMessage")
            ,DeforceFunc := ObjBindMethod(SingleInstance,"DeForce")

    Force(include_otherdir:=false, ext_needle:="(exe|ahk)")    {
        SplitPath, A_ScriptFullPath, outfilename, outdir, outextension, outnamenoext, outdrive
        this.include_otherdir:=!!include_otherdir, this.ext_needle:=ext_needle
        ,this.outfilename:=outfilename, this.outdir:=outdir, this.outextension:=outextension, this.outnamenoext:=outnamenoext, this.outdrive:=outdrive
        ,DllCall("RegisterShellHookWindow", "UInt",A_ScriptHwnd)
        ,OnMessage(this.MsgNum:=DllCall("RegisterWindowMessage", "Str","SHELLHOOK"), this.ShellMessageFunc)
        ,OnExit(this.DeforceFunc)
    }
    DeForce()    {
        DllCall("DeregisterShellHookWindow", "UInt",A_ScriptHwnd)
    }
    ShellMessage(wParam, lParam)    {
        static EVENT_SYSTEM_DIALOGSTART:=0x0010
        prev_DetectHiddenWindows:=A_DetectHiddenWindows
        DetectHiddenWindows, On
        Switch wParam
        {
            case EVENT_SYSTEM_DIALOGSTART:
                WinGetTitle, vTitle, % "ahk_id" lParam
                Switch this.include_otherdir
                {
                    case true:
                        if (vTitle~="iD)\\\Q" this.outnamenoext "\E\." this.ext_needle)
                            ExitApp
                    case false:
                        if (vTitle~="iD)^\Q" A_ScriptFullPath "\E")
                            ExitApp
                }
        }
        DetectHiddenWindows, % prev_DetectHiddenWindows
    }
}

Code: Select all

Class SingleInstance ; v2.0
{
    ShellMessageFunc := ObjBindMethod(this,"ShellMessage")
    ,DeforceFunc := ObjBindMethod(this,"DeForce")

    Force(include_otherdir:=false, ext_needle:="(exe|ahk)")    {
        SplitPath(A_ScriptFullPath, &outfilename, &outdir, &outextension, &outnamenoext, &outdrive)
        ,this.include_otherdir:=!!include_otherdir, this.ext_needle:=ext_needle
        ,this.outfilename:=outfilename, this.outdir:=outdir, this.outextension:=outextension, this.outnamenoext:=outnamenoext, this.outdrive:=outdrive
        ,DllCall("RegisterShellHookWindow", "UInt",A_ScriptHwnd)
        ,OnMessage(this.MsgNum:=DllCall("RegisterWindowMessage", "Str","SHELLHOOK"), this.ShellMessageFunc)
        ,OnExit(this.DeforceFunc)
    }
    DeForce(*)    {
        DllCall("DeregisterShellHookWindow", "UInt",A_ScriptHwnd)
    }
    ShellMessage(wParam, lParam, *)    {
        static EVENT_SYSTEM_DIALOGSTART:=0x0010
        prev_DetectHiddenWindows:=A_DetectHiddenWindows
        ,DetectHiddenWindows(true)
        Switch wParam
        {
            case EVENT_SYSTEM_DIALOGSTART:
				vTitle:=WinGetTitle("ahk_id" lParam)
                Switch this.include_otherdir
                {
                    case true:
                        if (vTitle~="iD)\\\Q" this.outnamenoext "\E\." this.ext_needle)
                            ExitApp
                    case false:
                        if (vTitle~="iD)^\Q" A_ScriptFullPath "\E")
                            ExitApp
                }
        }
        DetectHiddenWindows(prev_DetectHiddenWindows)
    }
}

 Hello. I'm Seven0528. Nice to meet you.
(I don't speak English well. Sorry)
There is no information about v2 in the Korean AutoHotkey community, that's why I signed up. I'm pleased to be here.
Actually, I just signed up for forums, So I am not used to this forum yet. :)

There was an issue with SingleInstance in admin rights,
that's why I made this script. I wanted the user rights script to be able to turn off the admin rights script.
Here are my questions.






1.
Is there a better way to do what I want?
I wanted the script Instance to be replaced, regardless of some rights issue or directory issue.

Code: Select all

SingleInstance().Force(true) ; Just care about the process name
SingleInstance().Force() 


2.
For a long time, I've been using my code alone. (it's also because of my bad English, I'm working on it though)
I want to be verified about my script by someone else.
Also, it is the first time I switched the ahk script from v1 to v2.
Did I convert it well? Am I right...?



3.
I just signed up, so I'm not sure if I'm qualified.
Compared to the people here, I feel like I'm not good enough.
Can I use Scripts and Functions board...?
Is it a place where I can post my insufficient(?) script? Are there any other conditions for using that board?
(Also I wonder if I can't edit a post that was posted already)






 I'm sorry to have so many questions. I don't even know if I picked the right board.
Thank you for reading it:)

Return to “Ask for Help (v2)”