bool QPlc::OpenConnection(void) { bool result=true; char ip[16]; Plc_Type vPlcType; vPlcType=(Plc_Type) m_PlcType; strcpy(ip,m_ip.toStdString().c_str()); m_Session=OpenSession(ip); if (m_Session!=NULL) { if (RegisterSession(m_Session)<0) { CloseSession(m_Session); return false; } } else { return false; } m_Connection=ConnectPLCOverCNET(m_Session,vPlcType,(long)m_Session,GetSerial(),m_time_out,m_PlcPath,m_path_size);//(int)m_Session if (m_Connection==NULL) { return false; } qDebug("Connection OK"); return result; }
int Connect(PLC *Plc,char *TagName, char *responseValue) { int result=SUCCESS; int path_size=0; char ip[16]; BYTE path[40]; Eip_Session *Session=NULL; Eip_Connection *Connection=NULL; int dataType; Log(LOG_DEBUG,"[Connect] Building Session for %s\n",Plc->PlcName); path_size=ParsePath(Plc->PlcPath,ip,path); if (path_size>0) { Session=OpenSession(ip); if (Session!=NULL) { if (RegisterSession(Session)<0) { CloseSession(Session); Log(LOG_CRIT,"[Connect] Unable to register session for Plc: %s (%s) \n",Plc->PlcName,cip_err_msg); return ERROR; } } else { Log(LOG_CRIT,"[Connect] Unable to open session for Plc: %s (%s)\n",Plc->PlcName,cip_err_msg); return ERROR; } if (Plc->NetWork) Connection=ConnectPLCOverDHP(Session,Plc->PlcType,(int)Session,GetSerial(),MAX_SAMPLE,Plc->NetWork,path,path_size); else Connection=ConnectPLCOverCNET(Session,Plc->PlcType,(int)Session,GetSerial(),MAX_SAMPLE,path,path_size); if (Connection!=NULL) { Log(LOG_DEBUG,"[Connect] Connection (%p) created for PLC : %s (%s) )\n",Connection,Plc->PlcName,cip_err_msg); } else { Log(LOG_CRIT,"[Connect] Unable to create connection for Plc: %s (%s)\n",Plc->PlcName,cip_err_msg); return ERROR; } } else Log(LOG_ERR,"[Connect] Invalid path : %s\n",Plc->PlcPath); Log(LOG_DEBUG,"[Connect] Connect : %s [%s](%p / %p)\n",TagName,writeValue,Session,Connection); if (ReadTag(Plc, Session, Connection, TagName, &dataType, responseValue)!=SUCCESS) return ERROR; else { if (isWrite) { if (WriteTag(Plc, Session, Connection, TagName,dataType)!=SUCCESS) return ERROR; Log(LOG_DEBUG,"[Connect] %s [%s] %x (%p / %p)\n",TagName,writeValue,dataType,Session,Connection); if (ReadTag(Plc, Session, Connection, TagName, &dataType, responseValue)!=SUCCESS) return ERROR; } } return result; }
//================================================================================================ //-------------------------//-----------------------------+++--> Save Current Settings to Registry: void OnApply(HWND hDlg) //----------------------------------------------------------------+++--> { wchar_t str[2]; char bRefresh = ((unsigned)api.GetInt(L"Desktop",L"Multimon",1) != IsDlgButtonChecked(hDlg,IDCB_MULTIMON)); api.SetInt(L"Calendar", L"bCustom", IsDlgButtonChecked(hDlg,IDCB_USECALENDAR)); api.SetInt(L"Calendar", L"CloseCalendar", IsDlgButtonChecked(hDlg,IDCB_CLOSECAL)); api.SetInt(L"Calendar", L"ShowWeekNums", IsDlgButtonChecked(hDlg,IDCB_SHOWWEEKNUMS)); api.SetInt(L"Calendar", L"ShowDayOfYear", IsDlgButtonChecked(hDlg,IDCB_SHOW_DOY)); api.SetInt(L"Calendar", L"CalendarTopMost", IsDlgButtonChecked(hDlg,IDCB_CALTOPMOST)); api.SetInt(L"Calendar", L"ViewMonths", (int)SendDlgItemMessage(hDlg,IDC_CALMONTHSPIN,UDM_GETPOS32,0,0)); api.SetInt(L"Calendar", L"ViewMonthsPast", (int)SendDlgItemMessage(hDlg,IDC_CALMONTHPASTSPIN,UDM_GETPOS32,0,0)); # ifdef WIN2K_COMPAT if(api.OS == TOS_2000) { int value = IsDlgButtonChecked(hDlg,IDCB_TRANS2KICONS); SetDesktopIconTextBk(value); api.SetInt(L"Desktop", L"Transparent2kIconText", value); if(value) TimetableAdd(SCHEDID_WIN2K, 30, 30); else TimetableRemove(SCHEDID_WIN2K); } # endif // WIN2K_COMPAT api.SetInt(L"Desktop", L"MonOffOnLock", IsDlgButtonChecked(hDlg, IDCB_MONOFF_ONLOCK)); api.SetInt(L"Desktop", L"Multimon", IsDlgButtonChecked(hDlg,IDCB_MULTIMON)); str[1] = '\0'; str[0] = '0' + (char)ComboBox_GetCurSel(GetDlgItem(hDlg,IDC_FIRSTWEEK)); SetInternational(L"iFirstWeekOfYear", str); str[0] = '0' + (char)ComboBox_GetCurSel(GetDlgItem(hDlg,IDC_FIRSTDAY)); SetInternational(L"iFirstDayOfWeek", str); if(api.OS >= TOS_XP) { // This feature requires XP+ BOOL enabled=IsDlgButtonChecked(hDlg, IDCB_MONOFF_ONLOCK); if(enabled){ RegisterSession(g_hwndTClockMain); } else { UnregisterSession(g_hwndTClockMain); } } if(api.OS >= TOS_WIN10){ int old_calendar = api.GetSystemInt(HKEY_LOCAL_MACHINE, kSectionImmersiveShell, kKeyWin32Tray, 0); if((int)IsDlgButtonChecked(hDlg, IDC_OLDCALENDAR) != old_calendar){ wchar_t param[5] = L"/Wc0"; wchar_t exe[MAX_PATH]; if(!old_calendar) param[3] = '1'; if(api.ExecElevated(GetClockExe(exe), param, NULL) == 1) CheckDlgButton(hDlg, IDC_OLDCALENDAR, old_calendar); } } if(bRefresh){ SendMessage(g_hwndClock,CLOCKM_REFRESHCLOCK,0,0); } }
int BuildSession(PLC *plc) { int path_size=0; char ip[16],path[40]; Eip_Session *session=NULL; Log(LOG_DEBUG,"Building Session for %s (%d sessions)\n",plc->PlcName,SESSIONs.Count); if (plc->Session!=NULL) return(0); path_size=ParsePath(plc->PlcPath,ip,path); if (path_size>0) { // Creating Sessions if (plc->Session==NULL) { session=FindSession(ip,&PLCs); if (session==NULL) // not found { session=OpenSession(ip); if (session!=NULL) { if (RegisterSession(session)<0) { CloseSession(session); Log(LOG_CRIT,"Unable to register session for Plc: %s (%s) \n",plc->PlcName,cip_err_msg); } AddChListe(&SESSIONs,session); session->References=1; //session->timeout=2000; Log(LOG_DEBUG,"Session (%p) created for PLC : %s (%d sessions)\n",session,plc->PlcName,SESSIONs.Count); plc->Session=session; return(1); } else { Log(LOG_CRIT,"Unable to open session for Plc: %s (%s)\n",plc->PlcName,cip_err_msg); return(0); } } else { plc->Session=session; session->References++; Log(LOG_DEBUG,"%s Sharing Session (%p) with another PLC (%d references)\n",plc->PlcName,session,session->References); return(1); } } else return(0); } else return(0); }
//================================================================================================ //--------------------------------------------------==-+++--> Entry Point of Program Using WinMain: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS wndclass; HWND hwndMain; MSG msg; int updated; (void)hPrevInstance; (void)nCmdShow; #if defined(__GNUC__) && defined(_DEBUG) # ifdef _WIN64 # define LoadExcHndl() LoadLibraryExA("dbg\\64\\exchndl", NULL, LOAD_WITH_ALTERED_SEARCH_PATH) # else # define LoadExcHndl() LoadLibraryExA("dbg\\exchndl", NULL, LOAD_WITH_ALTERED_SEARCH_PATH) # endif #else # define LoadExcHndl() #endif LoadExcHndl(); // LOAD_WITH_ALTERED_SEARCH_PATH works :P At least since Win2k g_instance = hInstance; if(LoadClockAPI("misc/T-Clock" ARCH_SUFFIX, &api)){ MessageBox(NULL, "Error loading: T-Clock" ARCH_SUFFIX ".dll", "API error", MB_OK|MB_ICONERROR); return 2; } chdir(api.root); // make sure we've got the right working directory // Make sure we're running Windows 2000 and above if(!api.OS) { MessageBox(NULL,"T-Clock requires Windows 2000 or newer","old OS",MB_OK|MB_ICONERROR); return 1; } // make sure ObjectBar isn't running -> From Original Code/Unclear if This is Still a Conflict. (test suggested not really.. no crash but no clock either :P) if(FindWindow("ObjectBar Main","ObjectBar")) { MessageBox(NULL,"ObjectBar and T-Clock can't be run together","ObjectBar detected!",MB_OK|MB_ICONERROR); return 1; } // Load ALL of the Global Resources g_hIconTClock = LoadIcon(api.hInstance, MAKEINTRESOURCE(IDI_MAIN)); g_hIconPlay = LoadImage(g_instance, MAKEINTRESOURCE(IDI_PLAY), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR); g_hIconStop = LoadImage(g_instance, MAKEINTRESOURCE(IDI_STOP), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR); g_hIconDel = LoadImage(g_instance, MAKEINTRESOURCE(IDI_DEL), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR); // FindTrayServer(hwndMain); // Make sure we're not running 32bit on 64bit OS / start the other one #ifndef _WIN64 if(IsWow64()){ hwndMain = FindWindow(g_szClassName, NULL); if(hwndMain) { // send commands to existing instance ProcessCommandLine(hwndMain,lpCmdLine); }else{ // start new instance char clock64[MAX_PATH]; memcpy(clock64, api.root, api.root_len+1); add_title(clock64,"Clock" ARCH_SUFFIX_64 ".exe"); api.Exec(clock64,lpCmdLine,NULL); } return 0; } #endif // _WIN64 // Do Not Allow the Program to Execute Twice! updated = 25; /**< wait up to 5 sec in 1/5th seconds for other instance */ do{ HANDLE processlock=CreateMutex(NULL,FALSE,g_szClassName); // we leak handle here, but Windows closes on process exit anyway (so why do it manually?) if(processlock && GetLastError()==ERROR_ALREADY_EXISTS){ CloseHandle(processlock); hwndMain = FindWindow(g_szClassName, NULL); if(hwndMain) { // This One Sends Commands to the Instance ProcessCommandLine(hwndMain,lpCmdLine); // That is Currently Running. return 0; } Sleep(200); continue; } break; }while(updated--); // Update settings if required and setup defaults if((updated=CheckSettings())<0){ return 1; } CancelAllTimersOnStartUp(); // Message of the taskbar recreating - Special thanks to Mr.Inuya g_WM_TaskbarCreated = RegisterWindowMessage("TaskbarCreated"); // register a window class wndclass.style = 0; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = g_instance; wndclass.hIcon = g_hIconTClock; wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)(intptr_t)(COLOR_WINDOW+1); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = g_szClassName; g_atomTClock = RegisterClass(&wndclass); if(api.OS >= TOS_VISTA) { // allow non elevated processes to send control messages (eg, App with admin rights, explorer without) #define MSGFLT_ADD 1 #define MSGFLT_REMOVE 2 typedef BOOL (WINAPI* ChangeWindowMessageFilter_t)(UINT message,DWORD dwFlag); ChangeWindowMessageFilter_t ChangeWindowMessageFilter=(ChangeWindowMessageFilter_t)GetProcAddress(GetModuleHandle("user32"), "ChangeWindowMessageFilter"); if(ChangeWindowMessageFilter){ int msgid; ChangeWindowMessageFilter(g_WM_TaskbarCreated,MSGFLT_ADD); ChangeWindowMessageFilter(WM_COMMAND,MSGFLT_ADD); for(msgid=WM_MOUSEFIRST; msgid<=WM_MOUSELAST; ++msgid) ChangeWindowMessageFilter(msgid,MSGFLT_ADD); for(msgid=MAINMFIRST; msgid<=MAINMLAST; ++msgid) ChangeWindowMessageFilter(msgid,MSGFLT_ADD); } } // create a hidden window g_hwndTClockMain = hwndMain = CreateWindowEx(WS_EX_NOACTIVATE, MAKEINTATOM(g_atomTClock),NULL, 0, 0,0,0,0, NULL,NULL,g_instance,NULL); // This Checks for First Instance Startup Options ProcessCommandLine(hwndMain,lpCmdLine); GetHotKeyInfo(hwndMain); if(api.OS > TOS_2000) { if(api.GetInt("Desktop", "MonOffOnLock", 0)) RegisterSession(hwndMain); } if(updated==1){ PostMessage(hwndMain,WM_COMMAND,IDM_SHOWPROP,0); } while(GetMessage(&msg, NULL, 0, 0)) { if(!(g_hwndSheet && IsWindow(g_hwndSheet) && PropSheet_IsDialogMessage(g_hwndSheet,&msg)) && !(g_hDlgTimer && IsWindow(g_hDlgTimer) && IsDialogMessage(g_hDlgTimer,&msg)) && !(g_hDlgTimerWatch && IsWindow(g_hDlgTimerWatch) && IsDialogMessage(g_hDlgTimerWatch,&msg)) && !(g_hDlgStopWatch && IsWindow(g_hDlgStopWatch) && IsDialogStopWatchMessage(g_hDlgStopWatch,&msg))){ TranslateMessage(&msg); DispatchMessage(&msg); } } UnregisterHotKey(hwndMain, HOT_TIMER); UnregisterHotKey(hwndMain, HOT_WATCH); UnregisterHotKey(hwndMain, HOT_STOPW); UnregisterHotKey(hwndMain, HOT_PROPR); UnregisterHotKey(hwndMain, HOT_CALEN); UnregisterHotKey(hwndMain, HOT_TSYNC); UnregisterSession(hwndMain); EndNewAPI(NULL); return (int)msg.wParam; }
int readwritedata(PLC *plc,LISTE *var) { int res,i,path_size; char ip[16],path[40]; Eip_Session *session=NULL; Eip_Connection *connection=NULL; path_size=ParsePath(plc->PlcPath,ip,path); if (path_size<=0) return(1); session=OpenSession(ip); if (session!=NULL) { session->Sender_ContextL=getpid(); if (RegisterSession(session)>=0) { if (plc->NetWork) connection=_ConnectPLCOverDHP(session, plc->PlcType, _Priority,_TimeOut_Ticks, (int)session, //TO_ConnID, GetSerial(), //ConnSerialNumber _OriginatorVendorID,_OriginatorSerialNumber,_TimeOutMultiplier, UPDATERATE*500, _Transport, plc->NetWork, path, path_size); else connection=_ConnectPLCOverCNET(session, plc->PlcType, _Priority,_TimeOut_Ticks, (int)session, //TO_ConnID, GetSerial(), //ConnSerialNumber _OriginatorVendorID,_OriginatorSerialNumber,_TimeOutMultiplier, UPDATERATE*500, _Transport, path, path_size); /******* Connection is Ok **********/ if (connection!=NULL) { Log(LOG_DEBUG,"Connection (%p) created for PLC : %s\n",connection,cip_err_msg); /*********** Reading / Writing Value from Lgx ******************/ for(i=0;i<AB_VARCOUNT;i++) { double val_float; int val_int; TAG *atag=var->Data[i]; Log(LOG_INFO,"entering ReadData %s\n",atag->Value_Address); res=readplc(plc,session,connection,atag->Value_Address,AB_REAL,&val_float); if (res) Log(LOG_WARNING,"ReadData (%d): %s\n",res,cip_err_msg); Log(LOG_INFO,"ReadData (%d): %s\n",res,cip_err_msg); atag->Value=val_float; val_int=1; Log(LOG_INFO,"entering WriteData %s = %f\n",atag->Reset_Address,val_int); res=writeplc(plc,session,connection,atag->Reset_Address,AB_BIT,&val_int); if (res) Log(LOG_WARNING,"WriteData (%d): %s\n",res,cip_err_msg); Log(LOG_INFO,"WriteData (%d): %s\n",res,cip_err_msg); } /***************************************************************/ if ((res=Forward_Close(connection))>=0) Log(LOG_DEBUG,"Connection (%p) Killed\n",connection); else Log(LOG_WARNING,"Unable to kill Connection (%p)\n",connection); } else //connection=NULL { Log(LOG_CRIT,"Unable to create connection : %s\n",cip_err_msg); } if ((res=UnRegisterSession(session))>=0) Log(LOG_DEBUG,"Session (%p) Killed\n",session); else Log(LOG_WARNING,"Unable to kill session (%p)\n",session); //Log(LOG_WARNING,"closing session (%p)\n",session); CloseSession(session); return(0); }else // Prob RegisterSession { Log(LOG_CRIT,"Unable to register session : %s \n",cip_err_msg); CloseSession(session); return(1); } } else { Log(LOG_CRIT,"Unable to open session for : %s\n",cip_err_msg); return(1); } }
int main(int argc,char *argv[]) { //cip_debuglevel=LogDebug; // You can uncomment this line to see the data exchange between TuxEip and your Logic controller char *IP="192.168.1.4\0"; BYTE path[]={1,0}; // char *IP="10.140.200.58\0"; // BYTE path[]={1,0}; int res; int count=1; char *var; int tns=getpid(); printf("Starting, Pid = %d\n",getpid()); if (argc<2) { printf("You have to provide a tag name\n"); return(1); } var=argv[1]; /* Openning a session */ printf("entering OpenSession \n"); Eip_Session *session=OpenSession(IP); if (session==NULL) { printf("Error : OpenSession %s (%d:%d)\n",cip_err_msg,cip_errno,cip_ext_errno); return(1); } printf("OpenSession Ok\n"); printf("entering RegisterSession \n"); res=RegisterSession(session); if (res!=Error) { int i,val_int,val_bool; float val_float; printf("RegisterSession Ok\n"); printf("entering ConnectPLCOverCNET\n"); Eip_Connection *connection=ConnectPLCOverCNET( session, // session whe have open SLC500, // plc type 0x12345678, // Target To Originator connection ID 0x6789, // Connection Serial Number 5000, // Request Packet Interval path, // Path to the ControlLogix sizeof(path) // path size ); if (connection!=NULL) { printf("ConnectPLCOverCNET Ok, \n"); printf("Reading %s\n",var); //LGX_Read *data=ReadPlcData(session,connection,var,1); PLC_Read *data=ReadPLCData(session,connection,NULL,NULL,0,SLC500,tns++,var,count); if (data!=NULL) { if (data->Varcount>0) { printf("ReadPLCData Ok :\n"); printf("\telements :%d\n\tDatatype : %d\n\ttotalsize : %d\n\tElement size : %d\n",data->Varcount,data->type,data->totalsize,data->elementsize,data->mask); for (i=0;i<data->Varcount;i++) { val_bool=PCCC_GetValueAsBoolean(data,i); val_int=PCCC_GetValueAsInteger(data,i); val_float=PCCC_GetValueAsFloat(data,i); printf("Value [%d] = %f (%d)\n",i,val_float,val_int,val_bool); } } else printf("Error ReadPLCData : %s\n",cip_err_msg); free(data); // You have to free the data return by ReadLgxData } else { printf("Error : ReadPLCData %s (%d:%d)\n",cip_err_msg,cip_errno,cip_ext_errno); } printf("entering Forward_Close\n"); res=Forward_Close(connection); if (res!=Error) printf("Forward_Close %s\n",cip_err_msg); else printf("Error : Forward_Close %s (%d:%d)\n",cip_err_msg,cip_errno,cip_ext_errno); } else { printf("Error : ConnectPLCOverCNET %s (%d:%d)\n",cip_err_msg,cip_errno,cip_ext_errno); } UnRegisterSession(session); printf("UnRegister : %s\n",cip_err_msg); }else { printf("Error : RegisterSession %s (%d:%d)\n",cip_err_msg,cip_errno,cip_ext_errno); } printf("entering CloseSession\n"); CloseSession(session); return(0); }
static void TermInit( TermNode *term) { SessionData *data; LD_Node *ld; int i; uuid_t u; ENTER_FUNC; data = NewSessionData(); data->term = term; data->fInProcess = TRUE; uuid_generate(u); uuid_unparse(u,data->hdr->uuid); if ((ld = g_hash_table_lookup(APS_Hash,ThisEnv->InitialLD)) == NULL) { Error("cannot find initial ld:%s.check directory",ThisEnv->InitialLD); } RecvnString(term->fp,SIZE_NAME,data->hdr->user);ON_IO_ERROR(term->fp,badio); RecvnString(term->fp,SIZE_HOST,data->host); ON_IO_ERROR(term->fp,badio); RecvnString(term->fp,SIZE_NAME,data->agent);ON_IO_ERROR(term->fp,badio); MessageLogPrintf("[%s:%s] session start",data->hdr->user,data->hdr->uuid); dbgprintf("uuid = [%s]",data->hdr->uuid); dbgprintf("user = [%s]",data->hdr->user); dbgprintf("host = [%s]",data->host); dbgprintf("agent = [%s]",data->agent); if (SesNum != 0 && GetSessionNum() >= SesNum) { Warning("Discard new session(%s);max session number(%d)",term,SesNum); SendPacketClass(term->fp,WFC_NOT); CloseNet(term->fp); return; } SendPacketClass(term->fp,WFC_OK); ON_IO_ERROR(term->fp,badio); SendString(term->fp,data->hdr->uuid); ON_IO_ERROR(term->fp,badio); data->ld = ld; data->linkdata = NewLinkData(); data->cWindow = ld->info->cWindow; data->scrdata = (LargeByteString **)xmalloc(sizeof(void*)*data->cWindow); for (i = 0 ; i < data->cWindow ; i ++) { if (data->ld->info->windows[i] != NULL) { dbgprintf("[%s]",data->ld->info->windows[i]->name); data->scrdata[i] = GetScreenData(data,data->ld->info->windows[i]->name); } else { data->scrdata[i] = NULL; } } data->hdr->puttype = SCREEN_NULL; RegisterSession(data); TermMain(term,data); CloseNet(term->fp); badio: LEAVE_FUNC; return; }
//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; }