[StructLayout( LayoutKind.Sequential)]
public class SECURITY_ATTRIBUTES
{
public int nLength;
public int lpSecurityDescriptor;
public int bInheritHandle;
}
const int ERROR_ALREADY_EXISTS = 0183;
然后编写代码
static void Main()
{
#region Api_Call CreateMutex;
IntPtr hMutex;
hMutex=CreateMutex(null,false,"程序名");
if (GetLastError()!=ERROR_ALREADY_EXISTS)
{
Application.Run(new Frmmenu());
}
else
{
MessageBox.Show("本程序只允许同时运行一个");
ReleaseMutex(hMutex);
}
#endregion
}
3、有时我们希望自己的程序能做到激活一个窗体,但在 .NET Framework 中没有函数可以激活属于另外一个进程或程序的窗体。所以,我们还是要调用API函数来实现:
using System.Runtime.InteropServices;
[DllImport("coredll.Dll")]
public static extern IntPtr FindWindow(String classname, String title);