Solved

Background process

Posted on 2000-02-18
7
190 Views
Last Modified: 2010-04-04
I am trying to build a program that always runs but which is not visible. I did it as follows but whenever I start it as SW_HIDE the code inside it does not seem to be running. Is there a way to have the code running and hidden?

program monitor;

uses
  Windows,
  Messages;

var
  WinClass: TWndClassA;
  Handle:  Integer;
  Inst: Integer;
  Msg: TMsg;
  vTpPrev,
  vTp         : TTokenPrivileges;
  vToken      : THandle;
  vDwRetLen   : DWord;

function WindowProc(hWnd, uMsg,      wParam,      lParam: Integer): Integer; stdcall;
begin
  Result := DefWindowProc(hWnd, uMsg, wParam, lParam);
  if uMsg = WM_DESTROY then
    Halt;
end;

begin
  Inst := hInstance;
  with WinClass do
  begin
    style              := CS_CLASSDC or CS_PARENTDC;
    lpfnWndProc        := @WindowProc;
    hInstance          := Inst;
    hbrBackground      := color_btnface + 1;
    lpszClassname      := 'MONITORWINDOW';
    hCursor            := LoadCursor(0, IDC_ARROW);
  end;
  RegisterClass(WinClass);

  Handle := CreateWindowEx(WS_EX_WINDOWEDGE, 'MONITORWINDOW', 'Kiosk Monitor',
                           WS_VISIBLE,
                           0, 0, 0, 0, 0, 0, Inst, nil);

  while(GetMessage(Msg, Handle, 0, 0)) do
  begin
    TranslateMessage(msg);
    DispatchMessage(msg);

    if FindWindow('TApplication', 'Kiosk') = 0 then
    begin
      OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, vToken);
      vTp.PrivilegeCount := 1;
      if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', vTp.Privileges[0].LUID) then
      begin
        vTp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
        vDwRetLen := 0;
        AdjustTokenPrivileges(vToken, False, vTp, SizeOf(vTpPrev), vTpPrev, vDwRetLen);
      end;
      CloseHandle(vToken);
      ExitWindowsEx(EWX_REBOOT or EWX_FORCE, 0);
    end;
    Sleep(5000);
  end;
end.
0
Comment
Question by:moonrise
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 2534805
mmm, why not just doing this ?


eg;

program pExcel;

uses
  Forms,
  uMain in 'uMain.pas' {fMain},
  uOptions in 'uOptions.pas' {fOptions},
  uAbout in 'uAbout.pas' {fAbout};

{$R *.RES}

begin
  Application.Initialize;
  Application.Title := 'DencelPort';
  Application.CreateForm(TfMain, fMain);
  Application.CreateForm(TfOptions, fOptions);
  Application.ShowMainForm := false;                  <------!
  Application.Run;
end.

Regards, Zif.
0
 
LVL 5

Expert Comment

by:TheNeil
ID: 2534809
You could just run your code as any other application, and position the form offscreen, and hide it from the taskbar.

Just add the following code to your OnShow method

ShowWindow(application.handle, SW_HIDE);
ShowWindow(FindWindow(nil, @Application.Title[1]), SW_HIDE);
Left := Screen.Width;
Top := Screen.Height;

Your app will still appear when you use ALT+TAB but you can trap the form activation and switch to another task.
It's a bit of a cheat solution but it'll do what you want it to do.

The Neil
0
 

Author Comment

by:moonrise
ID: 2534827
I do it this way to keep the program very small (16 K instead of over 200 K).

I tried to position the window outside the screen but then it treats it as if it was hidden.

I always though that a program was either running or not running. In that case the program is running (a process exists) but nothing is happening within the program.

Is there a way to do what I want to do without even creating a window (but is has to work win NT, 95, and 98)?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:ZifNab
ID: 2534831
TheNeil,

Application.ShowMainform := false; will hide it form the taskbar.

Zif.
0
 
LVL 5

Expert Comment

by:TheNeil
ID: 2534858
Thanks Zifnab, I didn't know that. My solution is based on having apps that display but don't appear on the taskbar

The Neil
0
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 2534861
Your program should just run fine. I don't see why it shoudn't work. But for which purpose do you create the window? Why not doing it this way:

program monitor;

uses Windows;

var
  vTpPrev,
  vTp         : TTokenPrivileges;
  vToken      : THandle;
  vDwRetLen   : DWord;

begin
  while true do begin
    if FindWindow('TApplication', 'Kiosk') = 0 then
    begin
      OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, vToken);
      vTp.PrivilegeCount := 1;
      if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', vTp.Privileges[0].LUID) then
      begin
        vTp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
        vDwRetLen := 0;
        AdjustTokenPrivileges(vToken, False, vTp, SizeOf(vTpPrev), vTpPrev, vDwRetLen);
      end;
      CloseHandle(vToken);
      ExitWindowsEx(EWX_REBOOT or EWX_FORCE, 0);
    end;
    Sleep(5000);
  end;
end.

Regards, Madshi.
0
 

Author Comment

by:moonrise
ID: 2535012
Thank you. It works great without the window.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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