Creating application with a single instance in C and VB .NET

Most of us prefer to have single instances application to make it look professional or probably some other personal reason especially making malicious applications too.  Here is the source code for the 2 languages that has been mentioned.


#include <windows.h>

int main()
	char *mutex = "some name here";

	HANDLE hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, mutex);
	if(hMutex == NULL)
		hMutex = CreateMutex(NULL, FALSE, mutex);
		MessageBox(0, "Instance Exists!", 0, 0);
		return 0;

	return 0;


Function PrevInstance() As Boolean
	If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
            Return True
            Return False
        End If
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	If PrevInstance() = True Then
            MsgBox("Instance Exists!")
        End If
End Sub

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: