Exemplo n.º 1
0
DWORD WINAPI WiredEthernetMonitorThread(LPVOID lpParams)
{
    HRESULT hr;
    PMONITOR_THREAD_PARAMS pMonitorThreadParams = (PMONITOR_THREAD_PARAMS)lpParams;
    PIP_ADAPTER_ADDRESSES pOriginalAddresses = NULL;
    ULONG originalAddressesSize = 0;
    PIP_ADAPTER_ADDRESSES pNewAddresses = NULL;
    ULONG newAddressesSize = 0;
    PIP_ADAPTER_ADDRESSES pCurrOriginalAddress = NULL;
    PIP_ADAPTER_ADDRESSES pCurrNewAddress = NULL;

    WriteLineToLog("WiredEtherMonitorThread: Started");

    hr = GetInternetAdapterAddresses(&pOriginalAddresses, &originalAddressesSize);
    if(HB_FAILED(hr)) {
        WriteLineToLog("WiredEtherMonitorThread: Failed to get original adapter addresses");
        goto cleanexit;
    }

    while(1) {
        hr = GetInternetAdapterAddresses(&pNewAddresses, &newAddressesSize);
        if(HB_FAILED(hr)) {
            WriteLineToLog("WiredEtherMonitorThread: Failed to get new adapter addresses");
            goto cleanexit;
        }
        
        pCurrOriginalAddress = pOriginalAddresses;
        pCurrNewAddress = pNewAddresses;
        while(pCurrOriginalAddress && pCurrNewAddress) {
            if(pCurrNewAddress->IfType == IF_TYPE_ETHERNET_CSMACD) {
                if((pCurrNewAddress->OperStatus == IfOperStatusDown) && (pCurrOriginalAddress->OperStatus == IfOperStatusUp)) {
                    WriteLineToLog("WiredEtherMonitorThread: Firing monitor event");
                    SetEvent(pMonitorThreadParams->hMonitorEvent);
                    goto cleanexit;
                }
                else {
                    ResetEvent(pMonitorThreadParams->hMonitorEvent);
                }
            }
            pCurrOriginalAddress = pCurrOriginalAddress->Next;
            pCurrNewAddress = pCurrNewAddress->Next;
        }
        
        switch (WaitForSingleObject(pMonitorThreadParams->hMonitorStopEvent, DEFAULT_SLEEP_TIME)) {
        case WAIT_OBJECT_0:
            goto cleanexit;
        case WAIT_TIMEOUT:
            continue;
        }
    }

cleanexit:
    WriteLineToLog("WiredEtherMonitorThread: Exiting");

    HB_SAFE_FREE(pOriginalAddresses);
    HB_SAFE_FREE(pNewAddresses);
    HB_SAFE_FREE(pMonitorThreadParams);
    
    return 0;
}
Exemplo n.º 2
0
HRESULT GetInternetAdapterAddresses(__inout PIP_ADAPTER_ADDRESSES* ppAdapterAddresses,__inout PULONG pAdapterAddressesSize)
{
    HRESULT hr;
    ULONG rc = 0;

    rc = GetAdaptersAddresses(AF_UNSPEC,
                              GAA_FLAG_INCLUDE_PREFIX,
                              NULL,
                              *ppAdapterAddresses,
                              pAdapterAddressesSize);
    if(rc == ERROR_SUCCESS) {

        //
        // Our original buffer was large enough to store the result, so we're done.
        //

        hr = S_OK;
        goto cleanexit;
    }
    else if(rc != ERROR_BUFFER_OVERFLOW) {
        hr = HRESULT_FROM_WIN32(GetLastError());
        goto cleanexit;
    }

    //
    // Our original buffer couldn't store the result, so we need to allocate a larger buffer.
    //

    HB_SAFE_FREE(*ppAdapterAddresses);
    *ppAdapterAddresses = (PIP_ADAPTER_ADDRESSES)malloc(*pAdapterAddressesSize);
    if(*ppAdapterAddresses == NULL) {
        hr = E_OUTOFMEMORY;
        goto cleanexit;
    }

    rc = GetAdaptersAddresses(AF_UNSPEC,
                              GAA_FLAG_INCLUDE_PREFIX,
                              NULL,
                              *ppAdapterAddresses,
                              pAdapterAddressesSize);
    if(rc != ERROR_SUCCESS) {
        hr = HRESULT_FROM_WIN32(rc);
        goto cleanexit;
    }

    hr = S_OK;

cleanexit:

    return hr;
}
Exemplo n.º 3
0
DWORD WINAPI PowerMonitorThread(LPVOID lpParams)
{
    SYSTEM_POWER_STATUS PowerStatus;
    PMONITOR_THREAD_PARAMS pMonitorThreadParams = (PMONITOR_THREAD_PARAMS)lpParams;
    
    WriteLineToLog("PowerMonitorThread: Started");

    if(!GetSystemPowerStatus(&PowerStatus)) {
        WriteLineToLog("PowerMonitorThread: Error detecting initial power state.");
        goto cleanexit;
    }
    if(!PowerStatus.ACLineStatus) {
        WriteLineToLog("PowerMonitorThread: Machine was not connected to AC power at lock time.");
        goto cleanexit;
    }

    while(1) {
        if(!GetSystemPowerStatus(&PowerStatus)) {
            continue;
        }
        if(!PowerStatus.ACLineStatus) {
            WriteLineToLog("PowerMonitorThread: Firing monitor event");
            SetEvent(pMonitorThreadParams->hMonitorEvent);
            goto cleanexit;
        }
        else {
            ResetEvent(pMonitorThreadParams->hMonitorEvent);
        }
        switch (WaitForSingleObject(pMonitorThreadParams->hMonitorStopEvent, DEFAULT_SLEEP_TIME)) {
        case WAIT_OBJECT_0:
            goto cleanexit;
        case WAIT_TIMEOUT:
            continue;
        }
    }

cleanexit:
    WriteLineToLog("PowerMonitorThread: Exiting");
    HB_SAFE_FREE(pMonitorThreadParams);

    return 0;
}
Exemplo n.º 4
0
//
// Description:
//  Converts a BSTR to an equivalent PWSTR.
//
// Parameters:
//  bstr - A string represented as a BSTR.
//
//  ppWStr - On success, is set to the PWSTR equivalent of bstr. Caller must
//      free with HB_SAFE_FREE.
//
HRESULT BStrToPWchar(__in BSTR bstr, __out PWSTR* ppWStr)
{
    HRESULT hr;
    size_t cbWStr;
    PWSTR pWStrLocal = NULL;

    hr = SizeTMult(SysStringLen(bstr), sizeof(WCHAR), &cbWStr);
    if(HB_FAILED(hr)) {
        goto cleanexit;
    }

    //
    // Correct for additional NULL pointer
    //

    hr = SizeTAdd(cbWStr, sizeof(WCHAR), &cbWStr);
    if(HB_FAILED(hr)) {
        goto cleanexit;
    }

    pWStrLocal = (PWSTR)malloc(cbWStr);
    if(!pWStrLocal) {
        hr = E_OUTOFMEMORY;
        goto cleanexit;
    }

    hr =  StringCbCopy(pWStrLocal, cbWStr, static_cast<wchar_t*>(bstr));
    if(HB_FAILED(hr)) {
        goto cleanexit;
    }

    *ppWStr = pWStrLocal;
    pWStrLocal = NULL;

cleanexit:
    HB_SAFE_FREE(pWStrLocal);

    return hr;
}
Exemplo n.º 5
0
//
// Description:
//  Retrieves that the service manage uses to launch the YoNTMA binary as a
//  service.
//
// Parameters:
//  ppszServiceExecutionString - On success, contains the command line string
//      used to launch YoNTMA (including arguments). Caller must free with
//      HB_SAFE_FREE.
//
HRESULT GetServiceExecutionString(__out PTSTR* ppszServiceExecutionString)
{
    HRESULT hr;
    DWORD rc;
    SC_HANDLE hService = NULL;
    LPQUERY_SERVICE_CONFIG pQueryServiceConfig = NULL;
    DWORD cbQueryServiceConfig;
    DWORD cbQueryServiceConfigRequired;
    PTSTR pszServiceExecutionStringLocal = NULL;
    size_t cchServiceExecutionStringLocal;
    size_t cbServiceExecutionStringLocal;

    hr = OpenYontmaService(&hService);
    if(HB_FAILED(hr)) {
        goto cleanexit;
    }

    QueryServiceConfig(hService,
                       NULL,
                       0,
                       &cbQueryServiceConfigRequired);

    rc = GetLastError();

    //
    // QueryServiceConfig succeeded with a zero-sized buffer? Something is wrong.
    //

    if(rc == ERROR_SUCCESS) {
        hr = E_UNEXPECTED;
        goto cleanexit;
    }
    else if(rc != ERROR_INSUFFICIENT_BUFFER) {
        hr = HRESULT_FROM_WIN32(GetLastError());
        goto cleanexit;
    }

    cbQueryServiceConfig = cbQueryServiceConfigRequired;
    pQueryServiceConfig = (LPQUERY_SERVICE_CONFIG)LocalAlloc(LMEM_FIXED, cbQueryServiceConfig);
    if(!pQueryServiceConfig) {
        hr = E_OUTOFMEMORY;
        goto cleanexit;
    }

    if(!QueryServiceConfig(hService,
                           pQueryServiceConfig,
                           cbQueryServiceConfig,
                           &cbQueryServiceConfigRequired)) {
        hr = HRESULT_FROM_WIN32(GetLastError());
        goto cleanexit;
    }

    cchServiceExecutionStringLocal = _tcslen(pQueryServiceConfig->lpBinaryPathName);
    hr = SizeTAdd(cchServiceExecutionStringLocal,
                  1,
                  &cchServiceExecutionStringLocal);
    if(HB_FAILED(hr)) {
        goto cleanexit;
    }

    hr = SizeTMult(cchServiceExecutionStringLocal, sizeof(TCHAR), &cbServiceExecutionStringLocal);
    if(HB_FAILED(hr)) {
        goto cleanexit;
    }

    pszServiceExecutionStringLocal = (PTSTR)malloc(cbServiceExecutionStringLocal);
    if(!pszServiceExecutionStringLocal) {
        hr = E_OUTOFMEMORY;
        goto cleanexit;
    }

    hr = StringCchCopy(pszServiceExecutionStringLocal,
                       cchServiceExecutionStringLocal,
                       pQueryServiceConfig->lpBinaryPathName);
    if(HB_FAILED(hr)) {
        goto cleanexit;
    }

    *ppszServiceExecutionString = pszServiceExecutionStringLocal;
    pszServiceExecutionStringLocal = NULL;

cleanexit:
    HB_SAFE_CLOSE_SERVICE_HANDLE(hService);
    HB_SAFE_LOCAL_FREE(pQueryServiceConfig);
    HB_SAFE_FREE(pszServiceExecutionStringLocal);

    return hr;
}