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

📄 dllmain.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 <windows.h>
#include <commctrl.h>

#define INITGUID
#include <initguid.h>
#include "DisplayItem.hpp"
#undef INITGUID

#include "Common.hpp"
#include "Debug.hpp"
#include "Menubar.hpp"
#include "MenuButton.hpp"
#include "PopupMenu.hpp"
#include "ListBox.h"
#include "Edit.hpp"
#include "StatusRegion.hpp"
#include "ToolTip.hpp"
#include "Trackbar.hpp"
#include "DialogScreen.hpp"
#include "MessageBox.hpp"
#include "TransparentTextBox.hpp"
#include "Layout.hpp"
#include "PhoneAPI.hpp"
#include "MemTrace.hpp"
#include "CommandApi.hpp"
#include "Input.hpp"


MODULE_NAME (L"[PHCommon]");

#ifdef DEBUG

//Debug zone information
DBGPARAM dpCurSettings =
{
    //Module name
    L"PHCommon",

    //Debug zone names
    {
      L"Error",                     // 1
      L"Constructor/Destructor",    // 2   Lowest Hex Digit
      L"",                          // 4
      L"",                          // 8

      L"", L"", L"", L"",           // 2nd Hex digit
      L"", L"", L"", L"",           // 3rd Hex digit
      L"", L"", L"", L"",           // 4th Hex digit
    },

    //Initial debug zone mask
    0x0001
};
#endif

typedef HRESULT (*PFNPROCESSPROVISIONXML)(
    const WCHAR*    pInputBuffer, 
    WCHAR**         ppOutputBuffer
    ); 

typedef HRESULT (*PFNVALIDATEPROVISIONXML) (
    const WCHAR*    pInputBuffer
    ); 

typedef HRESULT (*PFNMAKEPHONECALL) (
    const PH_MAKEPHONECALL_PARAMETERS* pParameters
    ); 

typedef HRESULT (*PFNONHOOKDIALING) (
    const WCHAR CurrentChar
    ); 

const WCHAR c_PhoneCoreDllName[]        = L"PhoneCore.dll"; 
const WCHAR c_ProcessProvisionXML[]     = L"ProcessProvisionXML"; 
const WCHAR c_ValidateProvisionXML[]    = L"ValidateProvisionXML"; 
const WCHAR c_MakePhoneCall[]           = L"MakePhoneCall"; 
const WCHAR c_OnHookDialing[]           = L"OnHookDialing"; 

//Common HINSTANCE used across the dll
HINSTANCE GlobalData_t::s_ModuleInstance = NULL;
HBRUSH GlobalData_t::s_ListBoxBrush = NULL;
GDICache_t GlobalData_t::s_GDICacheObject;


MemTrackInitialize();

/*------------------------------------------------------------------------------
    PHInitCommonControls

    Registers control classes implemented by this dll.
------------------------------------------------------------------------------*/
EXTERN_C
BOOL
WINAPI
PHInitCommonControls(
    void
    )
{
    BOOL Success = TRUE;
    int Index;

    if (!CommonUtilities_t::MakeSureRegKeyExists())
    {
        ASSERT(0); 
        return FALSE; 
    }

    typedef bool (*PFN_CONTROL_INIT)(void);
    static const PFN_CONTROL_INIT sc_CommonControls[] =
        {
        MenuBarImpl_t::Register,
        MenuButtonImpl_t::Register,
        PopupMenuImpl_t::Register,
        ListBoxImpl_t::Register,
        EditImpl_t::Register,
        StatusHeaderImpl_t::Register,
        ToolTipImpl_t::Register,
        TrackBarImpl_t::Register,
        DialogScreen_t::Register,
        MessageBoxImpl_t::Register,
        TransparentTextBoxImpl_t::Register,
        };

    INITCOMMONCONTROLSEX CommonControlsEx = {0};
    CommonControlsEx.dwSize = sizeof(CommonControlsEx);
    CommonControlsEx.dwICC = ICC_LISTVIEW_CLASSES;

    Success = InitCommonControlsEx(&CommonControlsEx);
    if (!Success)
    {
        ASSERT(0);
        COMMON_RETAILMSG(ZONE_COMMON_ERROR, (L"InitCommonControlsEx failure."));
        return Success;
    }

    for (Index = 0; Index < _countof(sc_CommonControls); Index++)
    {
        Success = sc_CommonControls[Index]();
        if (!Success)
        {
            ASSERT(0);
            COMMON_RETAILMSG(ZONE_COMMON_ERROR, (L"Failed to initialize PHCommonControl class %d.", Index));
            return Success;
        }
    }

    if (FAILED(Layout_t::InitializeRCData()) ||
        FAILED(Colors_t::InitializeRCData()) ||
        FAILED(Fonts_t::InitializeRCData()))
    {
        ASSERT(0);
        Success = FALSE;
    }

    // Finally initialize the input library
    RECT DefaultScreen = {0};
    DefaultScreen.right = GetSystemMetrics(SM_CXSCREEN);
    DefaultScreen.bottom = GetSystemMetrics(SM_CYSCREEN) -
        2*Layout_t::ButtonHeight() +
        Layout_t::TextItemVerMargin();

    Input_Initialize(&DefaultScreen);

    return Success;
}

