What is a good way to limit a process to a single instance. As in attempting to start another process while one is already running it could detect this..?
I'm looking for something un-obvious like checking the process list.
What is a good way to limit a process to a single instance. As in attempting to start another process while one is already running it could detect this..?
I'm looking for something un-obvious like checking the process list.
not checking the process list makes it sound as if you don't want it hacked? ;p
you have a great deal of options but not many that are going to stump a veteran. here are some i can think of at the moment:
try a named synchronization object like a mutex or event. maybe even a section object, those seem to be popular methods.
Last edited by sitnspinlock; 12-03-2012 at 10:15 PM.
Everything can be solved with a rootkit. If you are looking for a quick fix that will work its not going to be easy. I have seen some software you can buy that will do the trick with drivers.
on start-up, check if process is already running, and if so, exit current process?
main () {
_allProcesses = GetAllRunningProcesses();
if _allProcesses.Contains (myAppName)
Application.Exit();
end if
}
edit: once your process has started, there should be 2 copies of 'myAppName' running -- check for .Contains() twice and exit on that ?
Some things that can be counted, don't matter. And some things that matter, can't be counted.
Use a mutex
The common solution is to use a named mutex. Though like everdox said, it sounds like you're looking for something a little more 'stealthy'... Perhaps you could tell us why it needs to be 'un-obvious'?
Last edited by abuckau907; 12-04-2012 at 04:21 AM.
Some things that can be counted, don't matter. And some things that matter, can't be counted.
Checking the task manager - any reason you mention doing it this way? It's unobvious, but for a reason - why do it?
process1
process1' <--second occurrence
You say "could it detect this"<-- so you want process1 to 'detect' that process1' is loading/starting up, and kill it ? That's the hard way? Instead make process1' check if process1 is running, and if so, cancel loading. This is the 'usual' way to do it. Unless you have a specific reason to not do it this way? Use regex for string comparison if you want*
example in vb.net
c#Code:Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim _allProcesses() As Process = Process.GetProcesses() Dim _myAppName As String = Process.GetCurrentProcess.ProcessName Dim _foundOnce As Boolean = False For Each pp As Process In _allProcesses If pp.ProcessName = _myappname Then If _foundOnce = False Then 'first occurance _foundOnce = True Else Me.Exit() ''Application.Exit() End If End If Next '' If you get here, there was no 2nd copy found. '' Continue as normal. '' ... End Sub *this assumes the process name is constant. If yours isn't, please give more project details and I'll try to update the demo. *if you're not on .Net you might have to code some of the Process() functions yourself - do you have a specific project/language in mind?
Code:private void Form1_Load(object sender, EventArgs e) { Process[] _allProcesses = Process.GetProcesses(); string _myAppName = Process.GetCurrentProcess().ProcessName; //.exe's filename bool _foundOnce = false; foreach (Process pp in _allProcesses) { if (pp.ProcessName == _myAppName) { if (!_foundOnce) _foundOnce = true; else this.Close(); //auto exit, no warning to user. } } //if you get here, this is the only copy running. Continue as normal. //.. }
Last edited by abuckau907; 12-06-2012 at 10:53 AM.
Some things that can be counted, don't matter. And some things that matter, can't be counted.
edit: removed flame
Last edited by abuckau907; 12-05-2012 at 08:20 PM.
Some things that can be counted, don't matter. And some things that matter, can't be counted.
@namreeb: ok, not what I wanted to hear ofc, but thnx. Not taken, I wrote it for mixtape, not you--in my first post, i had a quazi-c pseudocode..i should have taken the extra 2 seconds to replace end if w/ } and wrap the if in ( )'s. I'll consider that next time I post code.
Last edited by abuckau907; 12-05-2012 at 09:40 PM.
Some things that can be counted, don't matter. And some things that matter, can't be counted.
edit: removed flame
Last edited by abuckau907; 12-05-2012 at 08:16 PM.
Some things that can be counted, don't matter. And some things that matter, can't be counted.
edit: removed flame
Last edited by abuckau907; 12-05-2012 at 08:16 PM.
Some things that can be counted, don't matter. And some things that matter, can't be counted.
@dark
[I assume] he wasn't asking how to hide it from the task manager, or anything else hacker related?
It's still back there on page 1,show me where it says he's trying to hide his process from the task manager? Or anything else 'hacker-ish.' If he had, my answer would have reflected that. --He just needs basic programming help! "unobvious" != "anti-hacker" --maybe* that is what he meant, but it's not really clearly defined in the post.
My method works for what he asked for.. Your comment was rude and unwarranted. Just because YOU won't read it, doesn't mean the user who asked the question won't read it to help learn some core concept.
edit: massive edits /flames removed
Last edited by abuckau907; 12-05-2012 at 09:41 PM.
Some things that can be counted, don't matter. And some things that matter, can't be counted.