Page 2 of 2 FirstFirst 12
Results 16 to 17 of 17
  1. #16
    Administrator Rick Corbett's Avatar
    Join Date
    Dec 2009
    Location
    South Glos., UK
    Posts
    3,462
    Thanks
    153
    Thanked 941 Times in 756 Posts
    All joshing aside... much as I love what I consider to be the relative simplicity of AutoHotkey (AHK) to automate many things, there are times when I can't find a simple way in AHK to do something I want... so revert to using PowerShell.

    For example, I use Synergy to share a single mouse and keyboard between multiple computers... a software KVM. It works great but the version I use has a habit of losing its shared clipboard. As a result I now use a PowerShell one-liner to stop and start the service to fix this:

    Code:
    Restart-Service -Name Synergy
    It's that easy (and elegant)...

    To compare, here's a function in AHK to do the same:

    Code:
    RestartService(ServiceName, NumServiceArgs := 0, ServiceArgVectors := 0, ByRef SERVICE_STATUS := "")
    {
        Local hSCManager
        If (!(hSCManager := DllCall("Advapi32.dll\OpenSCManagerW", "Ptr", 0, "Ptr", 0, "UInt", 0xF003F, "Ptr")))
            Return -1
    
        Local hService
        If (!(hService := DllCall("Advapi32.dll\OpenServiceW", "Ptr", hSCManager, "UPtr", &ServiceName, "UInt", 0x0020|0x0010, "Ptr")))    ; SERVICE_STOP|SERVICE_START
        {
            DllCall("AdvApi32.dll\CloseServiceHandle", "Ptr", hSCManager)
            Return -2
        }
    
        VarSetCapacity(SERVICE_STATUS, 7 * 4, 0)
        Local R := DllCall("AdvApi32.dll\ControlService", "Ptr", hService, "UInt", 0x00000001, "UPtr", &SERVICE_STATUS) ? 0 : (A_LastError != 1062)    ; SERVICE_CONTROL_STOP = 0x00000001 | ERROR_SERVICE_NOT_ACTIVE = 1062
    
        If (!R)
            R := DllCall("AdvApi32.dll\StartServiceW", "Ptr", hService, "UInt", NumServiceArgs, "UPtr", ServiceArgVectors) ? 0 : 2
    
        DllCall("AdvApi32.dll\CloseServiceHandle", "Ptr", hService, "UInt")
        DllCall("AdvApi32.dll\CloseServiceHandle", "Ptr", hSCManager, "UInt")
    
        Return R    ; 0 = ERROR_SUCCESS
    }
    The point is... AHK doesn't have a native way to manipulate services in the same way that PowerShell does so has to rely on hooking the OS's built-in methods.
    Last edited by Rick Corbett; 2018-06-27 at 10:23.

  2. #17
    Administrator Rick Corbett's Avatar
    Join Date
    Dec 2009
    Location
    South Glos., UK
    Posts
    3,462
    Thanks
    153
    Thanked 941 Times in 756 Posts
    Quote Originally Posted by access-mdb
    Shame your attachment is invalid Rick!
    Thank you... I've been getting that a few times recently. Now fixed.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •