WINCE中只允许程序运行一个实例

View Code
//.NET CF:   
  
[DllImport(
"coredll.Dll")]    
private static extern int GetLastError();    
  
[DllImport(
"coredll.Dll")]    
private static extern int ReleaseMutex(IntPtr hMutex);    
  
[DllImport(
"coredll.Dll")]    
private static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes,bool bInitialOwner,string lpName);    
  
[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    
}   
  
    EVC:       
    HANDLE   hMutex
=::CreateMutex(NULL,TRUE,L"LiCR");     
    
if(hMutex!=NULL)      
    {      
        
if(GetLastError()==ERROR_ALREADY_EXISTS)      
        {                
            AfxMessageBox( 
"已经有一个程序运行." );                   
            ReleaseMutex(hMutex);               
            
return  FALSE;              
        }          
  
    }   
  
  
//本文转自:http://blog.csdn.net/jimmy54/archive/2010/01/21/5221508.aspx  
posted @ 2011-03-16 23:15  MrNobody_123456  阅读(225)  评论(0编辑  收藏  举报