windowsでプログラムを起動してチェックします。一例だけ起動します。

959 ワード


 
問題のソース:http://bbs.csdn.net/topics/390998279?page=1菵post-33983061
 
 
 
 
// Only_once.cpp :              。
//





//   <<windows    >>
#include "StdAfx.h"
#include <iostream>
#include <windows.h>
using namespace std;

#define MUTEX_NAME     TEXT("Global//onename")//onename     

bool IsSingleProcess()
{
	HANDLE hMutex = CreateMutex(NULL, FALSE, MUTEX_NAME);
	if (GetLastError() == ERROR_ALREADY_EXISTS)
	{
		::CloseHandle(hMutex);
		return FALSE;
	}
	else
	{
		return TRUE;
	}
}


//   : exit()
//
//		      :stdlib.h
//
//		    :       ,         。
//
//		 exit(1)      .  1         。
//
//		 exit(x)(x  0)       
//
//		 exit(0)      
//
//		 exit()              ,  UNIX,Linux, MS DOS,        。
int main()
{
	if (!IsSingleProcess())
	{
		cout << "already exist" << endl;
		getchar();
		exit(1);
	}
	Sleep(100000);
	return 0;
}