Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Nov : Re: Converting working Win32 app to Windows Service - using TServerSocket

www.cryer.info
Managed Newsgroup Archive

Re: Converting working Win32 app to Windows Service - using TServerSocket

Subject:Re: Converting working Win32 app to Windows Service - using TServerSocket
Posted by:"Remy Lebeau (TeamB)" (no.spam@no.spam.com)
Date:Tue, 20 Nov 2007 21:58:43

"Warrick Wilson" <warrick@cwwilson.com> wrote in message
news:47432c64$1@newsgroups.borland.com...

> I want to convert it to a Windows service, so I've dug up a few
> articles on writing Windows services in Delphi, and followed the
> basic outlines. Everything's good until I get to the point where I
> have to fill in the Execute part. Based on my reading, and my
> Googling, I've still not gotten anything to work. And I'll freely
> admit I'm guessing at some of this - it's my first serious foray into
> sockets and services.

I strongly suggest you not use OnExecute at all.  In fact, you don't even
need it.  Most of your code is better suited for the OnStart and
OnStop/Shutdown events anyway.  For example:

    procedure TmyService.ServiceStart(Sender: TService; var Started:
Boolean);
    var
        path: string;
    begin
        fCurrRecvState := recvWaiting;
        FCurrMsg := '';
        FMsgReceived := false;
        FOutputWritten := false;
        FLastWrittenMsg := '';

        fIniFile := ExtractFilePath(ParamStr(0)) + iniFile;
        LoadIniSettings;

        path := ExtractFilePath(FOutputFileName);
        try
            ForceDirectories(path);
        except
            // Swallows the exception ... BAD, BAD, BAD
        end;

        ServerSocket1 := TServerSocket.Create(Self);
        ServerSocket1.ServerType := stNonBlocking;
        ServerSocket1.Port := fListenPort;
        ServerSocket1.OnClientRead := ServerSocket1ClientRead;
        ServerSocket1.Active := true;

        Started := True;
    end;

    procedure TmyService.ServiceStop(Sender: TService; var Stopped:
Boolean);
    begin
        ServiceShutdown(Sender);
        Stopped := True;
    end;

    procedure TmyService.ServiceShutdown(Sender: TService);
    begin
        ServerSocket1.Active := false;
        FreeAndNil(ServerSocket1);
    end;

> I'd also tried logging some info to a debug log to trace the
> progress (sadly, because of the network setup required to
> get the feed into our location, I can't tap into it on my dev
> machine, but it only runs on a test box that I can't do too
> much to as far as instrumenting for debug).

TService has its own LogMessage() method that logs to Windows's own Event
Log.


Gambit

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive