Keep process running while logoff...

How can my process keep running while the user logs off and on again? A little source example would be great!

Thanx, Madshi.
LVL 20
Who is Participating?
ahalyaConnect With a Mentor Commented:
Here goes the code that will work in Win95.  

//this declaration goes just after implementation
function RegisterServiceProcess (ThreadID : THandle; Flags : Integer):
Integer; stdcall; external kernel32 name 'RegisterServiceProcess';

//and this goes in FormCreate

if RegisterServiceProcess (0, 0) = 0 then
   ShowMessage ('Error Registering Service')
  else   Application.HookMainWindow (MsgHook);

//and this defines the hook

function TForm1.MsgHook (var Msg : TMessage) : Boolean;

if Msg.Msg = WM_ENDSESSION then
   Result := (Msg.lparam <> 0);{set to true if logging on as different user}

//Hope this helps..

I believe this is just what a service application is about: A program that starts before any user is logged on, and keeps running until the machine is shut down.  If you have Delphi 4, look under Service Application in the help file, and make your application with the File | New | Service Application function in D4.

MadshiAuthor Commented:

I looked at the service application help texts right before asking this question. My first problem was that I have no Fil|New|ServiceApplication icon. So I did what I think Delphi does when you click on this icon by myself. But there was no change in the behaviour of this test program. It was still closed by logoff. My second problem is: In the help file they say that I must not import "Forms.pas". But without this unit my program doesn't run!

Regards, Madshi.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Service is Delphi 4 C/S feature only and it's only for Windows NT.
Madshi, It seems that you are right - D4 will not allow you to make forms in a service application. Could a service program activate another (normal) program, which could then show your forms?

According to the D4 VCL hierarchy diagram, TServiceApplication should be included in the Proffesionel edition and above; I am running the D4 C/S edition, so I can not tell if this is correct.

JPiispa points out that services are an NT concept. Is your application NT only, or are we talking 95/98 too? For 95/98 you would be talking about the situation where the user does a "Shutdown | Close all programs and log on as different user", or what?

JPiispa, you must have hit the wrong button with your proposed answer. While your comment is correct, surely is does not solve Madshi's problem!

MadshiAuthor Commented:
JPiispa, AndersWP is right. Your comment maybe correct, but doesn't help me. Sorry.

AndersWP, I've the Professional Edition, but no ServiceApplication icon in the object gallery.
We're talking about win95/98. And I mean the "Shutdown|Close..." menu item. You're right.
It would be too much work to devide my program into two programs - one without forms and one with forms. If there is no other possibility, I'll have to live with the status quo...
Any ideas?

Thanx for your time...
Regards, Madshi.
Well, if you use the timer component, and you place your code on the OnTimer event, the process will run until such time that u disable the timer by using the following code

    Timer1.Enabled := False;

Hope this is what u looking for..

Kind Regards
MadshiAuthor Commented:

sorry - that is not my problem. My problem is that when the user logs off (using shutdown|Close all programs and log on as different user on a win95/98 computer) my program is shutdown, too. So a timer won't help me. I just want my program not to be closed...
Madshi...Sorry, I didn't understand what u ment

Well if i have anything to help u i will keep in touch..

Sorry again
Regards Tazi
The Service skeleton is included in D4 Professional and C/S, but for some unknown reason, the wizard is only included in the C/S version?! I have seen somewhere that a currently unknown person has made a wizard for the professional version, but of cource I don't remember where I saw it :-)
Regarding your problem, although Services is a NT thing, there is something simular in Win95/Win98.
In NT you can make services that interact with the desktop (see the Interactive property on TServerThread), but since I've never made any services in Delphi, I don't know what can go wrong if you include Forms. One thing is for sure, to make your application a service takes a great deal of re-coding!
I think, but I'm not sure, that a service in Win95/98 is more like a normal program.
All in all, you'll make yourself a lot of extra work if you are going to make your program as a service...

Hope this can help you clear some things..
if you need it to be done for Win95, then you register your program as a "simpleservice" (unfortunately there are no "simple" services in NT).

if you're programming for Win95 then i can show you the trick ! (and btw, i still use Delphi 2 !)
MadshiAuthor Commented:
Tazi, doesn't matter... :-)

BlackMan, thanx for your comment. I cannot afford much re-coding. So if this is the only way to solve my problem, it will remain unsolved...  :-(

ahalya, that sounds interesting. I need my program to run primary for Win95. So I'm quite interested in your trick. Please tell me more! (In my win32 help file I found nothing like a simpleService...).

Thanx all of you for your comments, regards, Madshi.
MadshiAuthor Commented:
Adjusted points to 77
I am just beginning to do Windows NT services.  By the way - I am using D3 professional.

The bare minimum sevices requirements are:
    1.  A service must be a command line application
    2. A service must respond to windows events

The following code fragment illustrates these issues, but it still does not address responding to the services control panel.  (Any others have suggestions?)

It also has a problem with handling WM_QUIT and WM_SHUTDOWN messages.

program DTableIPServer;

  Forms, Windows,
  uMain in 'uMain.pas' {fmMain};

{$R *.RES}
      Command line service / desktop application to
      service the synchronization requirements of the
      distributed table components.

            dtableipserver [S]

            S[ERVICE]: run as an NT service

      buf: String;
      buf := ParamStr (1);
      if (buf <> '') and (buf [1] in ['s','S']) then begin
            { run as NT service }
            fmMain := TfmMain.Create (Application);
                  sleep (0);
            until Application.Terminated;
      else begin
            { Run normally }
            Application.CreateForm(TfmMain, fmMain);
            fmMain.Timer1Timer (Application);
MadshiAuthor Commented:

thanx for your comment. But how does this help resolving my problem? I mean, this program will be closed if the win95 user logs off, too.      :-(
Or am I wrong?

Regards, Madshi.
MadshiAuthor Commented:

somehow it doesn't work! If I use it exactly the way you wrote it there comes a compiler warning because in the MsgHook function the return value is not always defined. If I run the program with ignoring this compiler warning and then try to log off, the logOff action is cancelled by my program. If I add a "result:=false" in the first line of the MsgHook function (I think that's the way you thought it), my program is still closed at logOff.   :-(
Furthermore, I've something in mind that service processes will not be in the tasklist (Ctrl+Alt+Del). My program is still in this list.
Are you sure about the second parameter in RegisterServiceProcess? In my help file they talk about two constants. But I've no header file (neither *.pas nor *.h) where these constants are defined...

Thanx for your time.
Regards, Madshi.
ooops ! i'm sorry. the second parameter in the call to "RegisterServiceProcess" should have been 1 (not zero.  Actually 0  "unregisters the program" and that's why your program appears in the task list)

also you're right about defining result in "MsgHook". i normally define it as :
Result := (Msg.msg = WM_ENDSESSION) and (Msg.lparam <> 0);

Hope (heck it should, but with M$ Windoze, you never know) it works now...

i don't have much info on "RegisterServiceProcess" function, except that the first parameter should always be zero and a 1 as the second parameter will register your program as a service (and a 0 as the second parameter will unregister it).

MadshiAuthor Commented:

thank you very much! Now it works perfectly!!!     :-)

Regards, Madshi.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.