读书人

这段代码为什么在VS 2003运行正常在

发布时间: 2012-02-26 20:19:45 作者: rapoo

这段代码为什么在VS 2003运行正常,在VS 2005运行出错的?
private void Form1_Load(object sender, EventArgs e)
{
IntPtr hMutex;
hMutex=CreateMutex(null,false, "test ");
if (GetLastError()!=ERROR_ALREADY_EXISTS)
{
Application.Run(new Form1());
}
else
{
MessageBox.Show( "本程序只允许同时运行一个 ");
ReleaseMutex(hMutex);
}
}

[StructLayout( LayoutKind.Sequential)]
public class SECURITY_ATTRIBUTES
{
public int nLength;
public int lpSecurityDescriptor;
public int bInheritHandle;
}
[System.Runtime.InteropServices.DllImport( "kernel32 ")]
private static extern int GetLastError();
[System.Runtime.InteropServices.DllImport( "kernel32 ")]
private static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes,bool bInitialOwner,string lpName);
[System.Runtime.InteropServices.DllImport( "kernel32 ")]
private static extern int ReleaseMutex(IntPtr hMutex);
const int ERROR_ALREADY_EXISTS = 0183;

[解决办法]
up

读书人网 >C#

热点推荐