• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

Detecting External Processes

I'm writing a screen saver and a requirement is that when any other process is started the screen saver quits (unless password is enabled).

Does any one have any idea how to do this in Delphi 2
0
iowens
Asked:
iowens
1 Solution
 
PeterLarsenCommented:
Windows has a event that is triggeret when windows is waiting (some kind of a idle-timer).
I dont remember, but try search for IDLE in windows programmers reference.
0
 
schutnikCommented:
The only thing I see so far is that you could set a windows Hook procedure for the WH_SHELL type notifications and look for a HSHELL_WINDOWCREATED in the ShellProc nCode parameter...

It would look something like this (In D2)

var hHook: Integer;

function MyShellProc(nCode: Integer; wParam, lParam: Integer): Integer; stdcall;
begin
  Result := 0; // MSDN says return 0 always
  case (nCode) of
    HSHELL_WINDOWCREATED: begin
      // close your screen saver window
      //   if it is open, else just go
      //   on to rest of this function
      // (you should always call CallNextHookEx)
    end;
  else
    // do nothing
  end;
  CallNextHookEx(hHook, nCode, wParam, lParam);
end;

// to install the hook
// this should probably go in around
//   the Application.Run in the Project
//   source. (Set above, Unhook below)
begin
  hHook := SetWindowsHookEx(WH_SHELL, @MyShellProc, NULL, 0);
  if hHook = 0 then
    // error occurred, handle it
    ;
  // do some work
  UnhookWindowsHookEx(hHook); // delete the hook
end;

Hope this is good enough for your needs.
If you need some of the definitions of these functions (they should be in the windows unit), they can be found on MSDN or I can send them to you if you contact me by email (remove the not. in the email address below)

kleahy@rgare.not.com
0
 
schutnikCommented:
BTW, in addition to my proposed answer, there is another way to do the same thing, but it is only supported on NT4 Service Pack 4 or Windows 95 & higher.

The proposed answer will work on all 32-bit versions of Windows.

If you are interested in the other answer, look up SetWinEventHook in MSDN.
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
schutnikCommented:
BTW, in addition to my proposed answer, there is another way to do the same thing, but it is only supported on NT4 Service Pack 4 or Windows 95 & higher.

The proposed answer will work on all 32-bit versions of Windows.

If you are interested in the other answer, look up SetWinEventHook in MSDN.
0
 
iowensAuthor Commented:
This Seems to Terminate the process starting not the screen saver
0
 
schutnikCommented:
Actually, it doesn't do either.

I was just being stupid when I wrote the above answer.

Try this...

Make a dll project with this project source...

library hookDll;

uses
  SysUtils,
  Classes,
  windows,
  dialogs;

{$R *.RES}

var hHook: Integer;

function MyShellProc(nCode: Integer; wParam, lParam: Integer): Integer; stdcall;
begin
  case (nCode) of
    HSHELL_WINDOWCREATED: begin
      // close your screen saver window
      //   if it is open, else just go
      //   on to rest of this function
      // (you should always call CallNextHookEx)
      ShowMessage('Window Created');
    end;
  else
    // do nothing
  end;
  Result := CallNextHookEx(hHook, nCode, wParam, lParam);
end;

function InstallHook(hModule: THandle): Boolean; stdcall;
begin
  hHook := SetWindowsHookEx(WH_SHELL, MyShellProc, hModule, 0);
  Result := hHook <> 0;
end;

procedure RemoveHook; stdcall;
begin
  if hHook <> 0 then
    UnhookWindowsHookEx(hHook);
end;

exports
  InstallHook,
  RemoveHook;

begin
  hHook := 0;
end.

---------------------------------------
Now make an application (in the same directory) with the following source for a form(just a sample usage).

// header & type declarations omitted
// for brevity, this is a standard form
// with only a single button on it.
// be sure to attach the methods properly
// otherwise, you will get compile errors
// i.e. use the IDE to create the event handlers

function InstallHook(hModule: THandle): Boolean; stdcall; external 'hookDll.dll';
procedure RemoveHook; stdcall; external 'hookDll.dll';

procedure TForm1.Button1Click(Sender: TObject);
begin
  InstallHook(GetModuleHandle('hookDll.dll'));
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  RemoveHook;
end;

----------------------------------------

Now, this code will produce a lot of message boxes if you let it run like this, so only open a program with a very small number of windows (controls) like notepad.exe, otherwise, you'll be clicking ok for a long time!

Good luck,
Kelly
0
 
rwilson032697Commented:
Listening
0
 
plasmatekCommented:
You could create a VxD which detects when a program is run, which is how real-time virus scanners work.
I have no idea how to do this however.

Listening...
0
 
schutnikCommented:
A VxD would be able to detect that information, but you would find yourself writing two completely different implementations for 95/98 & NT (maybe even more) and also, Delphi can't do VxDs (unless they changed something I don't know about) because they use a different executable format.

Also, for a screen saver program, it seems pretty strange to require a user to install a device driver!  I certainly wouldn't be interested in any screen saver that required me to install a VxD (that would be a lot like a virus in my opinion), since I don't have any idea what that VxD does (and it can actually do anything as a device driver)

Kelly
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now