//============================================================================================ // 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; }
//---------------------------------------------------------------------- // This is a DialogProc, not a WindowProc // It should not call "return DefWindowProc()" at the end // It returns TRUE for handled messages and FALSE otherwise BOOL CALLBACK x_DialogProc(HWND hDlgWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { LRESULT lngRes; char * szResult; int iResult; switch (uMsg) { case WM_PI_RAWDATA_READY: g_OnNewData( wParam, lParam); return TRUE; case WM_PI_DATA_STARTED: CP_printf("\nLiberty data started.\n"); return TRUE; case WM_PI_DATA_STOPPED: CP_printf("\nLiberty data stopped.\n"); InvalidateRgn( hDlgWnd, NULL, TRUE); // refresh the window return TRUE; case WM_PI_RAWDATA_ERROR : g_GetLibertyResult( &szResult, &iResult ); CP_printf("\nLiberty data error: %s Code: %X\n", szResult, iResult); return TRUE; case WM_PI_RAWDATA_WARNING : CP_printf("\nLiberty data warning.\n"); return TRUE; case WM_COMMAND: if (HIWORD(wParam) == BN_CLICKED) { switch (LOWORD(wParam)) { case IDC_CMD_EXIT: // Exit button PostMessage( hDlgWnd, WM_CLOSE, 0,0); return TRUE; case IDC_CMD_START: // Start button g_CmdStart(); return TRUE; case IDC_CMD_STOP: // Stop button g_CmdStop(); return TRUE; case IDC_CHK_SHOWLOG: // Retrieve the state of the check box. lngRes = SendDlgItemMessage( hDlgWnd, IDC_CHK_SHOWLOG, BM_GETCHECK, 0, 0); if( lngRes == BST_CHECKED ) CP_printfShow(); if( lngRes == BST_UNCHECKED ) CP_printfHide(); return TRUE; } // switch } // if BN_CLICKED break; case WM_TIMER: OnTimer( hDlgWnd ); return TRUE; case WM_DESTROY: PostQuitMessage(0); return TRUE; case WM_CLOSE: g_Release_LibertyTrack(); CP_printfDestroy(); // close console window DestroyWindow (hDlgWnd); return TRUE; } return FALSE; }
//---------------------------------------------------------------------- BOOL CALLBACK x_DialogProc(HWND hDlgWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam) { UINT uiTimerID; int iRes; switch (uiMsg) { case WM_COMMAND: if (HIWORD(wParam) == BN_CLICKED) { switch (LOWORD(wParam)) { case IDC_CMD_EXIT: // Exit button PostMessage( hDlgWnd, WM_CLOSE, 0, 0); return TRUE; case IDC_CMD_START: // Start button g_CmdStart(); return TRUE; case IDC_CMD_STOP: // Stop button g_CmdStop(); return TRUE; // Hide/show log window case IDC_CHK_SHOWLOG: // Retrieve the state of the check box. iRes = SendDlgItemMessage( hDlgWnd, IDC_CHK_SHOWLOG, BM_GETCHECK, 0, 0); if( iRes == BST_CHECKED ) CP_printfShow(); if( iRes == BST_UNCHECKED ) CP_printfHide(); return TRUE; } // switch } // if BN_CLICKED break; case WM_TIMER: OnTimer( hDlgWnd ); return TRUE; case WM_DESTROY: PostQuitMessage(0); return TRUE; case WM_CLOSE: KillTimer( hDlgWnd, TIMER_ID); g_Release_OptoTrack(); DestroyWindow (hDlgWnd); return TRUE; case WM_INITDIALOG: g_hWnd = hDlgWnd; SetWindowText(hDlgWnd, x_szTitle); SendMessage(hDlgWnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(x_hInstance, MAKEINTRESOURCE(IDI_ICON_APPL))); SendMessage(hDlgWnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadImage(x_hInstance, MAKEINTRESOURCE(IDI_ICON_APPL), IMAGE_ICON, 16, 16, 0)); ShowWindow(hDlgWnd, SW_SHOW); 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 } iRes = g_Init_OptoTrack( ); if( iRes != 0 ) { MessageBox( NULL, "Error: g_Init_OptoTrack() failed.", x_szTitle, MB_OK); PostMessage( hDlgWnd, WM_CLOSE, 0, 0); return TRUE; } return TRUE; } // switch (uiMsg) return FALSE; // did not process a message }