Findwindows
互斥量
Atom
互斥量
Atom
解决方案 »
- 请教个关于BitBlt的问题
- 请教关于变量间调用和非模态对话框的问题,谢谢!
- list 控件的替代
- MFC错误谁能解决
- 如何结束一个自动开启的隐藏的子线程!!!
- SQL语句时间比较
- vc的ado连接oracle数据库的问题--原来好用,增加了一些流程控制后竟然连不上了,能分析一下吗
- 讨论:如何用最简单的方法实现局域网中的两台计算机文件互传?
- 在已有的MFC工程中加入一个自定义的CMysocket类进行文件传输
- 高分求解,加分不够者,另开帖子加分!要多少给多少!
- 搞VC对于一个没动手作过项目的来讲周围环境重要吗,假设就你一人来做。劳驾大家帮我出出注意,这可决定我去留问题
- -----------VC的几个小问题,谢谢-------
http://www.codeproject.com/cpp/avoidmultinstance.asp
class CSingleInstance{public: CSingleInstance(); ~CSingleInstance(); BOOL Create(LPCSTR pszClass); CString GetClassName( void ) const;protected: HANDLE m_hMutex; CString m_strClassName;};CSingleInstance::CSingleInstance(){ // Set our default values m_hMutex = NULL;}CSingleInstance::~CSingleInstance(){ if ( m_hMutex != NULL ) { ReleaseMutex( m_hMutex ); }}BOOL CSingleInstance::Create(LPCSTR pszClassName){ // Add the word 'Class' to the end m_strClassName = pszClassName; // Create the mutex m_hMutex = CreateMutex( NULL, FALSE, m_strClassName ); // Check for errors if ( GetLastError() == ERROR_ALREADY_EXISTS ) { // Reset our mutext handle (just in case) m_hMutex = NULL; // The mutex already exists, which means an instance is already // running. Find the app and pop it up HWND hWnd = FindWindowEx( NULL, NULL, m_strClassName, NULL ); if ( hWnd != NULL ) { ShowWindow(hWnd,SW_SHOW); ShowWindow( hWnd, SW_RESTORE ); BringWindowToTop( hWnd ); SetForegroundWindow( hWnd ); } // Return failure return FALSE; } // Return success return TRUE;}CString CSingleInstance::GetClassName( void ) const{ return m_strClassName;}
在Create中我使用了FindWindow来找出已经存在的窗口句柄,当然这不是必要的。你可以根据自己的需要去掉这写代码。使用说明:BOOL CXXXApp::InitInstance(....){//m_ss 为成员变量 if(!m_ss.Create("my class name")) return FALSE;
........
}
{
bool bSuccess = false;
try
{
// First get the handle to the mutex
m_hMutex = CreateMutex(NULL, FALSE, szMutexName);
if(m_hMutex != NULL)
{
// Test the state of the mutex
// If the state is signaled, we successfully opened the mutex
if(WaitForSingleObject(m_hMutex, 0) == WAIT_OBJECT_0)
bSuccess = true;
}
}
catch(...) {}
}