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
iowensAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.