Solved

Background process

Posted on 2000-02-18
7
194 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
Industry Leaders: 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

707 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