Jump to content

Open a gui from a Service


Pirosoft
 Share

Recommended Posts

Hi i have cretaed a service, now i want that this service open a gui to make a test. But don't open the gui, why?

In main function a call Example function, this open a gui and write what i sent in a tcp connection on a port 33891.

This is my code:

#include <GUIConstantsEx.au3>
#include<Service.au3>
$sServiceName = "test command"
If $cmdline[0] > 0 Then
    Switch $cmdline[1]
        Case "install", "-i", "/i"
            InstallService()
        Case "remove", "-u", "/u", "uninstall"
            RemoveService()
        Case Else
            ConsoleWrite(" - - - Help - - - " & @crlf)
            ConsoleWrite("params : " & @crlf)
            ConsoleWrite("  -i : install service" & @crlf)
            ConsoleWrite("  -u : remove service" & @crlf)
            ConsoleWrite(" - - - - - - - - " & @crlf)
            Exit
        ;start service.
    EndSwitch
EndIf
_Service_init($sServiceName)

func main()
    while 1
    ;doing what you want here
    ;msgbox(4,"","ok")
        Example();
    Sleep(1000)
    WEnd
EndFunc

Func InstallService()
    ConsoleWrite("Installing service, please wait" & @CRLF)
    _Service_Create("", $sServiceName, "AIRcommand", '"' & @ScriptFullPath & '"')
    If @error Then
        ConsoleWrite("Problem installing service, Error number is " & @error & @CRLF & " message  : " & _WinAPI_GetLastErrorMessage())
    Else
        ConsoleWrite("Installation of service successful")
        _SelfRun($sServiceName,"start")
    EndIf
    Exit
EndFunc  ;==>InstallService

Func RemoveService()
    _StopService("", $sServiceName)
    _DeleteService("", $sServiceName)
    if not @error then ConsoleWrite("service removed successfully" & @crlf)
    Exit
EndFunc  ;==>RemoveService

Func _SelfRun($servicename,$action)
    $sCmdFile = 'sc ' & $action & ' "' & $servicename & '"' & @CRLF & 'del ' & @TempDir & '\runsvc.bat'
    FileWrite(@TempDir & "\runsvc.bat", $sCmdFile)
    Run(@TempDir & "\runsvc.bat", @TempDir, @SW_HIDE)
    Exit
EndFunc


Func Example()
; Set Some reusable info
; Set your Public IP address (@IPAddress1) here.
;   Local $szServerPC = @ComputerName
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = 33891
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg, $recv

; Start The TCP Services
;==============================================
    TCPStartup()

; Create a Listening "SOCKET".
;   Using your IP Address and Port 33891.
;==============================================
    $MainSocket = TCPListen($szIPADDRESS, $nPORT)

; If the Socket creation fails, exit.
    If $MainSocket = -1 Then Exit


; Create a GUI for messages
;==============================================
    $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)
    $edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
    GUISetState()


; Initialize a variable to represent a connection
;==============================================
    $ConnectedSocket = -1


;Wait for and Accept a connection
;==============================================
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1


; Get IP of client connecting
    $szIP_Accepted = SocketToIP($ConnectedSocket)

