Solved

Background process

Posted on 2000-02-18
7
186 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

813 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

14 Experts available now in Live!

Get 1:1 Help Now