Solved

Keep process running while logoff...

Posted on 1998-10-13
18
347 Views
Last Modified: 2010-04-06
How can my process keep running while the user logs off and on again? A little source example would be great!

Thanx, Madshi.
0
Comment
Question by:Madshi
  • 8
  • 3
  • 2
  • +4
18 Comments
 
LVL 1

Expert Comment

by:AndersWP
ID: 1342666
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.

Regards,
AndersWP
0
 
LVL 20

Author Comment

by:Madshi
ID: 1342667
AndersWP,

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.
0
 

Expert Comment

by:JPiispa
ID: 1342668
Service is Delphi 4 C/S feature only and it's only for Windows NT.
0
 
LVL 1

Expert Comment

by:AndersWP
ID: 1342669
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!

Regards,
AndersWP
0
 
LVL 20

Author Comment

by:Madshi
ID: 1342670
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.
0
 

Expert Comment

by:Tazi
ID: 1342671
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
Tazi
0
 
LVL 20

Author Comment

by:Madshi
ID: 1342672
Tazi,

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...
0
 

Expert Comment

by:Tazi
ID: 1342673
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
0
 
LVL 7

Expert Comment

by:BlackMan
ID: 1342674
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..
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 7

Expert Comment

by:ahalya
ID: 1342675
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 !)
0
 
LVL 20

Author Comment

by:Madshi
ID: 1342676
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.
0
 
LVL 20

Author Comment

by:Madshi
ID: 1342677
Adjusted points to 77
0
 

Expert Comment

by:dejohn99
ID: 1342678
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;

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

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

      Syntax:
            dtableipserver [S]

      Options:
            s[ervice]:
            S[ERVICE]: run as an NT service

====================================================}
var
      buf: String;
begin
      Application.Initialize;
      buf := ParamStr (1);
      if (buf <> '') and (buf [1] in ['s','S']) then begin
            { run as NT service }
            fmMain := TfmMain.Create (Application);
            repeat
                  Application.HandleMessage;
                  sleep (0);
            until Application.Terminated;
            fmMain.Destroy;
      end
      else begin
            { Run normally }
            Application.CreateForm(TfmMain, fmMain);
            fmMain.Show;
            fmMain.Timer1Timer (Application);
            Application.Run;
      end;
end.
0
 
LVL 20

Author Comment

by:Madshi
ID: 1342679
dejohn99,

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.
0
 
LVL 7

Accepted Solution

by:
ahalya earned 110 total points
ID: 1342680
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;

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


//===============================
//Hope this helps..

0
 
LVL 20

Author Comment

by:Madshi
ID: 1342681
ahalya,

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.
0
 
LVL 7

Expert Comment

by:ahalya
ID: 1342682
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).

0
 
LVL 20

Author Comment

by:Madshi
ID: 1342683
ahalya,

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

Regards, Madshi.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now