| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- // CTI.cpp : 定义应用程序的类行为。
- //
- #include "stdafx.h"
- #include "CTI.h"
- #include "DisplayWnd.h"
- #include "CtiCore.h"
- #include "ExceptionHandler.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- volatile unsigned long ThreadCounter = 0;
- LONG WINAPI AppUnhandledExceptionFilter(struct _EXCEPTION_POINTERS* ExceptionInfo)
- {
- RecordExceptionInfo(ExceptionInfo, "CTI");
- return EXCEPTION_EXECUTE_HANDLER;
- }
- // CCTIApp
- BEGIN_MESSAGE_MAP(CCTIApp, CWinApp)
- ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
- END_MESSAGE_MAP()
- // CCTIApp 构造
- CCTIApp::CCTIApp()
- {
- // 支持重新启动管理器
- m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;
- // TODO: 在此处添加构造代码,
- // 将所有重要的初始化放置在 InitInstance 中
- SetUnhandledExceptionFilter(AppUnhandledExceptionFilter);
- }
- // 唯一的一个 CCTIApp 对象
- CCTIApp theApp;
- // CCTIApp 初始化
- BOOL CCTIApp::InitInstance()
- {
- #ifndef _DEBUG
- SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
- #endif
- // 禁止多个实例的同时运行
- HANDLE hMutexOneInstance = ::CreateMutex(NULL, TRUE, _T(AfxGetAppName()));
- if (::GetLastError() == ERROR_ALREADY_EXISTS)
- {
- if (hMutexOneInstance) // Release the mutex
- {
- ::ReleaseMutex(hMutexOneInstance);
- }
- HWND hWndPrevious = ::GetWindow(::GetDesktopWindow(), GW_CHILD);
- while (::IsWindow(hWndPrevious))
- {
- // 检查窗口是否有预设的标记? 有,则是我们寻找的主窗口
- if (::GetProp(hWndPrevious, m_pszAppName))
- {
- // 主窗口已最小化,则恢复其大小
- if (::IsIconic(hWndPrevious)) ::ShowWindow(hWndPrevious, SW_RESTORE);
- ::SetForegroundWindow(hWndPrevious); // 将主窗激活
- ::SetForegroundWindow(::GetLastActivePopup(hWndPrevious)); // 将主窗的对话框激活
- return FALSE;
- } // end if
- hWndPrevious = ::GetWindow(hWndPrevious, GW_HWNDNEXT); // 继续寻找下一个窗口
- } // end while
- } // end if
- // 如果一个运行在 Windows XP 上的应用程序清单指定要
- // 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
- //则需要 InitCommonControlsEx()。否则,将无法创建窗口。
- INITCOMMONCONTROLSEX InitCtrls;
- InitCtrls.dwSize = sizeof(InitCtrls);
- // 将它设置为包括所有要在应用程序中使用的
- // 公共控件类。
- InitCtrls.dwICC = ICC_WIN95_CLASSES;
- InitCommonControlsEx(&InitCtrls);
- CWinApp::InitInstance();
- if (!AfxSocketInit())
- {
- AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
- return FALSE;
- }
- AfxEnableControlContainer();
- // 标准初始化
- // 如果未使用这些功能并希望减小
- // 最终可执行文件的大小,则应移除下列
- // 不需要的特定初始化例程
- // 更改用于存储设置的注册表项
- // TODO: 应适当修改该字符串,
- // 例如修改为公司或组织名
- SetRegistryKey(_T("CTI"));
- //if(!CCtiCore::GetInstance().stage1Start())
- // return FALSE;
- CDisplayWnd* pDisWnd = (CDisplayWnd *)RUNTIME_CLASS(CDisplayWnd)->CreateObject();
- m_pMainWnd = pDisWnd;
- pDisWnd->Show();
- //CCtiCore::GetInstance().stage2Start();
- if (ThreadCounter == 0)
- {
- AfxBeginThread(StageStartThread, this);
- }
- return TRUE;
- }
- UINT CCTIApp::StageStartThread(LPVOID lp)
- {
- InterlockedIncrement(&ThreadCounter);
- CCTIApp* pCTIApp = (CCTIApp*)lp;
- bool Stage1Ok = false;
- bool Stage2Ok = false;
- while (TRUE)
- {
- CCtiCore::GetInstance().UnInit(); // 先取消消息注册
- CCtiCore::GetInstance().StopForContinue(); // 再释放资源
- if (false == (Stage1Ok = CCtiCore::GetInstance().stage1Start()) || false == (Stage2Ok = CCtiCore::GetInstance().stage2Start()))
- {
- Sleep(15 * 1000);
- }
- else
- {
- break; //连接成功退出
- }
- }
- InterlockedDecrement(&ThreadCounter);
- return 0;
- }
|