moonrise
asked on
Background process
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_WINDO WEDGE, '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(GetCurren tProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, vToken);
vTp.PrivilegeCount := 1;
if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', vTp.Privileges[0].LUID) then
begin
vTp.Privileges[0].Attribut es := SE_PRIVILEGE_ENABLED;
vDwRetLen := 0;
AdjustTokenPrivileges(vTok en, False, vTp, SizeOf(vTpPrev), vTpPrev, vDwRetLen);
end;
CloseHandle(vToken);
ExitWindowsEx(EWX_REBOOT or EWX_FORCE, 0);
end;
Sleep(5000);
end;
end.
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_WINDO
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',
begin
OpenProcessToken(GetCurren
vTp.PrivilegeCount := 1;
if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', vTp.Privileges[0].LUID) then
begin
vTp.Privileges[0].Attribut
vDwRetLen := 0;
AdjustTokenPrivileges(vTok
end;
CloseHandle(vToken);
ExitWindowsEx(EWX_REBOOT or EWX_FORCE, 0);
end;
Sleep(5000);
end;
end.
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.han dle, 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
Just add the following code to your OnShow method
ShowWindow(application.han
ShowWindow(FindWindow(nil,
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
ASKER
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)?
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)?
TheNeil,
Application.ShowMainform := false; will hide it form the taskbar.
Zif.
Application.ShowMainform := false; will hide it form the taskbar.
Zif.
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
The Neil
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you. It works great without the window.
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(TfM
Application.CreateForm(TfO
Application.ShowMainForm := false; <------!
Application.Run;
end.
Regards, Zif.