Latest News
Showing posts with label mutex. process. Show all posts
Showing posts with label mutex. process. Show all posts

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

Posted by genesisdatabase on Tuesday, 3 August 2010 , under , , , , , , , , , | comments (0)



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.

C


[code]
#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);
}
else
{
MessageBox(0, "Instance Exists!", 0, 0);
return 0;
}

return 0;
}
[/code]

VB .NET


[code]
Function PrevInstance() As Boolean
If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
Return True
Else
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
End If
End Sub
[/code]