From 2cf824fd2efaec5fa0125fdde5fd513dd906d844 Mon Sep 17 00:00:00 2001 From: Kirill Volinsky Date: Wed, 25 Sep 2013 19:25:38 +0000 Subject: plugins cleanup git-svn-id: http://svn.miranda-ng.org/main/trunk@6233 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/AutoShutdown/src/common.h | 2 + plugins/AutoShutdown/src/shutdownsvc.cpp | 121 +++++++++++++------------------ 2 files changed, 52 insertions(+), 71 deletions(-) (limited to 'plugins/AutoShutdown/src') diff --git a/plugins/AutoShutdown/src/common.h b/plugins/AutoShutdown/src/common.h index 2eae1b9bab..7a59d8961f 100644 --- a/plugins/AutoShutdown/src/common.h +++ b/plugins/AutoShutdown/src/common.h @@ -23,6 +23,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #include #include +#include +#include /* WinXP+: shutdown reason codes */ #if defined(EWX_RESTARTAPPS) /* new MS Platform SDK */ diff --git a/plugins/AutoShutdown/src/shutdownsvc.cpp b/plugins/AutoShutdown/src/shutdownsvc.cpp index fb351ebef5..f9fe70fb56 100644 --- a/plugins/AutoShutdown/src/shutdownsvc.cpp +++ b/plugins/AutoShutdown/src/shutdownsvc.cpp @@ -73,14 +73,7 @@ static BOOL IsShutdownTypeEnabled(BYTE shutdownType) switch(shutdownType) { case SDSDT_HIBERNATE: case SDSDT_STANDBY: - { HMODULE hPowerDLL=LoadLibraryA("POWRPROF"); /* all ascii */ - if (hPowerDLL != NULL) { - BOOLEAN (STDAPICALLTYPE *pfnIsPwrModeAllowed)(void); - *(PROC*)&pfnIsPwrModeAllowed=GetProcAddress(hPowerDLL,(shutdownType==SDSDT_HIBERNATE)?"IsPwrHibernateAllowed":"IsPwrSuspendAllowed"); - if (pfnIsPwrModeAllowed) bReturn=pfnIsPwrModeAllowed() != 0; - FreeLibrary(hPowerDLL); - } - } + bReturn = shutdownType == SDSDT_HIBERNATE ? IsPwrHibernateAllowed() : IsPwrSuspendAllowed() != 0; /* test privilege */ if (bReturn) { bReturn=WinNT_SetPrivilege(SE_SHUTDOWN_NAME,TRUE); @@ -117,7 +110,7 @@ static BOOL IsShutdownTypeEnabled(BYTE shutdownType) break; case SDSDT_CLOSERASCONNECTIONS: /* check if RAS installed/available */ - bReturn=SearchPath(NULL,_T("RASAPI32"),_T(".DLL"),0,NULL,NULL) != 0; + bReturn = TRUE; break; case SDSDT_SETMIRANDAOFFLINE: case SDSDT_CLOSEMIRANDA: @@ -180,74 +173,60 @@ static DWORD ShutdownNow(BYTE shutdownType) ShutdownNow(SDSDT_SETMIRANDAOFFLINE); /* set Miranda offline */ /* hang up all ras connections */ { - HMODULE hRasApiDLL=LoadLibrary(_T("RASAPI32")); /* all ascii */ - if (hRasApiDLL != NULL) { - DWORD (APIENTRY *pfnRasEnumConnections)(RASCONN*,DWORD*,DWORD*); - DWORD (APIENTRY *pfnRasHangUp)(HRASCONN); - DWORD (APIENTRY *pfnRasGetConnectStatus)(HRASCONN,RASCONNSTATUS*); - *(PROC*)&pfnRasEnumConnections=GetProcAddress(hRasApiDLL,"RasEnumConnectionsW"); - *(PROC*)&pfnRasHangUp=GetProcAddress(hRasApiDLL,"RasHangUpW"); - *(PROC*)&pfnRasGetConnectStatus=GetProcAddress(hRasApiDLL,"RasGetConnectStatusW"); - if (pfnRasEnumConnections && pfnRasGetConnectStatus && pfnRasHangUp) { - RASCONN *paConn; - RASCONN *paConnBuf; - DWORD dwConnSize,dwConnItems,dwRetries; - RASCONNSTATUS rcs; - DWORD dw,dwLastTickCount; - - dwConnSize=sizeof(RASCONN); - dwConnItems=0; - paConn=(RASCONN*)mir_alloc(dwConnSize); - dwErrCode=ERROR_NOT_ENOUGH_MEMORY; - if (paConn != NULL) { - for(dwRetries=5; dwRetries != 0; dwRetries--) { /* prevent infinite loop (rare) */ - ZeroMemory(paConn, dwConnSize); - paConn[0].dwSize = sizeof(RASCONN); - dwErrCode = pfnRasEnumConnections(paConn,&dwConnSize,&dwConnItems); - if (dwErrCode != ERROR_BUFFER_TOO_SMALL) break; - paConnBuf=(RASCONN*)mir_realloc(paConn,dwConnSize); - if (paConnBuf != NULL) { - mir_free(paConn); - paConn = NULL; - dwErrCode = ERROR_NOT_ENOUGH_MEMORY; - break; - } - paConn=paConnBuf; + RASCONN *paConn; + RASCONN *paConnBuf; + DWORD dwConnSize,dwConnItems,dwRetries; + RASCONNSTATUS rcs; + DWORD dw,dwLastTickCount; + + dwConnSize=sizeof(RASCONN); + dwConnItems=0; + paConn=(RASCONN*)mir_alloc(dwConnSize); + dwErrCode=ERROR_NOT_ENOUGH_MEMORY; + if (paConn != NULL) { + for(dwRetries=5; dwRetries != 0; dwRetries--) { /* prevent infinite loop (rare) */ + ZeroMemory(paConn, dwConnSize); + paConn[0].dwSize = sizeof(RASCONN); + dwErrCode = RasEnumConnections(paConn, &dwConnSize, &dwConnItems); + if (dwErrCode != ERROR_BUFFER_TOO_SMALL) break; + paConnBuf=(RASCONN*)mir_realloc(paConn,dwConnSize); + if (paConnBuf != NULL) { + mir_free(paConn); + paConn = NULL; + dwErrCode = ERROR_NOT_ENOUGH_MEMORY; + break; + } + paConn=paConnBuf; + } + if (dwErrCode==ERROR_SUCCESS || dwErrCode==ERROR_BUFFER_TOO_SMALL) { + for(dw=0;dw3000) - break; /* wraparound works */ - } - } + else { + dwErrCode = RasHangUp(paConn[dw].hrasconn); + if (!dwErrCode) paConn[dw].hrasconn=NULL; /* do not wait for on error */ + } + } + /* RAS does not allow to quit directly after HangUp (see docs) */ + dwLastTickCount = GetTickCount(); + ZeroMemory(&rcs,sizeof(RASCONNSTATUS)); + rcs.dwSize = sizeof(RASCONNSTATUS); + for(dw=0; dw < dwConnItems; ++dw) { + if (paConn[dw].hrasconn != NULL) { + while(RasGetConnectStatus(paConn[dw].hrasconn, &rcs) != ERROR_INVALID_HANDLE) { + Sleep(0); /* give rest of time silce to other threads with equal priority */ + /* infinite loop protection (3000ms defined in docs) */ + dwRetries = GetTickCount(); + if (dwRetries - dwLastTickCount>3000) + break; /* wraparound works */ } } - mir_free(paConn); /* does NULL check */ } } - else dwErrCode = GetLastError(); - FreeLibrary(hRasApiDLL); + mir_free(paConn); /* does NULL check */ } - else dwErrCode = GetLastError(); } /* set Miranda to offline again, to remain offline with reconnection plugins */ ShutdownNow(SDSDT_SETMIRANDAOFFLINE); -- cgit v1.2.3