良好的程序应该有自我诊断能力
2011-11-09 22:37
295 查看
Good Design的程序应该会报告程序自己的状态,例如内存的堆与栈的使用情况。。。。
// Check by opening a global mutex; if the mutex exists, then an applicaiton using the same name is already running. BOOL IsUniqueInstanceInSystem(LPCWSTR Name) { // simple mechanism to ensure single instance WCHAR MutexName[MAX_PATH]; BOOL retValue = FALSE; const errno_t error=wcsncpy_s(MutexName, _countof(MutexName), Name, CTSTRLEN(MutexName)); ABSASSERT(error==0); // The buffer was not copied. MutexName[CTSTRLEN(MutexName)]='\0'; OSVERSIONINFO OSversion; OSversion.dwOSVersionInfoSize=sizeof(OSVERSIONINFO); GetVersionEx(&OSversion); switch(OSversion.dwPlatformId) { case VER_PLATFORM_WIN32_NT: if (OSversion.dwMajorVersion >= 5) { // Specify that this is a system mutex rather than a Terminal Services session mutex errno_t error; error=wcsncpy_s(MutexName, _countof(MutexName), L"Global\\", CTSTRLEN(MutexName)); ABSASSERT(error==0); // The buffer was not copied. ABSASSERT(wcslen(Name)<=_countof(MutexName)-1-wcslen(MutexName)); error=wcsncat_s(MutexName, _countof(MutexName), Name, _countof(MutexName)-1-wcslen(MutexName)); } break; } // try to open mutex with presctibed name, unique to our application HANDLE hMutex = OpenMutexW(MUTEX_MODIFY_STATE, 0, MutexName); if (hMutex) { // Allow a few seconds to let the previous instance (if any) to // terminate before a new one is started. Otherwise, user attempt // to start more than one session a time. CloseHandle (hMutex); Sleep (5000); hMutex = OpenMutexW(MUTEX_MODIFY_STATE, 0, MutexName); } if (!hMutex) { // otherwise, we are the first instance, so we can create the Mutex and continue PSECURITY_DESCRIPTOR pSD; // Initialize a security descriptor. pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH); if (pSD != NULL) { if (InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)) { // Add a NULL descriptor ACL to the security descriptor. if (SetSecurityDescriptorDacl(pSD, TRUE, (PACL) NULL, FALSE)) { SECURITY_ATTRIBUTES sec_attr; sec_attr.nLength=sizeof(SECURITY_ATTRIBUTES); sec_attr.lpSecurityDescriptor=pSD; sec_attr.bInheritHandle=TRUE; hMutex = CreateMutexW(&sec_attr, TRUE, MutexName); } } LocalFree((HLOCAL) pSD); } retValue = (hMutex != NULL); } return retValue; }
相关文章推荐
- 一段良好的程序永远不应该发生panic异常
- 项目经理应该具备的能力 和自我评估
- 程序的自我修养-链接、装载与库
- 旧文章:用c语言实现程序的自我复制 (2008-08-30 16:01:36)
- Event_Handler在程序退出前应该自己关闭
- 为什么我要竖向对齐程序代码,你也应该这样做
- 微信小程序之-------循环嵌套应该怎样取值
- 学习汇编前你应该知道的知识(四)------汇编程序
- Web架构师应该具备的能力
- Cordova 7 架构学习 良好程序结构要点
- 让 Eclipse 插件程序具有二维作图能力
- 写程序10大习惯和如何提高编程能力(不知道被转了几百次了找不到原出处了) 【转载】_beyond yourself--思路决定出路
- win2003 下配置 asp 应该程序
- 程序的自我删除
- ios-依赖系统语言的应该程序名字的国际化
- 一名普通程序员的自我诊断书
- 服务端程序应该掌握的知识
- 高级程序员应该具备什么能力
- g++ 4.4.7 template 没问题,前面应该程序问题!!