BOOL CHttpRequest::IsConnected() { ODS(_T("XFILTER.EXE: Internet Check Connected...")); RASCONN lpRasConn; RASCONNSTATUS rasStatus; DWORD cbBuf = 0; DWORD cConn = 0; DWORD dwRet = 0; cbBuf = sizeof(RASCONN); lpRasConn.dwSize = sizeof(RASCONN ); dwRet = RasEnumConnections(&lpRasConn, &cbBuf, &cConn ); if ( dwRet != 0 ) return FALSE; else { rasStatus.dwSize = sizeof(RASCONNSTATUS); RasGetConnectStatus(lpRasConn.hrasconn,&rasStatus); if (rasStatus.rasconnstate==RASCS_Connected) return TRUE; else return FALSE; } return TRUE; }
void CScheduler::HangUpConnection() { DWORD dwCb = sizeof( RASCONN ); DWORD dwConnections = 0; RASCONN* lpRasConn = NULL; LPRASCONNSTATUS RasConStatus = 0; for (;;) { // Free the memory if necessary. if ( lpRasConn != NULL ) { HeapFree( GetProcessHeap(), 0, lpRasConn ); lpRasConn = NULL; } // Allocate the size needed for the RAS structure. lpRasConn = (RASCONN*)HeapAlloc( GetProcessHeap(), 0, dwCb ); if ( ! lpRasConn ) // Out of memory return; // Set the first structure size for version checking purposes. lpRasConn->dwSize = sizeof( RASCONN ); // Call the RAS API DWORD ret = RasEnumConnections( lpRasConn, &dwCb, &dwConnections ); if ( ret == 0 ) // Ok break; if ( ret == ERROR_NOT_ENOUGH_MEMORY ) // Re-allocate more memory continue; // Error return; } DWORD loop = 0; for ( DWORD i = 0; i < dwConnections; ++i ) // Loop through all current connections { RasHangUp( lpRasConn[i].hrasconn ); // Hang up the connection while( ( RasGetConnectStatus( lpRasConn[i].hrasconn,RasConStatus ) || ( loop > 10 ) ) ) { // Loop until the connection handle is invalid, or 3 seconds have passed Sleep( 300 ); loop++; } } // Free the memory if necessary. if ( lpRasConn != NULL ) { HeapFree( GetProcessHeap(), 0, lpRasConn ); lpRasConn = NULL; } }
BOOL findCurrentlyConnected(BOOL* foundOut) { DWORD dwCb = 0; DWORD dwRet = ERROR_SUCCESS; DWORD dwConnections = 0; LPRASCONN lpRasConn = NULL; // Call RasEnumConnections with lpRasConn = NULL. dwCb is returned with the required buffer size and // a return code of ERROR_BUFFER_TOO_SMALL dwRet = RasEnumConnections(lpRasConn, &dwCb, &dwConnections); if (dwRet == ERROR_BUFFER_TOO_SMALL) { // Allocate the memory needed for the array of RAS structure(s). lpRasConn = (LPRASCONN) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb); if (lpRasConn == NULL){ fwprintf(stderr, L"HeapAlloc failed!\n"); return FALSE; } // The first RASCONN structure in the array must contain the RASCONN structure size lpRasConn[0].dwSize = sizeof(RASCONN); // Call RasEnumConnections to enumerate active connections dwRet = RasEnumConnections(lpRasConn, &dwCb, &dwConnections); // If successful, print the names of the active connections. if (ERROR_SUCCESS == dwRet){ for (DWORD i = 0; i < dwConnections; i++) { if (lstrcmp(ENTRY_NAME_W, lpRasConn[i].szEntryName) == 0) { *foundOut = TRUE; break; }; } } //Deallocate memory for the connection buffer HeapFree(GetProcessHeap(), 0, lpRasConn); lpRasConn = NULL; return TRUE; } return TRUE; }
// Figure out which Dial-Up or VPN connection is active; in a normal LAN connection, this should // return NULL LPTSTR FindActiveConnection() { DWORD dwCb = sizeof(RASCONN); DWORD dwErr = ERROR_SUCCESS; DWORD dwRetries = 5; DWORD dwConnections = 0; RASCONN* lpRasConn = NULL; RASCONNSTATUS rasconnstatus; rasconnstatus.dwSize = sizeof(RASCONNSTATUS); // // Loop through in case the information from RAS changes between calls. // while (dwRetries--) { // If the memory is allocated, free it. if (NULL != lpRasConn) { HeapFree(GetProcessHeap(), 0, lpRasConn); lpRasConn = NULL; } // Allocate the size needed for the RAS structure. lpRasConn = (RASCONN*)HeapAlloc(GetProcessHeap(), 0, dwCb); if (NULL == lpRasConn) { dwErr = ERROR_NOT_ENOUGH_MEMORY; break; } // Set the structure size for version checking purposes. lpRasConn->dwSize = sizeof(RASCONN); // Call the RAS API then exit the loop if we are successful or an unknown // error occurs. dwErr = RasEnumConnections(lpRasConn, &dwCb, &dwConnections); if (ERROR_INSUFFICIENT_BUFFER != dwErr) { break; } } // // In the success case, print the names of the connections. // if (ERROR_SUCCESS == dwErr) { DWORD i; for (i = 0; i < dwConnections; i++) { RasGetConnectStatus(lpRasConn[i].hrasconn, &rasconnstatus); if (rasconnstatus.rasconnstate == RASCS_Connected){ return lpRasConn[i].szEntryName; } } } return NULL; // Couldn't find an active dial-up/VPN connection; return NULL }
BOOL CAutoDialDlg::AutoCheck() { m_csStatus = ""; if (m_csEntryName.IsEmpty()){ m_csStatus = "Empty EntryName!"; return FALSE; } RASCONN rasconn; DWORD dwConnCount(0); DWORD dwSize(0); DWORD dwRet(0); rasconn.dwSize = 0x19c;//sizeof(RASCONN); dwSize = rasconn.dwSize; dwRet = RasEnumConnections(&rasconn, &dwSize, &dwConnCount); if (ERROR_SUCCESS != dwRet){ m_csStatus.Format("RasEnumConn Fail:%d", dwRet); return FALSE; } if (dwConnCount > 0){//有连接直接退出 m_csStatus.Format("Connected"); return TRUE; //RasHangUp(rasconn.hrasconn); } //没有连接 //DWORD dwConn(0); //RASDIALPARAMS stRasParam; //BOOL bHasPw; //stRasParam.dwSize = sizeof(RASDIALPARAMS); //strcpy_s(stRasParam.szEntryName, RAS_MaxEntryName, m_csEntryName.GetBuffer()); // //dwRet = RasGetEntryDialParams(NULL, &stRasParam, &bHasPw); //if (ERROR_SUCCESS != dwRet){ // m_csStatus.Format("GetEntry Fail:%d", dwRet); // return FALSE; //} //HRASCONN hRasConn(NULL); //dwRet = RasDial(NULL, NULL, &stRasParam, 0, NULL, &hRasConn); //if (ERROR_SUCCESS != dwRet){ // m_csStatus.Format("RasDial Fail:%d", dwRet); // return FALSE; //} WinExec(m_csCmdLine, SW_HIDE); return FALSE; }
int GetCurrentConnections(BYTE (&RasConnData)[10*sizeof(RASCONN)]) { // Get the current list of connections //BYTE RasConnData[10*sizeof(RASCONN)]; LPRASCONN pRasConn = (LPRASCONN) RasConnData; pRasConn->dwSize = sizeof(RASCONN); DWORD Connections = 0; DWORD cb = sizeof(RasConnData); if ( RasEnumConnections(pRasConn, &cb, &Connections)) { return 0; } return Connections; }
int main( int argc, char *argv ) { LPRASCONN lpRasConn; int nRet, i; DWORD lpcb, lpcConnections; HWND dw; HDC hdc; lpRasConn = ( LPRASCONN ) GlobalAlloc(GPTR, sizeof(RASCONN)); lpRasConn->dwSize = sizeof( RASCONN ); nRet = RasEnumConnections( lpRasConn, &lpcb, &lpcConnections ); if( nRet != 0 ) { printf( "RasEnumConnections failed: Error = %d", nRet ); } else { printf( "The following RAS connections are currently active\n\n" ); for( i = 0; i < lpcConnections; i++ ) { printf( "Entry name: %s\n", lpRasConn->szEntryName ); lpRasConn++; } if( lpcConnections < 1 ) { printf( "error! no connection!" ); dw = FindWindow( "#32769", NULL ); hdc = GetDC( dw ); TextOut( hdc, 15, 15, "PLEASE PLUG THE NETWORK CABLE BACK IN!", 28 ); for( i = 0x250; i < 0x2500; i += 0x100 ) { Beep( i, 10 ); } ReleaseDC( dw, hdc ); } } return 0; }
value_t c_win32_iterate_connections ( value_t iterator ) { RASCONN * lpRasConn; DWORD lpcb, lpcConnections; unsigned int nRet, i; CAMLparam1 ( iterator ); CAMLlocal1 ( name ); #ifdef WRAS_EMULATION lpRasConn = calloc ( 1, sizeof(RASCONN)); #else lpRasConn = (LPRASCONN) GlobalAlloc(GPTR, sizeof(RASCONN)); #endif lpRasConn->dwSize = sizeof(RASCONN); lpcb = sizeof(RASCONN); nRet = RasEnumConnections(lpRasConn, &lpcb, &lpcConnections); for ( i = 0; i < lpcConnections; i ++ ) { if ( debug_print ) printf ( "Processing connection %s 0x%08x\n", lpRasConn[i].szEntryName, (unsigned) lpRasConn[i].hrasconn ); name = copy_string ( lpRasConn[i].szEntryName ); callback3 ( iterator, name, Val_int (LOWORD(lpRasConn[i].hrasconn)), Val_int (HIWORD(lpRasConn[i].hrasconn))); } #ifdef WRAS_EMULATION free ( lpRasConn ); #else GlobalFree ( lpRasConn ); #endif CAMLreturn (Val_int ( lpcConnections )); return 0; /* dummy, to shut down warning */ }
bool CHttpReqSocket::IsInternetConnected () { return true; // MCH 17.09.04 (what the heck...) ////////////////////////////////////////////////////////////// DWORD dwState; InternetGetConnectedState (&dwState, 0); if (dwState & INTERNET_CONNECTION_LAN) return true; else { bool bConnected = false; RASCONN rasConn; rasConn.dwSize = sizeof (RASCONN); DWORD dwBufSize = sizeof (RASCONN); DWORD dwNumConns; if (RasEnumConnections (&rasConn, &dwBufSize, &dwNumConns) == 0) { // RasEnumConnections succeeded RASCONNSTATUS rasConnStatus; for (UINT i = 0; i < dwNumConns; i++) { rasConnStatus.dwSize = sizeof (RASCONNSTATUS); RasGetConnectStatus (rasConn.hrasconn, &rasConnStatus); if (rasConnStatus.rasconnstate == RASCS_Connected) { bConnected = true; break; } } } return bConnected; } }
static DWORD ShutdownNow(BYTE shutdownType) { DWORD dwErrCode = ERROR_SUCCESS; switch (shutdownType) { case SDSDT_CLOSEMIRANDA: if (!Miranda_Terminated()) { /* waiting for short until ready (but not too long...) */ DWORD dwLastTickCount = GetTickCount(); while (!CallService(MS_SYSTEM_OKTOEXIT, 0, 0)) { /* infinite loop protection (max 5 sec) */ if (GetTickCount() - dwLastTickCount >= 5000) { /* wraparound works */ OutputDebugStringA("Timeout (5 sec)\n"); /* tell others, all ascii */ break; } SleepEx(1000, TRUE); if (Miranda_Terminated()) break; /* someone else did it */ OutputDebugStringA("Not ready to exit. Waiting...\n"); /* tell others, all ascii */ } /* shutdown service must be called from main thread anyway */ if (!DestroyWindow(pcli->hwndContactList)) dwErrCode = GetLastError(); } break; case SDSDT_SETMIRANDAOFFLINE: /* set global status mode to offline (is remembered by Miranda on exit) */ CallService(MS_CLIST_SETSTATUSMODE, (WPARAM)ID_STATUS_OFFLINE, 0); break; case SDSDT_STANDBY: case SDSDT_HIBERNATE: WinNT_SetPrivilege(SE_SHUTDOWN_NAME, TRUE); if (!SetSystemPowerState(shutdownType == SDSDT_STANDBY, TRUE)) dwErrCode = GetLastError(); WinNT_SetPrivilege(SE_SHUTDOWN_NAME, FALSE); break; case SDSDT_LOCKWORKSTATION: if (!IsWorkstationLocked()) dwErrCode = GetLastError(); break; case SDSDT_CLOSERASCONNECTIONS: ShutdownNow(SDSDT_SETMIRANDAOFFLINE); /* set Miranda offline */ /* hang up all ras connections */ { DWORD dwRetries; RASCONNSTATUS rcs; DWORD dw, dwLastTickCount; DWORD dwConnSize = sizeof(RASCONN); DWORD dwConnItems = 0; RASCONN *paConn = (RASCONN*)mir_alloc(dwConnSize); dwErrCode = ERROR_NOT_ENOUGH_MEMORY; if (paConn != NULL) { for (dwRetries = 5; dwRetries != 0; dwRetries--) { /* prevent infinite loop (rare) */ memset(paConn, 0, dwConnSize); paConn[0].dwSize = sizeof(RASCONN); dwErrCode = RasEnumConnections(paConn, &dwConnSize, &dwConnItems); if (dwErrCode != ERROR_BUFFER_TOO_SMALL) break; RASCONN *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; dw < dwConnItems; ++dw) { if (dwErrCode) { if (RasHangUp(paConn[dw].hrasconn)) paConn[dw].hrasconn = NULL; /* do not wait for on error */ } 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(); memset(&rcs, 0, 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 */ } } /* set Miranda to offline again, to remain offline with reconnection plugins */ ShutdownNow(SDSDT_SETMIRANDAOFFLINE); break; case SDSDT_REBOOT: case SDSDT_SHUTDOWN: if (GetSystemMetrics(SM_SHUTTINGDOWN)) { /* Win2000+, 0 on error */ dwErrCode = ERROR_SHUTDOWN_IN_PROGRESS; break; } /* WinNT4/2000/XP */ { WinNT_SetPrivilege(SE_SHUTDOWN_NAME, TRUE); /* does not send out WM_ENDSESSION messages, so we do it manually to * give the applications the chance to save their data */ WinNT_SetPrivilege(SE_TCB_NAME, TRUE); /* for BSM_ALLDESKTOPS */ BroadcastEndSession(BSM_APPLICATIONS | BSM_ALLDESKTOPS, ENDSESSION_CLOSEAPP); /* app should close itself */ WinNT_SetPrivilege(SE_TCB_NAME, FALSE); if (!InitiateSystemShutdownEx(NULL, TranslateT("AutoShutdown"), 0, TRUE, shutdownType == SDSDT_REBOOT, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER | SHTDN_REASON_FLAG_PLANNED)) dwErrCode = GetLastError(); /* cleanly close Miranda */ if (!dwErrCode) ShutdownNow(SDSDT_CLOSEMIRANDA); break; } /* fall through for Win9x */ case SDSDT_LOGOFF: { UINT flags; switch (shutdownType) { case SDSDT_LOGOFF: flags = EWX_LOGOFF; break; case SDSDT_REBOOT: flags = EWX_REBOOT; break; default: flags = EWX_SHUTDOWN | EWX_POWEROFF; } if (shutdownType == SDSDT_LOGOFF && !IsWorkstationLocked()) flags |= EWX_FORCEIFHUNG; /* only considered for WM_ENDSESSION messages */ else flags |= EWX_FORCE; /* must be used when workstation locked */ if (flags & EWX_FORCE) { /* EWX_FORCE does not send out WM_ENDSESSION messages, so we do it * manually to give the applications the chance to save their data */ BroadcastEndSession(BSM_APPLICATIONS, (shutdownType == SDSDT_LOGOFF) ? ENDSESSION_LOGOFF : 0); /* Windows Me/98/95 (msdn): Because of the design of the shell, * calling ExitWindowsEx with EWX_FORCE fails to completely log off * the user (the system terminates the applications and displays the * Enter Windows Password dialog box, however, the user's desktop remains.) * To log off the user forcibly, terminate the Explorer process before calling * ExitWindowsEx with EWX_LOGOFF and EWX_FORCE. */ } if (!ExitWindowsEx(flags, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER | SHTDN_REASON_FLAG_PLANNED)) dwErrCode = GetLastError(); /* cleanly close Miranda */ if (!dwErrCode) ShutdownNow(SDSDT_CLOSEMIRANDA); } break; } return dwErrCode; }
int __cdecl main(void) { DWORD nRet = 0; LPRASCONN lpRasConn = NULL; LPRASCONN lpTempRasConn = NULL; DWORD cb = sizeof(RASCONN); DWORD cConnections = 0; RAS_STATS *lpStatistics = NULL; BOOL fSuccess = FALSE; DWORD i = 0; TCHAR szTempBuf[256] = {0}; // Allocate buffer with default value lpRasConn = (LPRASCONN)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb); if (NULL == lpRasConn) { printf("HeapAlloc failed.\n"); return ERROR_OUTOFMEMORY; } lpRasConn->dwSize = sizeof(RASCONN); // Call RasEnumConnections to obtain the handle of the current active RAS connection nRet = RasEnumConnections(lpRasConn, &cb, &cConnections); switch (nRet) { case ERROR_BUFFER_TOO_SMALL: if (HeapFree(GetProcessHeap(), 0, (LPVOID)lpRasConn)) { lpRasConn = (LPRASCONN)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb); if (NULL == lpRasConn) { printf("HeapAlloc failed.\n"); return ERROR_OUTOFMEMORY; } lpRasConn->dwSize = sizeof(RASCONN); nRet = RasEnumConnections(lpRasConn, &cb, &cConnections); if (ERROR_SUCCESS == nRet) { fSuccess = TRUE; } else { printf("RasEnumConnections failed: Error = %d\n", nRet); goto done; } } else { printf("HeapFree failed.\n"); return GetLastError(); } break; case ERROR_SUCCESS: fSuccess = TRUE; break; default: printf("RasEnumConnections failed: Error = %d\n", nRet); goto done; break; } if (fSuccess) { // Allocate buffer to obtain the RAS statistics lpStatistics = (RAS_STATS*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RAS_STATS)); if (NULL == lpStatistics) { printf("HeapAlloc failed.\n"); goto done; } lpTempRasConn = lpRasConn; for (i = 0; i < cConnections; i++) { ZeroMemory(lpStatistics, sizeof(RAS_STATS)); lpStatistics->dwSize = sizeof(RAS_STATS); // Call RasGetConnectionStatistics nRet = RasGetConnectionStatistics(lpTempRasConn->hrasconn, lpStatistics); if(ERROR_SUCCESS != nRet) // RasGetConnectionStatistics returned an error { printf("RasGetConnectionStatistics failed: Error = %d\n", nRet); goto done; } // Print the results obtained StringCchPrintf(szTempBuf, CELEMS(szTempBuf), "Statistics for %s connection\n\n", lpTempRasConn->szEntryName); printf(szTempBuf); printf("Bytes Xmited\t\t\t%d\n", lpStatistics->dwBytesXmited); printf("Bytes Received\t\t\t%d\n", lpStatistics->dwBytesRcved); printf("Frames Xmited\t\t\t%d\n", lpStatistics->dwFramesXmited); printf("Frames Received\t\t\t%d\n", lpStatistics->dwFramesRcved); printf("Crc Error\t\t\t%d\n", lpStatistics->dwCrcErr); printf("Timeout Error\t\t\t%d\n", lpStatistics->dwTimeoutErr); printf("Alignment Error\t\t\t%d\n", lpStatistics->dwAlignmentErr); printf("Hardware Overrun Error\t\t%d\n", lpStatistics->dwHardwareOverrunErr); printf("Framing Error\t\t\t%d\n", lpStatistics->dwFramingErr); printf("Buffer Overrun Error\t\t%d\n", lpStatistics->dwBufferOverrunErr); printf("Compression Ratio [In]\t\t%d\n", lpStatistics->dwCompressionRatioIn); printf("Compression Ratio [Out]\t\t%d\n", lpStatistics->dwCompressionRatioOut); printf("Baud Rate [bps]\t\t\t%d\n", lpStatistics->dwBps); printf("Connection Duration [mili sec]\t%d\n", lpStatistics->dwConnectDuration); lpTempRasConn++; } nRet = ERROR_SUCCESS; } done: // Clean up if (lpRasConn) { HeapFree(GetProcessHeap(), 0, (LPVOID)lpRasConn); } if (lpStatistics) { HeapFree(GetProcessHeap(), 0, (LPVOID)lpStatistics); } return (int)nRet; }