Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (ByVal lpMutexAttributes As Integer, ByVal bInitialOwner As Integer, ByVal lpName As String) As Integer Declare Function GetLastError Lib "kernel32" Alias "GetLastError" () As Integer Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Integer) As IntegerConst ERROR_ALREADY_EXISTS = 183& Dim hwnd As Integer Private Function CMutex() As Boolean hwnd = CreateMutex(0, 1, "随便起个名字") If GetLastError = ERROR_ALREADY_EXISTS Then MessageBox.Show("一个应用程序的实例已被打开", "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop) Return False End If Return True End Function'最后程序退出的时候要 CloseHandle(hwnd)
参考这篇文章http://blog.csdn.net/gaoyunpeng/archive/2008/05/16/2452052.aspx
Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (ByVal lpMutexAttributes As Integer, ByVal bInitialOwner As Integer, ByVal lpName As String) As Integer
Declare Function GetLastError Lib "kernel32" Alias "GetLastError" () As Integer
Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Integer) As IntegerConst ERROR_ALREADY_EXISTS = 183&
Dim hwnd As Integer
Private Function CMutex() As Boolean
hwnd = CreateMutex(0, 1, "随便起个名字")
If GetLastError = ERROR_ALREADY_EXISTS Then
MessageBox.Show("一个应用程序的实例已被打开", "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Return False
End If
Return True
End Function'最后程序退出的时候要
CloseHandle(hwnd)
Process[] prc = Process.GetProcessesByName(name);
if (prc.Length > 1)
{
MessageBox.Show("当前程序已启动");
return;
}