views:

1393

answers:

8

Here's the idea, I'd like to make a service? that will look for a certain program starting up and dissallow it unless certain conditions are met.

Let's say I have a game I want to stop myself from playing during the week. So if I start it up on any day other than Friday/Saturday/Sunday, it will intercept and cancel. Is this possible with C#?

Main thing I am looking for is how to catch a program starting up, rest should be easy.

A: 

I'm not sure if you can catch it starting up, but you could try to look for the program in the list of windows (was it ENUM_WINDOWS? I can never remember) and shut it down as soon as it shows up.

You could probably even do this in AutoIt!

Drag out the Petzold and have some fun with windows...

EDIT: Check out sysinternals for source on how to list the active processes - do this in a loop and you can catch your program when it starts. I don't think the official site still has the source though, but it's bound to be out there somewhere...

Daren Thomas
A: 

Hrm, rather than intercepting it's startup, maybe your service could somehow sabotage the executable (i.e. muck with the permissions, rename the EXE, replace the EXE with something that scolds you for your weak will, etc).

If that's not good enough, you can try one of these approaches:

http://www.codeproject.com/KB/system/win32processusingwmi.aspx

http://msdn.microsoft.com/en-us/magazine/cc188966.aspx

chadmyers
+3  A: 

I don't know about C# in particular here but one why you could accomplish this (I'll be it is a dangerous way) is by using the Image File Execution Options (http://blogs.msdn.com/junfeng/archive/2004/04/28/121871.aspx) in the registry. For whatever executable you are interested in intercepting you could set the Debugger option for it and then create a small application that would be used at the debugger then have it essentially filter these calls. If you wanted to allow it to run then start up the process otherwise do whatever you like. I’ve never attempted this but it seems like it could do what you want.

Or if you wanted to react to the process starting up and then close it down you could use a ProcessWatcher (http://weblogs.asp.net/whaggard/archive/2006/02/11/438006.aspx) and subscribe to the process created event and then close it down if needed. However that is more of reactive approach instead a proactive approach like the first one.

Wes Haggard
+4  A: 

Well, you can definitely determine which programs are running by looking for the process names you want (GetProcessesByName()) and killing them.

Process[] processes = Process.GetProcessesByName(processName);
foreach(Process process in processes)
{
   process.Kill();
}

You could just have a list of them you didn't want to run, do the time check (or whatever criteria was to be met) and then walk the list. I did something like this once for a test and it works well enough.

itsmatt
This sounds like a good option; have it run every five minutes or so...enough to let you get into it, only to have it suddenly die right out from under you. Annoying enough to teach a lesson. :)
Kyralessa
Particularly if you are close to finishing the level or getting the high score. :) It's definitely abrupt.
itsmatt
A: 

There's always hooks. Although there's no support for it in the .net library you can always wrap the win32 dll. Here's a nice article: http://msdn.microsoft.com/sv-se/magazine/cc188966(en-us).aspx

This is low-level programming, and if you want your code to be portable i wouldn't use hooks.

Kristian
A: 

This app will do just that Kill Process from Orange Lamp
I'm not the author.

Niklas Winde
A: 

You don't necessarily need to intercept start up programs. You can write a "program start up manager" app that launches the programs for you (if they are white-listed). After you write the app, all you would need to do is modify your application shortcuts to point to your start up manager with the proper parameters.

Giovanni Galbo
A: 

If you just need to disable the application, you can edit the registry to try and attach a debugger to that application automatically, if the debugger doesn't exist, windows will complain and bail out.

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options] is the key, look up MS article http://support.microsoft.com/default.aspx?kbid=238788 for more info.

Stew