/* This function is called by the Windows function DispatchMessage() */ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static UINT s_uTaskbarRestart; int i; switch (message) { case WM_CREATE: /* Save Window Handle */ o.hWnd = hwnd; s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated")); WTSRegisterSessionNotification(hwnd, NOTIFY_FOR_THIS_SESSION); /* Load application icon */ HICON hIcon = LoadLocalizedIcon(ID_ICO_APP); if (hIcon) { SendMessage(hwnd, WM_SETICON, (WPARAM) (ICON_SMALL), (LPARAM) (hIcon)); SendMessage(hwnd, WM_SETICON, (WPARAM) (ICON_BIG), (LPARAM) (hIcon)); } CreatePopupMenus(); /* Create popup menus */ ShowTrayIcon(); if (o.allow_service[0]=='1' || o.service_only[0]=='1') CheckServiceStatus(); // Check if service is running or not if (!AutoStartConnections()) { SendMessage(hwnd, WM_CLOSE, 0, 0); break; } break; case WM_NOTIFYICONTRAY: OnNotifyTray(lParam); // Manages message from tray break; case WM_COMMAND: if ( (LOWORD(wParam) >= IDM_CONNECTMENU) && (LOWORD(wParam) < IDM_CONNECTMENU + MAX_CONFIGS) ) { StartOpenVPN(&o.conn[LOWORD(wParam) - IDM_CONNECTMENU]); } if ( (LOWORD(wParam) >= IDM_DISCONNECTMENU) && (LOWORD(wParam) < IDM_DISCONNECTMENU + MAX_CONFIGS) ) { StopOpenVPN(&o.conn[LOWORD(wParam) - IDM_DISCONNECTMENU]); } if ( (LOWORD(wParam) >= IDM_STATUSMENU) && (LOWORD(wParam) < IDM_STATUSMENU + MAX_CONFIGS) ) { ShowWindow(o.conn[LOWORD(wParam) - IDM_STATUSMENU].hwndStatus, SW_SHOW); } if ( (LOWORD(wParam) >= IDM_VIEWLOGMENU) && (LOWORD(wParam) < IDM_VIEWLOGMENU + MAX_CONFIGS) ) { ViewLog(LOWORD(wParam) - IDM_VIEWLOGMENU); } if ( (LOWORD(wParam) >= IDM_EDITMENU) && (LOWORD(wParam) < IDM_EDITMENU + MAX_CONFIGS) ) { EditConfig(LOWORD(wParam) - IDM_EDITMENU); } #ifndef DISABLE_CHANGE_PASSWORD if ( (LOWORD(wParam) >= IDM_PASSPHRASEMENU) && (LOWORD(wParam) < IDM_PASSPHRASEMENU + MAX_CONFIGS) ) { ShowChangePassphraseDialog(&o.conn[LOWORD(wParam) - IDM_PASSPHRASEMENU]); } #endif if (LOWORD(wParam) == IDM_SETTINGS) { ShowSettingsDialog(); } if (LOWORD(wParam) == IDM_CLOSE) { CloseApplication(hwnd); } if (LOWORD(wParam) == IDM_SERVICE_START) { MyStartService(); } if (LOWORD(wParam) == IDM_SERVICE_STOP) { MyStopService(); } if (LOWORD(wParam) == IDM_SERVICE_RESTART) MyReStartService(); break; case WM_CLOSE: CloseApplication(hwnd); break; case WM_DESTROY: WTSUnRegisterSessionNotification(hwnd); StopAllOpenVPN(); OnDestroyTray(); /* Remove Tray Icon and destroy menus */ PostQuitMessage (0); /* Send a WM_QUIT to the message queue */ break; case WM_QUERYENDSESSION: return(TRUE); case WM_ENDSESSION: StopAllOpenVPN(); OnDestroyTray(); break; case WM_WTSSESSION_CHANGE: switch (wParam) { case WTS_SESSION_LOCK: o.session_locked = TRUE; break; case WTS_SESSION_UNLOCK: o.session_locked = FALSE; if (CountConnState(suspended) != 0) ResumeConnections(); break; } break; case WM_POWERBROADCAST: switch (wParam) { case PBT_APMSUSPEND: if (o.disconnect_on_suspend[0] == '1') { /* Suspend running connections */ for (i=0; i<o.num_configs; i++) { if (o.conn[i].state == connected) SuspendOpenVPN(i); } /* Wait for all connections to suspend */ for (i=0; i<10; i++, Sleep(500)) if (CountConnState(suspending) == 0) break; } return FALSE; case PBT_APMRESUMESUSPEND: case PBT_APMRESUMECRITICAL: if (CountConnState(suspended) != 0 && !o.session_locked) ResumeConnections(); return FALSE; } default: /* for messages that we don't deal with */ if (message == s_uTaskbarRestart) { /* Explorer has restarted, re-register the tray icon. */ ShowTrayIcon(); CheckAndSetTrayIcon(); break; } return DefWindowProc (hwnd, message, wParam, lParam); } return 0; }
LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { LPMINMAXINFO lpmmi; switch (message) /* handle the messages */ { case WM_WTSSESSION_CHANGE: switch ( wParam ) { case WTS_SESSION_LOCK: SaveEnergyShort(); break; case WTS_SESSION_UNLOCK: break; default: break; } break; case WM_CREATE: { LoadRegistrySettings(); TRAY_Init(hWnd,AUTO_START_FLAG); OnTray(wParam); } break; case WM_KEYDOWN : if (wParam == VK_ESCAPE) { ShowWindow(hWnd, SW_SHOWMINIMIZED); } break; case WM_SIZE: OnSizeTray(wParam); // Minimize/Restore to/from tray break; case WM_NOTIFYICONTRAY: OnNotifyTray(lParam); // Manages message from tray return TRUE; case WM_COMMAND: { if (OnCommandTrayMenu(wParam)) break; } break; case WM_RBUTTONDOWN: { TRAY_Menu_Show();//load POPUP menu in main window (why?) } break; case WM_CLOSE: ShowWindow(hWnd, SW_SHOWMINIMIZED); // Minimize/Restore to/from tray break; case WM_DESTROY: UnregisterSession(hWnd); OnDestroyTray(); PostQuitMessage (0); /* send a WM_QUIT to the message queue */ break; default: /* for messages that we don't deal with */ return DefWindowProc (hWnd, message, wParam, lParam); } return 0; }
/* This function is called by the Windows function DispatchMessage() */ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static UINT s_uTaskbarRestart; int i; switch (message) { case WM_CREATE: /* Save Window Handle */ o.hWnd = hwnd; s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated")); CreatePopupMenus(); /* Create popup menus */ LoadAppIcon(); /* Load App Icon */ ShowTrayIcon(); if (o.allow_service[0]=='1' || o.service_only[0]=='1') CheckServiceStatus(); // Check if service is running or not if (!AutoStartConnections()) { SendMessage(hwnd, WM_CLOSE, 0, 0); break; } break; case WM_NOTIFYICONTRAY: OnNotifyTray(lParam); // Manages message from tray return TRUE; case WM_COMMAND: if ( (LOWORD(wParam) >= IDM_CONNECTMENU) && (LOWORD(wParam) < IDM_CONNECTMENU + MAX_CONFIGS) ) { StartOpenVPN(LOWORD(wParam) - IDM_CONNECTMENU); } if ( (LOWORD(wParam) >= IDM_DISCONNECTMENU) && (LOWORD(wParam) < IDM_DISCONNECTMENU + MAX_CONFIGS) ) { StopOpenVPN(LOWORD(wParam) - IDM_DISCONNECTMENU); } if ( (LOWORD(wParam) >= IDM_STATUSMENU) && (LOWORD(wParam) < IDM_STATUSMENU + MAX_CONFIGS) ) { ShowWindow(o.cnn[LOWORD(wParam) - IDM_STATUSMENU].hwndStatus, SW_SHOW); } if ( (LOWORD(wParam) >= IDM_VIEWLOGMENU) && (LOWORD(wParam) < IDM_VIEWLOGMENU + MAX_CONFIGS) ) { ViewLog(LOWORD(wParam) - IDM_VIEWLOGMENU); } if ( (LOWORD(wParam) >= IDM_EDITMENU) && (LOWORD(wParam) < IDM_EDITMENU + MAX_CONFIGS) ) { EditConfig(LOWORD(wParam) - IDM_EDITMENU); } #ifndef DISABLE_CHANGE_PASSWORD if ( (LOWORD(wParam) >= IDM_PASSPHRASEMENU) && (LOWORD(wParam) < IDM_PASSPHRASEMENU + MAX_CONFIGS) ) { ShowChangePassphraseDialog(LOWORD(wParam) - IDM_PASSPHRASEMENU); } #endif if (LOWORD(wParam) == IDM_PROXY) { ShowProxySettingsDialog(); } if (LOWORD(wParam) == IDM_ABOUT) { DialogBox(o.hInstance, (LPCTSTR)IDD_ABOUTDIALOG, NULL, (DLGPROC)AboutDialogFunc); } if (LOWORD(wParam) == IDM_CLOSE) { CloseApplication(hwnd); } if (LOWORD(wParam) == IDM_SERVICE_START) { MyStartService(); } if (LOWORD(wParam) == IDM_SERVICE_STOP) { MyStopService(); } if (LOWORD(wParam) == IDM_SERVICE_RESTART) MyReStartService(); break; case WM_CLOSE: CloseApplication(hwnd); break; case WM_DESTROY: StopAllOpenVPN(); OnDestroyTray(); /* Remove Tray Icon and destroy menus */ PostQuitMessage (0); /* Send a WM_QUIT to the message queue */ break; case WM_QUERYENDSESSION: return(TRUE); case WM_ENDSESSION: StopAllOpenVPN(); OnDestroyTray(); break; case WM_POWERBROADCAST: switch (wParam) { case PBT_APMSUSPEND: if (o.disconnect_on_suspend[0] == '1') { /* Suspend running connections */ for (i=0; i<o.num_configs; i++) { if (o.cnn[i].connect_status == CONNECTED) SuspendOpenVPN(i); } /* Wait for all connections to suspend */ for (i=0; i<10; i++, Sleep(500)) if (CountConnectedState(SUSPENDING) == 0) break; } return FALSE; case PBT_APMRESUMESUSPEND: case PBT_APMRESUMECRITICAL: for (i=0; i<o.num_configs; i++) { /* Restart suspend connections */ if (o.cnn[i].connect_status == SUSPENDED) StartOpenVPN(i); /* If some connection never reached SUSPENDED state */ if (o.cnn[i].connect_status == SUSPENDING) StopOpenVPN(i); } return FALSE; } default: /* for messages that we don't deal with */ if (message == s_uTaskbarRestart) { /* Explorer has restarted, re-register the tray icon. */ ShowTrayIcon(); CheckAndSetTrayIcon(); break; } return DefWindowProc (hwnd, message, wParam, lParam); } return 0; }
//main function int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) { HWND hwnd,hCheckWnd; /* This is the handle for our window */ MSG messages; /* Here messages to the application are saved */ WNDCLASSEX wincl; /* Data structure for the windowclass */ hCheckWnd = GetRunningWindow(); if (hCheckWnd) // hCheckWnd != NULL -> Previous instance exists { MessageBox(hCheckWnd, "The program is already running", "Info", MB_OK); return FALSE; // Exit program } /* The Window structure */ wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */ wincl.style = CS_DBLCLKS; /* Catch double-clicks */ wincl.cbSize = sizeof (WNDCLASSEX); /* Use default icon and mouse-pointer */ wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC_ARROW); wincl.lpszMenuName = NULL; /* No menu */ wincl.cbClsExtra = 0; /* No extra bytes after the window class */ wincl.cbWndExtra = 0; /* structure or the window instance */ /* Use Windows's default color as the background of the window */ //wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; wincl.hbrBackground = GetSysColorBrush(COLOR_3DFACE); /* Register the window class, and if it fails quit the program */ if (!RegisterClassEx (&wincl)) return 0; // int desktopwidth=GetSystemMetrics(SM_CXSCREEN); //int desktopheight=GetSystemMetrics(SM_CYSCREEN); /* The class is registered, let's create the program*/ hwnd = CreateWindow ( szClassName, /* Classname */ szTitleText, /* Title Text */ WS_DISABLED , /* default window */ CW_USEDEFAULT, /* Windows decides the position */ CW_USEDEFAULT, /* where the window ends up on the screen */ MAX_X, /* The programs width */ MAX_Y, /* and height in pixels */ HWND_DESKTOP, /* The window is a child-window to desktop */ NULL, /* No menu */ hThisInstance, /* Program Instance handler */ NULL /* No Window Creation data */ ); RegisterSession(hwnd); RegisterHotKey(NULL,1,MOD_CONTROL,0x71); SetProcessWorkingSetSize(GetCurrentProcess(), (SIZE_T) -1, (SIZE_T) -1); bool gRet; /* Run the message loop. It will run until GetMessage() returns 0 */ while ((gRet = GetMessage (&messages, NULL, 0, 0)) != 0 ) { if (gRet == -1) { UnregisterSession(hwnd); OnDestroyTray(); PostQuitMessage (0); } else { /* Translate virtual-key messages into character messages */ TranslateMessage(&messages); /* Send message to WindowProcedure */ DispatchMessage(&messages); if (messages.message == WM_HOTKEY) { SaveEnergyShort(); } } } /* The program return-value is 0 - The value that PostQuitMessage() gave */ return messages.wParam; }