/*------------------------------------------------------------------------------
    PHUnInitCommonControls

    Cleans up any resources used by this dll.
------------------------------------------------------------------------------*/
EXTERN_C
BOOL
WINAPI
PHUnInitCommonControls(
    void
    )
{
    if (GlobalData_t::s_ListBoxBrush)
    {
        DeleteObject(GlobalData_t::s_ListBoxBrush);
        GlobalData_t::s_ListBoxBrush = NULL;
    }

    GlobalData_t::s_GDICacheObject.Reset();

    // Uninitialize the input library
    Input_Uninitialize();

    return TRUE;
}

/*------------------------------------------------------------------------------
    PHRegisterSingletonApplication
    
    Register this application as a singleton application - meaning only
    one instance of the app can run at a time.
    
    Parameters:
        : [IN]  AppName to register
        : [OUT] Handle registered on the app's behalf. Caller is responsible for
                calling CloseHandle on the result
    
    Returns (BOOL): TRUE indicates success. FALSE failure
------------------------------------------------------------------------------*/
EXTERN_C
BOOL
WINAPI
PHRegisterSingletonApplication(
    const WCHAR* pNameToRegister, 
    HANDLE* phApplication
    )
{
    //superficial input check
    if (! pNameToRegister || ! phApplication)
    {
        ASSERT(FALSE);

        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }

    //Clear the OUT pointer
    *phApplication = NULL;
    
    //create a mutex for this application instance
    HANDLE AppMutex = CreateMutex(NULL, FALSE, pNameToRegister);
    if (! AppMutex)
    {
        COMMON_RETAILMSG(ZONE_COMMON_ERROR, (L"Failed to create the named mutex. GetLastError: %d", GetLastError()));
        return FALSE;
    }

    switch(WaitForSingleObject(AppMutex, 0))
    {
    case WAIT_OBJECT_0:
        //No one owns the mutex yet - this instance is the rightful singleton
        //return the mutex handle
        *phApplication = AppMutex;
        return TRUE;

    case WAIT_TIMEOUT:
        //Another app owns the mutex
        break;

    default:
        ASSERT(FALSE);
        COMMON_RETAILMSG(ZONE_COMMON_ERROR, (L"WaitForSingleObject unexpectedly failed. GetLastError: %d", GetLastError()));
        break;
    }

    //we don't own the mutex - close the handle
    CloseHandle(AppMutex);
    return FALSE;
}


/*------------------------------------------------------------------------------
    PHProcessProvisionXML
    
    Process the provision XML string
    
    Parameters:
        pInputBuffer: [IN] pointer to the input buffer, which contains the provision XML string
        ppOutputBuffer: [OUT] pointer to the output buffer, which contains the output XML string
    
    Returns (HRESULT): 
        S_OK: process is 
------------------------------------------------------------------------------*/
EXTERN_C
HRESULT
WINAPI PHProcessProvisionXML(
    const WCHAR*    pInputBuffer, 
    WCHAR**         ppOutputBuffer
    )
{
    if (pInputBuffer == NULL)
    {
        return E_INVALIDARG; 
    }

    if (!CommonUtilities_t::MakeSureRegKeyExists())
    {
        return E_UNEXPECTED; 
    }

    HINSTANCE PhoneCoreDllHandle = LoadLibrary(c_PhoneCoreDllName); 
    if (PhoneCoreDllHandle == NULL)
    {
        COMMON_RETAILMSG(ZONE_COMMON_ERROR, (L"Failed to load the phonecore.dll . GetLastError: %d", GetLastError()));        
        return E_FAIL; 
    }

    PFNPROCESSPROVISIONXML  pfnProcessProvisionXML = (PFNPROCESSPROVISIONXML)GetProcAddress(
                                                                                PhoneCoreDllHandle, 
                                                                                c_ProcessProvisionXML
                                                                                ); 
    if (pfnProcessProvisionXML == NULL)
    {
        COMMON_RETAILMSG(ZONE_COMMON_ERROR, (L"Failed to get proc address of %s . GetLastError: %d", c_ProcessProvisionXML, GetLastError()));                
        return E_FAIL;         
    }

    HRESULT hr = pfnProcessProvisionXML(
                    pInputBuffer, 
                    ppOutputBuffer
                    ); 

    FreeLibrary(PhoneCoreDllHandle); 

    return hr;
}

