Solved

Background process

Posted on 2000-02-18
7
179 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Delphi selector screen 2 58
Downloading email attachments 2 55
Dev Express grid collapse 2 35
Dynamically Created Query 3 9
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

12 Experts available now in Live!

Get 1:1 Help Now