⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 processmonitor.cpp

📁 一个WinCE6。0下的IP phone的源代码
💻 CPP
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include "ProcessMonitor.hpp"
#include <windows.h>
#include "Common.hpp"
#include "CommandAPI.hpp"


HANDLE ProcessMonitor_t::s_MonitoringList[ProcessMonitor_t::IdLast];

const
ProcessMonitor_t::Applications_t
ProcessMonitor_t::sc_Applications[ProcessMonitor_t::IdLast] =
{
    {L"PhoneApp.exe",       L"-n"}, //no UI
    {L"WakeUpEvent",        L""},
};

bool ProcessMonitor_t::s_IsMonitoring = false;

HRESULT
ProcessMonitor_t::LaunchApplications(
    void
    )
{
    memset(s_MonitoringList, sizeof(s_MonitoringList), 0);

    int Index;
    for (Index = 0; Index < IdLastMonitoredProcess; Index++)
    {
        //check if the application is already running
        HWND AppWindow = PHGetAppWindow(static_cast<PH_APPLICATION>(Index), FALSE);
        if (AppWindow)
        {
            DWORD ProcessId;
            GetWindowThreadProcessId(AppWindow, &ProcessId);
            s_MonitoringList[Index] = OpenProcess(
                0,
                FALSE,
                ProcessId
                );
            continue;
        }

        //otherwise launch the application
        HRESULT hr = PHLaunchProcess(
            sc_Applications[Index].m_pApplicationName,
            sc_Applications[Index].m_pArguments,
            &s_MonitoringList[Index]
            );
        if (FAILED(hr))
        {
            return hr;
        }
    }

    HANDLE WakeUpEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    if (!WakeUpEvent)
    {
        ASSERT(0);
        return CommonUtilities_t::GetErrorFromWin32();
    }
    s_MonitoringList[IdWakeUpEvent] = WakeUpEvent;

    HTHREAD MonitoringThread;

    MonitoringThread = CreateThread(
        0, // Security Attributes
        0, // Stack Size
        ProcessMonitor_t::MonitoringThreadProc,
        0, // Thread Parameters
        0, // Creation Flags
        NULL
        );
    if (!MonitoringThread)
    {
        return CommonUtilities_t::GetErrorFromWin32();
    }

    CloseHandle(MonitoringThread);
    return S_OK;
}


DWORD
ProcessMonitor_t::MonitoringThreadProc(
    void* pParameters
    )
{
    DWORD ReturnValue;
    bool Continue = true;

    s_IsMonitoring = true;

    while(Continue)
    {
        ReturnValue = WaitForMultipleObjects(
            _countof(s_MonitoringList), 
            s_MonitoringList,
            FALSE,
            INFINITE
            );

        switch (ReturnValue)
        {
        case IdPhoneApp:
            RETAILMSG(
                1,
                (
                    L"ProcessMonitor_t: %s seems to have died",
                    sc_Applications[ReturnValue - WAIT_OBJECT_0].m_pApplicationName
                    )
                );
            ASSERT(0);

            //Close previous handle
            CloseHandle(s_MonitoringList[ReturnValue - WAIT_OBJECT_0]);
            s_MonitoringList[ReturnValue - WAIT_OBJECT_0] = NULL;
              
            // PhoneApp.exe gone, launch this again
            if (FAILED(
                PHLaunchProcess(
                    sc_Applications[ReturnValue - WAIT_OBJECT_0].m_pApplicationName,
                    sc_Applications[ReturnValue - WAIT_OBJECT_0].m_pArguments,
                    &s_MonitoringList[ReturnValue - WAIT_OBJECT_0]
                    )
                ))
            {
                //Continue monitoring and wait on dummy event?
                Continue = false;
                s_MonitoringList[ReturnValue - WAIT_OBJECT_0] = NULL;
            }
            break;

        case IdWakeUpEvent:
            CloseHandle(s_MonitoringList[IdWakeUpEvent]);
            s_MonitoringList[IdWakeUpEvent] = NULL;
            Continue = false;
            break;

        default:
            ASSERT(0);                    
            ERRORMSG(
                1,
                (
                    L"MonitoringThreadProc: WaitForMultipleObjects failed with error: 0x%08X",
                    CommonUtilities_t::GetErrorFromWin32()
                    )
                );
            Continue = false;
            break;
        }
    }

    // Should never happen
    RETAILMSG(1, (L"MonitoringThreadProc exiting"));

    s_IsMonitoring = false;

    return 0;
}

HRESULT
ProcessMonitor_t::StopMonitoring(
    void
    )
{
    HRESULT hr = S_OK;

    if (s_IsMonitoring)
    {
        hr = SetEvent(s_MonitoringList[IdWakeUpEvent]) ?
            S_OK :
            CommonUtilities_t::GetErrorFromWin32();

        while (s_IsMonitoring);
    }

    //Clean up any outstanding handle
    int Index;
    for (Index = 0; Index < _countof(s_MonitoringList); Index++)
    {
        if (s_MonitoringList[Index])
        {
            CloseHandle(s_MonitoringList[Index]);
            s_MonitoringList[Index] = NULL;
        }
    }

    return hr;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -