void startConnMgrWatch(HWND hWnd){ HRESULT hr; hEdit=hWnd; if(WM_CM_STATUS_CHANGE==WM_USER+1234){ // do only once, if 1234 then not registered WM_CM_STATUS_CHANGE = RegisterWindowMessage( CONNMGR_STATUS_CHANGE_NOTIFICATION_MSG ); if(WM_CM_STATUS_CHANGE==0) //failed { logMsg(L"RegisterWindowMessage failed: %i\n", GetLastError()); } else logMsg(L"RegisterWindowMessage OK: id=%08x\n", WM_CM_STATUS_CHANGE); //DEBUGMSG(1, (L"RegisterWindowMessage =0x%x.\r\n", WM_CM_STATUS_CHANGE)); //logMsg(L"RegisterWindowMessage =0x%x.\r\n", WM_CM_STATUS_CHANGE); } // after you registered for the CONNMGR_STATUS_CHANGE_NOTIFICATION_MSG and got a constant you can watch for changes hr = ConnMgrRegisterForStatusChangeNotification(TRUE, hWnd); if(hr==S_OK) filelog(L"ConnMgrRegisterForStatusChangeNotification OK\n"); else if(hr==E_HANDLE) filelog(L"ConnMgrRegisterForStatusChangeNotification Invalid handle!\n"); else if(hr==E_ACCESSDENIED) filelog(L"ConnMgrRegisterForStatusChangeNotification Access denied!\n"); else filelog(L"ConnMgrRegisterForStatusChangeNotification Unknown error code: %i!\n", hr); return; }
DWORD WINAPI OnConnection(LPVOID lpParam) { Event *me = (Event *)lpParam; Configuration *conf; wstring subType; int delay, iterations, curIterations = 0, waitDelay = 0; HANDLE eventHandle, hConnManager = INVALID_HANDLE_VALUE; HRESULT hResult; MSG msg; BOOL connected = FALSE; eventHandle = me->getEvent(); me->setStatus(EVENT_RUNNING); conf = me->getConf(); try { delay = conf->getInt(L"delay") * 1000; } catch (...) { delay = INFINITE; } try { iterations = conf->getInt(L"iter"); } catch (...) { iterations = MAXINT; } // Attendiamo che il ConnectionManager sia pronto hConnManager = ConnMgrApiReadyEvent(); if (hConnManager == NULL || hConnManager == INVALID_HANDLE_VALUE) { DBG_TRACE(L"Debug - Connection.cpp - Connection FAILED [Connection Manager not present]\n", 5, FALSE); me->setStatus(EVENT_STOPPED); return 0; } if (WaitForSingleObject(hConnManager, 5000) != WAIT_OBJECT_0) { CloseHandle(hConnManager); DBG_TRACE(L"Debug - Connection.cpp - Connection FAILED [Connection Manager not ready]\n", 5, FALSE); me->setStatus(EVENT_STOPPED); return 0; } CloseHandle(hConnManager); HWND hWin = CreateWindow(L"STATIC", L"Window", 0, 0, 0, 0, 0, NULL, NULL, GetModuleHandle(NULL), NULL); if (hWin == NULL) { DBG_TRACE(L"Debug - Connection.cpp - Connection FAILED [Cannot create notification window]\n", 5, FALSE); me->setStatus(EVENT_STOPPED); return 0; } UINT uStatusChange = RegisterWindowMessage(CONNMGR_STATUS_CHANGE_NOTIFICATION_MSG); if (uStatusChange == 0) { DestroyWindow(hWin); DBG_TRACE(L"Debug - Connection.cpp - Connection FAILED [Cannot register window message]\n", 5, FALSE); me->setStatus(EVENT_STOPPED); return 0; } hResult = ConnMgrRegisterForStatusChangeNotification(TRUE, hWin); if (hResult != S_OK) { DestroyWindow(hWin); DBG_TRACE(L"Debug - Connection.cpp - Connection FAILED [Cannot register for notifications]\n", 5, FALSE); me->setStatus(EVENT_STOPPED); return 0; } DBG_TRACE(L"Debug - Connection.cpp - Connection Event started\n", 5, FALSE); LOOP { int now = GetTickCount(); if (PeekMessage(&msg, hWin, 0, 0, PM_REMOVE)) { if (msg.message == uStatusChange) { switch (msg.wParam) { case CONNMGR_STATUS_CONNECTED: WaitForSingleObject(eventHandle, 40000); if (me->shouldStop()) { ConnMgrRegisterForStatusChangeNotification(FALSE, hWin); DestroyWindow(hWin); me->setStatus(EVENT_STOPPED); DBG_TRACE(L"Debug - Connection.cpp - Connection clean stop [0]\n", 5, FALSE); return 0; } DBG_TRACE(L"Debug - Connection.cpp - Connection triggered [In Action]\n", 5, FALSE); me->triggerStart(); connected = TRUE; break; case CONNMGR_STATUS_DISCONNECTED: DBG_TRACE(L"Debug - Connection.cpp - Connection triggered [Out Action]\n", 5, FALSE); // Si potrebbe eseguire l'exit action qui me->triggerEnd(); connected = FALSE; break; default: break; } } DispatchMessage(&msg); } waitDelay += GetTickCount() - now; if (connected && waitDelay >= delay && curIterations < iterations) { waitDelay = 0; me->triggerRepeat(); curIterations++; } WaitForSingleObject(eventHandle, 5000); if (me->shouldStop()) { ConnMgrRegisterForStatusChangeNotification(FALSE, hWin); DestroyWindow(hWin); me->setStatus(EVENT_STOPPED); DBG_TRACE(L"Debug - Connection.cpp - Connection clean stop [0]\n", 5, FALSE); return 0; } } me->setStatus(EVENT_STOPPED); return 0; }