/*------------------------------------------------------------------------------
    PHValidateProvisionXML
    
    Description
    
    Parameters:
        pInputBuffer: 
    
    Returns (HRESULT): 
------------------------------------------------------------------------------*/
EXTERN_C
HRESULT
WINAPI PHValidateProvisionXML(
    const WCHAR*    pInputBuffer
    )
{
    if (pInputBuffer == NULL)
    {
        return E_INVALIDARG; 
    }

    HINSTANCE PhoneCoreDllHandle = LoadLibrary(c_PhoneCoreDllName); 
    if (PhoneCoreDllHandle == NULL)
    {
        COMMON_RETAILMSG(ZONE_COMMON_ERROR, (L"Failed to load the phonecore.dll . GetLastError: %d", GetLastError()));        
        return E_FAIL; 
    }

    PFNVALIDATEPROVISIONXML pfnValidateProvisionXML = (PFNVALIDATEPROVISIONXML)GetProcAddress(
                                                                                    PhoneCoreDllHandle, 
                                                                                    c_ValidateProvisionXML
                                                                                    ); 
    
    if (pfnValidateProvisionXML == NULL)
    {
        COMMON_RETAILMSG(ZONE_COMMON_ERROR, (L"Failed to get proc address of %s . GetLastError: %d", c_ValidateProvisionXML, GetLastError()));                
        return E_FAIL;         
    }

    HRESULT hr = pfnValidateProvisionXML(
                    pInputBuffer
                    ); 

    FreeLibrary(PhoneCoreDllHandle); 

    return hr; 
}


EXTERN_C
HRESULT
PHMakePhoneCall(
    const PH_MAKEPHONECALL_PARAMETERS* pParameters
    )
{
    if (pParameters == NULL)
    {
        return E_INVALIDARG; 
    }

    HINSTANCE PhoneCoreDllHandle = LoadLibrary(c_PhoneCoreDllName); 
    if (PhoneCoreDllHandle == NULL)
    {
        COMMON_RETAILMSG(ZONE_COMMON_ERROR, (L"Failed to load the phonecore.dll . GetLastError: %d", GetLastError()));        
        return E_FAIL; 
    }

    PFNMAKEPHONECALL pfnMakePhoneCall = (PFNMAKEPHONECALL)GetProcAddress(
                                                                PhoneCoreDllHandle, 
                                                                c_MakePhoneCall
                                                                ); 
    
    if (pfnMakePhoneCall == NULL)
    {
        COMMON_RETAILMSG(ZONE_COMMON_ERROR, (L"Failed to get proc address of %s . GetLastError: %d", c_MakePhoneCall, GetLastError()));                
        return E_FAIL;         
    }

    HRESULT hr = pfnMakePhoneCall(
                    pParameters
                    ); 

    FreeLibrary(PhoneCoreDllHandle); 

    return hr; 
    
}

EXTERN_C
HRESULT
PHOnHookDialing(
    const WCHAR CurrentChar
    )
{
    if (CurrentChar < L'0' || CurrentChar > L'9')
    {
        return E_INVALIDARG; 
    }

    HINSTANCE PhoneCoreDllHandle = LoadLibrary(c_PhoneCoreDllName); 
    if (PhoneCoreDllHandle == NULL)
    {
        COMMON_RETAILMSG(ZONE_COMMON_ERROR, (L"Failed to load the phonecore.dll . GetLastError: %d", GetLastError()));        
        return E_FAIL; 
    }

    PFNONHOOKDIALING pfnOnHookDialing = (PFNONHOOKDIALING)GetProcAddress(
                                                                PhoneCoreDllHandle, 
                                                                c_OnHookDialing
                                                                ); 
    
    if (pfnOnHookDialing == NULL)
    {
        COMMON_RETAILMSG(ZONE_COMMON_ERROR, (L"Failed to get proc address of %s . GetLastError: %d", c_OnHookDialing, GetLastError()));                
        return E_FAIL;         
    }

    HRESULT hr = pfnOnHookDialing(
                    CurrentChar
                    ); 

    FreeLibrary(PhoneCoreDllHandle); 

    return hr; 
}

/*------------------------------------------------------------------------------
    DllMain

    Main entry point into this dll.
------------------------------------------------------------------------------*/
BOOL
WINAPI
DllMain(
    HANDLE ThisInstanceDll,
    DWORD Reason,
    void* pReserved
    )
{
    switch(Reason)
    {
        case DLL_PROCESS_ATTACH:
            GlobalData_t::s_ModuleInstance = reinterpret_cast<HINSTANCE>(ThisInstanceDll);

            //register our debug zones
            DEBUGREGISTER(GlobalData_t::s_ModuleInstance);

            //don't want to be notified for threads attaching/detaching
            DisableThreadLibraryCalls(GlobalData_t::s_ModuleInstance);
            break;

        case DLL_PROCESS_DETACH:
            MemTrackCheck();
            break;
    }

    return TRUE;
}

⌨️ 快捷键说明

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