[DllImport("coredll.dll", SetLastError=true)]
public static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, bool bInitiaOwner, string lpName);
// Not needed due to using the SetLastError=true
// use Marshal.GetLastWin32Error() to retrieve the error.
//[DllImport("coredll.dll")]
//public static extern int GetLastError();
const long ERROR_ALREADY_EXISTS = 183;
It should be used in conjunction with Marshal.GetLastWin32Error() to check the application already existed in the process list.
private static bool CheckInstance()
{
CreateMutex(IntPtr.Zero, true, "<Application Name>"); // to be replaced by the name of your application
return Marshal.GetLastWin32Error() == ERROR_ALREADY_EXISTS;
}