; GUI Message Loop
;==============================================
    While 1
        $msg = GUIGetMsg()

    ; GUI Closed
    ;--------------------
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop

    ; Try to receive (up to) 2048 bytes
    ;----------------------------------------------------------------
        $recv = TCPRecv($ConnectedSocket, 2048)

    ; If the receive failed with @error then the socket has disconnected
    ;----------------------------------------------------------------
        If @error Then ExitLoop

    ; Update the edit control with what we have received
    ;----------------------------------------------------------------
        If $recv <> "" Then GUICtrlSetData($edit, _
                $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
    WEnd


    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

    TCPShutdown()
EndFunc  ;==>Example

; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet
    
    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "ptr", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc  ;==>SocketToIP

can you help me?

thanks

Link to comment
Share on other sites

Well you never tell the script to call Example(), so no GUI is displayed :mellow:

in function main:

func main()
    while 1
   ;doing what you want here
   ;msgbox(4,"","ok")
        Example();
    Sleep(1000)
    WEnd
EndFunc

the main is called on start program service.

see this post: autoit post

help!

Link to comment
Share on other sites

now i have resolved the problem, but now i have another problem!

I create a service, the service create a socket listening ina specific port.

I send data to service and the service must write the data in C:\test.txt

And it's all ok.

But if i close my program to send data, and run onther time, i can connect and return error 10061

????

this is my code:

#include <GUIConstantsEx.au3>
#include<Service.au3>

$sServiceName = "AIRcommand"
If $cmdline[0] > 0 Then
    Switch $cmdline[1]
        Case "install", "-i", "/i"
            InstallService()
        Case "remove", "-u", "/u", "uninstall"
            RemoveService()
        Case Else
            ConsoleWrite(" - - - Help - - - " & @crlf)
            ConsoleWrite("params : " & @crlf)
            ConsoleWrite("  -i : install service" & @crlf)
            ConsoleWrite("  -u : remove service" & @crlf)
            ConsoleWrite(" - - - - - - - - " & @crlf)
            Exit
        ;start service.
    EndSwitch
EndIf
_Service_init($sServiceName)

func main()
    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = 33891
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg, $recv

; Start The TCP Services
;==============================================
    TCPStartup()

; Create a Listening "SOCKET".
;   Using your IP Address and Port 33891.
;==============================================
    $MainSocket = TCPListen($szIPADDRESS, $nPORT)

; If the Socket creation fails, exit.
    If $MainSocket = -1 Then Exit


; Initialize a variable to represent a connection
;==============================================
    $ConnectedSocket = -1


;Wait for and Accept a connection
;==============================================
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1


; Get IP of client connecting
    $szIP_Accepted = SocketToIP($ConnectedSocket)

;==============================================
    While 1
        
    ; Try to receive (up to) 2048 bytes
    ;----------------------------------------------------------------
        $recv = TCPRecv($ConnectedSocket, 2048)

    ; If the receive failed with @error then the socket has disconnected
    ;----------------------------------------------------------------
        If @error Then ExitLoop

    ; Update the file test.txt
    ;----------------------------------------------------------------
        
        If $recv <> "" Then 
            $file = FileOpen("C:\test.txt", 1);
            fileWrite($file, "IP: " & $szIP_Accepted & " > " & $recv & @CRLF);
            FileClose($file);
        EndIf
            
    WEnd
    
    If ($ConnectedSocket <> -1) Then 
        TCPCloseSocket($ConnectedSocket)
    EndIf

    TCPShutdown()
EndFunc

Func InstallService()
    ConsoleWrite("Installing service, please wait" & @CRLF)
    _Service_Create("", $sServiceName, "AIRcommand", '"' & @ScriptFullPath & '"')
    If @error Then
        ConsoleWrite("Problem installing service, Error number is " & @error & @CRLF & " message  : " & _WinAPI_GetLastErrorMessage())
    Else
        ConsoleWrite("Installation of service successful")
        _SelfRun($sServiceName,"start")
    EndIf
    Exit
EndFunc  ;==>InstallService

Func RemoveService()
    _StopService("", $sServiceName)
    _DeleteService("", $sServiceName)
    if not @error then ConsoleWrite("service removed successfully" & @crlf)
    Exit
EndFunc  ;==>RemoveService

Func _SelfRun($servicename,$action)
    $sCmdFile = 'sc ' & $action & ' "' & $servicename & '"' & @CRLF & 'del ' & @TempDir & '\runsvc.bat'
    FileWrite(@TempDir & "\runsvc.bat", $sCmdFile)
    Run(@TempDir & "\runsvc.bat", @TempDir, @SW_HIDE)
    Exit
EndFunc

; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet
    
    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "ptr", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc  ;==>SocketToIP
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...