我想用一个文件保存变量的方法:当想运行该程序的时候,首先从文件读取该变量并判断该变量值以决定是否再运行程序.但我觉得这种方法麻烦了点,在MFC中还有没有更好的方法呢 ?

解决方案 »

  1.   

    创建系统内核变量吧.Mutex 之类的...
    google里应该很容易找到
      

  2.   

    用mutex呀。《Windows核心编程》里面有例子。
      

  3.   

    在APP的InitInstance中开头加入如下代码  
      HANDLE   hMutex=::CreateMutex(NULL,TRUE,"FirstName");//FirstName可以随便取一个唯一的名字  
      if   (hMutex!=NULL)  
      {  
      if   (GetLastError()==ERROR_ALREADY_EXISTS)  
      {  
      AfxMessageBox("已经有一个程序运行");    }
      

  4.   

    多谢 zhaohan7(热血小赵) !
    把你这段代码加到程序里直接可以用,,呵呵,谢谢!
      

  5.   

    好好看看WinMain函数几个参数的说明hInstance 
    [in] Handle to the current instance of the application. 
    hPrevInstance 
    [in] Handle to the previous instance of the application. This parameter is always NULL. 
    If you need to detect whether another instance already exists, create a uniquely named mutex using the CreateMutex function. CreateMutex will succeed even if the mutex already exists, but the GetLastError function will return ERROR_ALREADY_EXISTS. This indicates that another instance of your application exists, because it created the mutex first.这就是微软建议你这样做的,其实互斥对象内部也是使用了内存映射文件,内存映射文件共享数据变量.........