Solved

Background process

Posted on 2000-02-18
7
183 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Machine not responding during CopyFile() 3 89
Help on project with Soap 10 48
Delphi Mdi application Child forms get behind control 7 154
find a node in VST 2 63
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 The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

18 Experts available now in Live!

Get 1:1 Help Now