int CheckServiceStatus() { SC_HANDLE schSCManager; SC_HANDLE schService; SERVICE_STATUS ssStatus; // Open a handle to the SC Manager database. schSCManager = OpenSCManager( NULL, // local machine NULL, // ServicesActive database SC_MANAGER_CONNECT); // Connect rights if (NULL == schSCManager) { o.service_state = service_noaccess; SetServiceMenuStatus(); return(false); } schService = OpenService( schSCManager, // SCM database _T("OpenVPNService"), // service name SERVICE_QUERY_STATUS); if (schService == NULL) { /* open vpn service failed */ ShowLocalizedMsg(IDS_ERR_OPEN_VPN_SERVICE); o.service_state = service_noaccess; SetServiceMenuStatus(); return(false); } if (!QueryServiceStatus( schService, // handle to service &ssStatus) ) // address of status information structure { /* query failed */ ShowLocalizedMsg(IDS_ERR_QUERY_SERVICE); return(false); } if (ssStatus.dwCurrentState == SERVICE_RUNNING) { o.service_state = service_connected; SetServiceMenuStatus(); SetTrayIcon(connected); return(true); } else { o.service_state = service_disconnected; SetServiceMenuStatus(); SetTrayIcon(disconnected); return(false); } }
int MyStopService() { SC_HANDLE schSCManager; SC_HANDLE schService; SERVICE_STATUS ssStatus; int i; // Open a handle to the SC Manager database. schSCManager = OpenSCManager( NULL, // local machine NULL, // ServicesActive database SC_MANAGER_CONNECT); // Connect rights if (NULL == schSCManager) { /* can't open SCManager */ ShowLocalizedMsg(IDS_ERR_OPEN_SCMGR, (int) GetLastError()); return(false); } schService = OpenService( schSCManager, // SCM database _T("OpenVPNService"), // service name SERVICE_STOP); if (schService == NULL) { /* can't open vpn service */ ShowLocalizedMsg(IDS_ERR_OPEN_VPN_SERVICE); return(false); } /* Run DisConnect script */ for (i=0; i<o.num_configs; i++) RunDisconnectScript(&o.conn[i], true); if (!ControlService( schService, // handle to service SERVICE_CONTROL_STOP, // control value to send &ssStatus) ) // address of status info { /* stop failed */ ShowLocalizedMsg(IDS_ERR_STOP_SERVICE); return(false); } o.service_state = service_disconnected; SetServiceMenuStatus(); CheckAndSetTrayIcon(); return(true); }
int MyStartService() { SC_HANDLE schSCManager; SC_HANDLE schService; SERVICE_STATUS ssStatus; DWORD dwOldCheckPoint; DWORD dwStartTickCount; DWORD dwWaitTime; int i; /* Set Service Status = Connecting */ o.service_state = service_connecting; SetServiceMenuStatus(); CheckAndSetTrayIcon(); // Open a handle to the SC Manager database. schSCManager = OpenSCManager( NULL, // local machine NULL, // ServicesActive database SC_MANAGER_CONNECT); // Connect rights if (NULL == schSCManager) { /* open SC manager failed */ ShowLocalizedMsg(IDS_ERR_OPEN_SCMGR); goto failed; } schService = OpenService( schSCManager, // SCM database _T("OpenVPNService"), // service name SERVICE_START | SERVICE_QUERY_STATUS); if (schService == NULL) { /* can't open VPN service */ ShowLocalizedMsg(IDS_ERR_OPEN_VPN_SERVICE); goto failed; } /* Run Pre-connect script */ for (i=0; i<o.num_configs; i++) RunPreconnectScript(&o.conn[i]); if (!StartService( schService, // handle to service 0, // number of arguments NULL) ) // no arguments { /* can't start */ ShowLocalizedMsg(IDS_ERR_START_SERVICE); goto failed; } else { //printf("Service start pending.\n"); } // Check the status until the service is no longer start pending. if (!QueryServiceStatus( schService, // handle to service &ssStatus) ) // address of status information structure { /* query failed */ ShowLocalizedMsg(IDS_ERR_QUERY_SERVICE); goto failed; } // Save the tick count and initial checkpoint. dwStartTickCount = GetTickCount(); dwOldCheckPoint = ssStatus.dwCheckPoint; while (ssStatus.dwCurrentState == SERVICE_START_PENDING) { // Do not wait longer than the wait hint. A good interval is // one tenth the wait hint, but no less than 1 second and no // more than 5 seconds. dwWaitTime = ssStatus.dwWaitHint / 10; if( dwWaitTime < 1000 ) dwWaitTime = 1000; else if ( dwWaitTime > 5000 ) dwWaitTime = 5000; Sleep( dwWaitTime ); // Check the status again. if (!QueryServiceStatus( schService, // handle to service &ssStatus) ) // address of structure break; if ( ssStatus.dwCheckPoint > dwOldCheckPoint ) { // The service is making progress. dwStartTickCount = GetTickCount(); dwOldCheckPoint = ssStatus.dwCheckPoint; } else { if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint) { // No progress made within the wait hint break; } } } CloseServiceHandle(schService); CloseServiceHandle(schSCManager); if (ssStatus.dwCurrentState != SERVICE_RUNNING) { /* service hasn't started */ ShowLocalizedMsg(IDS_ERR_SERVICE_START_FAILED); goto failed; } /* Run Connect script */ for (i=0; i<o.num_configs; i++) RunConnectScript(&o.conn[i], true); /* Set Service Status = Connected */ o.service_state = service_connected; SetServiceMenuStatus(); CheckAndSetTrayIcon(); /* Show "OpenVPN Service Started" Tray Balloon msg */ ShowTrayBalloon(LoadLocalizedString(IDS_NFO_SERVICE_STARTED), _T("")); return(true); failed: /* Set Service Status = Disconnecting */ o.service_state = service_disconnected; SetServiceMenuStatus(); CheckAndSetTrayIcon(); return(false); }
/* Create popup menus */ void CreatePopupMenus() { int i; for (i = 0; i < o.num_configs; i++) hMenuConn[i] = CreatePopupMenu(); hMenuService = CreatePopupMenu(); hMenu = CreatePopupMenu(); if (o.num_configs == 1) { /* Create Main menu with actions */ if (o.service_only == 0) { AppendMenu(hMenu, MF_STRING, IDM_CONNECTMENU, LoadLocalizedString(IDS_MENU_CONNECT)); AppendMenu(hMenu, MF_STRING, IDM_DISCONNECTMENU, LoadLocalizedString(IDS_MENU_DISCONNECT)); AppendMenu(hMenu, MF_STRING, IDM_STATUSMENU, LoadLocalizedString(IDS_MENU_STATUS)); AppendMenu(hMenu, MF_SEPARATOR, 0, 0); } else { AppendMenu(hMenu, MF_STRING, IDM_SERVICE_START, LoadLocalizedString(IDS_MENU_SERVICEONLY_START)); AppendMenu(hMenu, MF_STRING, IDM_SERVICE_STOP, LoadLocalizedString(IDS_MENU_SERVICEONLY_STOP)); AppendMenu(hMenu, MF_STRING, IDM_SERVICE_RESTART, LoadLocalizedString(IDS_MENU_SERVICEONLY_RESTART)); AppendMenu(hMenu, MF_SEPARATOR, 0, 0); } AppendMenu(hMenu, MF_STRING, IDM_VIEWLOGMENU, LoadLocalizedString(IDS_MENU_VIEWLOG)); AppendMenu(hMenu, MF_STRING, IDM_EDITMENU, LoadLocalizedString(IDS_MENU_EDITCONFIG)); AppendMenu(hMenu, MF_STRING, IDM_CLEARPASSMENU, LoadLocalizedString(IDS_MENU_CLEARPASS)); #ifndef DISABLE_CHANGE_PASSWORD if (o.conn[0].flags & ALLOW_CHANGE_PASSPHRASE) AppendMenu(hMenu, MF_STRING, IDM_PASSPHRASEMENU, LoadLocalizedString(IDS_MENU_PASSPHRASE)); #endif AppendMenu(hMenu, MF_SEPARATOR, 0, 0); AppendMenu(hMenu, MF_STRING, IDM_IMPORT, LoadLocalizedString(IDS_MENU_IMPORT)); AppendMenu(hMenu, MF_STRING ,IDM_SETTINGS, LoadLocalizedString(IDS_MENU_SETTINGS)); AppendMenu(hMenu, MF_STRING ,IDM_CLOSE, LoadLocalizedString(IDS_MENU_CLOSE)); SetMenuStatus(&o.conn[0], o.conn[0].state); } else { /* Create Main menu with all connections */ int i; for (i = 0; i < o.num_configs; i++) AppendMenu(hMenu, MF_POPUP, (UINT_PTR) hMenuConn[i], o.conn[i].config_name); if (o.num_configs > 0) AppendMenu(hMenu, MF_SEPARATOR, 0, 0); if (o.service_only) { AppendMenu(hMenu, MF_STRING, IDM_SERVICE_START, LoadLocalizedString(IDS_MENU_SERVICEONLY_START)); AppendMenu(hMenu, MF_STRING, IDM_SERVICE_STOP, LoadLocalizedString(IDS_MENU_SERVICEONLY_STOP)); AppendMenu(hMenu, MF_STRING, IDM_SERVICE_RESTART, LoadLocalizedString(IDS_MENU_SERVICEONLY_RESTART)); AppendMenu(hMenu, MF_SEPARATOR, 0, 0); } AppendMenu(hMenu, MF_STRING, IDM_IMPORT, LoadLocalizedString(IDS_MENU_IMPORT)); AppendMenu(hMenu, MF_STRING, IDM_SETTINGS, LoadLocalizedString(IDS_MENU_SETTINGS)); AppendMenu(hMenu, MF_STRING, IDM_CLOSE, LoadLocalizedString(IDS_MENU_CLOSE)); /* Create popup menus for every connection */ for (i=0; i < o.num_configs; i++) { if (o.service_only == 0) { AppendMenu(hMenuConn[i], MF_STRING, IDM_CONNECTMENU + i, LoadLocalizedString(IDS_MENU_CONNECT)); AppendMenu(hMenuConn[i], MF_STRING, IDM_DISCONNECTMENU + i, LoadLocalizedString(IDS_MENU_DISCONNECT)); AppendMenu(hMenuConn[i], MF_STRING, IDM_STATUSMENU + i, LoadLocalizedString(IDS_MENU_STATUS)); AppendMenu(hMenuConn[i], MF_SEPARATOR, 0, 0); } AppendMenu(hMenuConn[i], MF_STRING, IDM_VIEWLOGMENU + i, LoadLocalizedString(IDS_MENU_VIEWLOG)); AppendMenu(hMenuConn[i], MF_STRING, IDM_EDITMENU + i, LoadLocalizedString(IDS_MENU_EDITCONFIG)); AppendMenu(hMenuConn[i], MF_STRING, IDM_CLEARPASSMENU + i, LoadLocalizedString(IDS_MENU_CLEARPASS)); #ifndef DISABLE_CHANGE_PASSWORD if (o.conn[i].flags & ALLOW_CHANGE_PASSPHRASE) AppendMenu(hMenuConn[i], MF_STRING, IDM_PASSPHRASEMENU + i, LoadLocalizedString(IDS_MENU_PASSPHRASE)); #endif SetMenuStatus(&o.conn[i], o.conn[i].state); } } SetServiceMenuStatus(); }