//============================================================================================ // MAIN //============================================================================================ extern "C" int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow ){ // show state TCHAR szTokens[] = "-/"; int iRes; HWND hDlgWnd; UINT uiTimerID; HRESULT hr; // Parse the command line for options LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens); while (lpszToken != NULL) { if (lstrcmpi(lpszToken, "UnregServer")==0) { iRes = g_UnregisterCOM(); if( iRes != 0 ) { MessageBox( NULL, "Error: Can't unregister server. g_UnregisterCOM() failed.", x_szTitle, MB_OK); } else { MessageBox( NULL, "Success: the server is unregistered.", x_szTitle, MB_OK); } return iRes; } if (lstrcmpi(lpszToken, "RegServer")==0) { iRes = g_RegisterCOM(); if( iRes != 0 ) { MessageBox( NULL, "Error: Can't register server. g_RegisterCOM() failed.", x_szTitle, MB_OK); } else { MessageBox( NULL, "Success: the server is registered.", x_szTitle, MB_OK); } return iRes; } lpszToken = FindOneOf(lpszToken, szTokens); } // while there are tokens //------- Windows staff starts here CP_printfCreate(x_szTitle); // create console window hDlgWnd=CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MAIN_DIALOG), NULL, x_DialogProc); SetWindowText(hDlgWnd, x_szTitle); SendMessage(hDlgWnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_APPL))); SendMessage(hDlgWnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_APPL), IMAGE_ICON, 16, 16, 0)); // SetWindowText(GetDlgItem(hDlgWnd, IDC_LBL_VERSION ) , g_szVersion); ShowWindow(hDlgWnd, SW_SHOW); CP_printf(g_szVersion); CP_printf("\n\n"); uiTimerID = SetTimer( hDlgWnd, TIMER_ID, TIMER_PERIOD_MS, NULL ); if ( uiTimerID == 0){ CP_printf("Error: could not create timer!"); // don't exit we are still functional } // Init COM using multi-threaded model hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); //COINIT_APARTMENTTHREADED if( hr != S_OK) { MessageBox( NULL, "Error in CoInitializeEx()", x_szTitle, MB_OK); return 1; } iRes = g_Init_EyeTrack( ); if( iRes != 0 ) { MessageBox( NULL, "Error: g_Init_EyeTrack() failed.", x_szTitle, MB_OK); return 1; } // Message loop MSG msg; while (GetMessage(&msg, 0, 0, 0)) { // Process dialog messages in IsDialogMessage() pass others to translate/dispatch if(!IsDialogMessage(hDlgWnd,&msg)){ TranslateMessage(&msg); DispatchMessage(&msg); } } // end of message loop KillTimer( hDlgWnd, TIMER_ID); CoUninitialize(); return 0; }
//=========================================================================== extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd ) { HANDLE hMutex; int iRet; HRESULT hRes; TCHAR szTokens[] = _T("-/"); // Check for another instance hMutex = OpenMutex( SYNCHRONIZE, // requested access (lowest possible) FALSE, // allow inheritance (does not matter) x_szMutexName); // unique name if(hMutex){ MessageBox( NULL, "Cannot start the application because another instance is already running.\n", g_szAppTitile, MB_OK | MB_ICONERROR ); return 1; } hMutex = CreateMutex( NULL, // default security TRUE, // obtain ownership x_szMutexName); // unique name if(!hMutex){ MessageBox( NULL, "Cannot create application mutex.\n", g_szAppTitile, MB_OK | MB_ICONERROR ); return 1; } hRes = CoInitialize(NULL); // single-threaded apartment _ASSERTE(SUCCEEDED(hRes)); _Module.Init(ObjectMap, hInstance, &LIBID_CP_DISPLAYLib); hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE); _ASSERTE(SUCCEEDED(hRes)); lpCmdLine = GetCommandLine(); //this line is necessary for _ATL_MIN_CRT // Parse the command line for options LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens); while (lpszToken != NULL) { if (lstrcmpi(lpszToken, "UnregServer")==0) { _Module.UpdateRegistryFromResource(IDR_CP_Display, FALSE); iRet = _Module.UnregisterServer(TRUE); // iRet = g_UnregisterCOM(); if( iRet != 0 ) { MessageBox( NULL, "Error: Can't unregister server. UnregisterServer() failed.", g_szAppTitile, MB_OK); } else { MessageBox( NULL, "Success: the server is unregistered.", g_szAppTitile, MB_OK); } _Module.Term(); CoUninitialize(); return iRet; } if (lstrcmpi(lpszToken, "RegServer")==0) { iRet = _Module.UpdateRegistryFromResource(IDR_CP_Display, TRUE); iRet = _Module.RegisterServer(TRUE); //iRet = g_RegisterCOM(); if( iRet != 0 ) { char str[200]; sprintf(str,"Error: Can't register server. g_RegisterCOM() failed (%lx).",(unsigned long)iRet); MessageBox( NULL, str, g_szAppTitile, MB_OK); //MessageBox( NULL, "Error: Can't register server. g_RegisterCOM() failed.", g_szAppTitile, MB_OK); } else { MessageBox( NULL, "Success: the server is registered.", g_szAppTitile, MB_OK); } _Module.Term(); CoUninitialize(); return iRet; } lpszToken = FindOneOf(lpszToken, szTokens); } // while there are tokens CP_printfCreate( g_szAppTitile ); // Creates log window (console) CP_printf("%s\n", g_szVersion ); CP_printf("----------------------------------------\n"); CP_printf("Build options:\n\n"); //~~~~~~~~~~~~~~~~~ #ifdef _DEBUG CP_printf("Debug version.\n"); #else CP_printf("Release version. \n\n"); #endif // #ifdef _DEBUG //~~~~~~~~~~~~~~~~~ CP_printf("SET_FULLSCREEN_MODE: %d\n", SET_FULLSCREEN_MODE); CP_printf("DEBUG_THRESHOLD: %d\n", DEBUG_THRESHOLD); CP_printf("----------------------------------------\n"); srand( (unsigned)123 ); try { if ( !g_StartGL_Engine( hInstance, nShowCmd ) ){ g_MainLoop(); } } catch( ... ) { MessageBox( NULL, "Exception!!! SOS!!! The program will shut down!", g_szAppTitile, MB_OK | MB_ICONERROR ); exit(1); } CP_printf("N objects before quitting: %d\n", _Module.GetLockCount() ); _Module.RevokeClassObjects(); Sleep(COM_THREAD_WAIT_MS); //wait for any threads to finish // WrapUp _Module.Term(); CoUninitialize(); // Left-over objects are deleted in CoUninitialize(). Since they call RequestUpdate // in the destuctor, we have to delete GL_Window after CoUninitialize() g_StopGL_Engine( ); InvalidateRect(NULL, NULL, TRUE); // redraw the desktop Sleep(5000); // show log window for 5 sec before quitting CP_printfDestroy( ); // Kills log window (console) ReleaseMutex(hMutex); return 0; }
//============================================================================================ // MAIN //============================================================================================ extern "C" int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow ){ // show state HANDLE hMutex; TCHAR szTokens[] = "-/"; LPCTSTR lpszToken; int iRes; HWND hDlgWnd; UINT uiTimerID; HRESULT hr; // Check for another instance hMutex = OpenMutex( SYNCHRONIZE, // requested access (lowest possible) FALSE, // allow inheritance (does not matter) x_szMutexName); // unique name if(hMutex){ MessageBox( NULL, "Cannot start the application because another instance is already running.\n", x_szTitle, MB_OK | MB_ICONERROR ); return 1; } hMutex = CreateMutex( NULL, // default security TRUE, // obtain ownership x_szMutexName); // unique name if(!hMutex){ MessageBox( NULL, "Cannot create application mutex.\n", x_szTitle, MB_OK | MB_ICONERROR ); return 1; } // Parse the command line for options lpszToken = FindOneOf(lpCmdLine, szTokens); while (lpszToken != NULL) { if (lstrcmpi(lpszToken, "UnregServer")==0) { iRes = g_UnregisterCOM(); if( iRes != 0 ) { MessageBox( NULL, "Error: Can't unregister server. g_UnregisterCOM() failed.", x_szTitle, MB_OK); } else { MessageBox( NULL, "Success: the server is unregistered.", x_szTitle, MB_OK); } return iRes; } if (lstrcmpi(lpszToken, "RegServer")==0) { iRes = g_RegisterCOM(); if( iRes != 0 ) { MessageBox( NULL, "Error: Can't register server. g_RegisterCOM() failed.", x_szTitle, MB_OK); } else { MessageBox( NULL, "Success: the server is registered.", x_szTitle, MB_OK); } return iRes; } lpszToken = FindOneOf(lpszToken, szTokens); } // while there are tokens //------- Windows staff starts here CP_printfCreate(x_szTitle); // create console window hDlgWnd=CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MAIN_DIALOG), NULL, x_DialogProc); g_hWnd = hDlgWnd; SetWindowText(hDlgWnd, x_szTitle); SendMessage(hDlgWnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_APPL))); SendMessage(hDlgWnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_APPL), IMAGE_ICON, 16, 16, 0)); ShowWindow(hDlgWnd, SW_SHOW); CP_printf(g_szVersion); CP_printf("\n\n"); uiTimerID = SetTimer( hDlgWnd, TIMER_ID, TIMER_PERIOD_MS, NULL ); if ( uiTimerID == 0){ CP_printf("Error: could not create timer!"); // don't exit we are still functional } CP_printfHide(); // default is no log window // Init COM using single-thread apartment model hr = CoInitialize(NULL); if( hr != S_OK) { MessageBox( NULL, "Error in CoInitializeEx()", x_szTitle, MB_OK); return 1; } iRes = g_Init_LibertyTrack( ); if( iRes != 0 ) { MessageBox( NULL, "Error: g_Init_LibertyTrack() failed.", x_szTitle, MB_OK); return 1; } // Message loop MSG msg; while (GetMessage(&msg, 0, 0, 0)) { // IsDialogMessage() processes keyboard events for the dialog and // passes others to translate/dispatch if(!IsDialogMessage(hDlgWnd,&msg)){ TranslateMessage(&msg); DispatchMessage(&msg); } } // end of uMsg loop KillTimer( hDlgWnd, TIMER_ID); CoUninitialize(); ReleaseMutex(hMutex); return 0; }
//============================================================================================ // MAIN //============================================================================================ extern "C" int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow ){ // show state HANDLE hMutex; TCHAR szTokens[] = "-/"; LPCTSTR lpszToken; int iRes; HRESULT hRes; x_hInstance = hInstance; // Check for another instance hMutex = OpenMutex( SYNCHRONIZE, // requested access (lowest possible) FALSE, // allow inheritance (does not matter) x_szMutexName); // unique name if(hMutex){ MessageBox( NULL, "Cannot start the application because another instance is already running.\n", x_szTitle, MB_OK | MB_ICONERROR ); return 1; } hMutex = CreateMutex( NULL, // default security TRUE, // obtain ownership x_szMutexName); // unique name if(!hMutex){ MessageBox( NULL, "Cannot create application mutex.\n", x_szTitle, MB_OK | MB_ICONERROR ); return 1; } // Parse the command line for options lpszToken = FindOneOf(lpCmdLine, szTokens); while (lpszToken != NULL) { if (lstrcmpi(lpszToken, "UnregServer")==0) { iRes = g_UnregisterCOM(); if( iRes != 0 ) { MessageBox( NULL, "Error: Can't unregister server. g_UnregisterCOM() failed.", x_szTitle, MB_OK); } else { MessageBox( NULL, "Success: the server is unregistered.", x_szTitle, MB_OK); } return iRes; } if (lstrcmpi(lpszToken, "RegServer")==0) { iRes = g_RegisterCOM(); if( iRes != 0 ) { MessageBox( NULL, "Error: Can't register server. g_RegisterCOM() failed.", x_szTitle, MB_OK); } else { MessageBox( NULL, "Success: the server is registered.", x_szTitle, MB_OK); } return iRes; } lpszToken = FindOneOf(lpszToken, szTokens); } // while there are tokens //------- Regular run starts here // Init COM using single-threaded model hRes = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); if( hRes != S_OK) { MessageBox( NULL, "Error in CoInitializeEx()", x_szTitle, MB_OK); return 1; } CP_printfCreate(x_szTitle); // create console window CP_printf(g_szVersion); CP_printf("\n\n"); CP_printfHide(); // hide console window //------- Windows main loop is here DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN_DIALOG), NULL, x_DialogProc); CP_printfDestroy(); CoUninitialize(); return 0; }