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

📄 mapirule.cpp

📁 采用cemapi库
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
// ************************************************************
// MAPIRULE.CPP
// 
// Sample implementation of a message Rule Client
//
//


#define INITGUID
#include "windows.h"
#include "cemapi.h"

int g_cServerLocks = 0;

// {3AB4C10E-673C-494c-98A2-CC2E91A48115}
DEFINE_GUID(CLSID_MapiRuleSample, 0x3ab4c10e, 0x673c, 0x494c, 0x98, 0xa2, 0xcc, 0x2e, 0x91, 0xa4, 0x81, 0x15);

/* Add to apps.reg:
[HKEY_CLASSES_ROOT\CLSID\{3AB4C10E-673C-494c-98A2-CC2E91A48115}\InProcServer32]
@="mapirule.dll"
*/

/*
Add this line too:
[HKEY_LOCAL_MACHINE\Software\Microsoft\Inbox\Svc\SMS\Rules]
"{3AB4C10E-673C-494c-98A2-CC2E91A48115}"=dword:1
*/




// **************************************************************************
// Function Name: DeleteMessage
//
// Purpose: Delete a MAPI message

// Arguments:
//		IN IMsgStore* pMsgStore - Message Store to delete from
//		IN IMessage* pMsg - ptr to message to be deleted
//		IN ULONG cbMsg - The size of lpMsg, in bytes
//		IN LPENTRYID lpMsg - The ENTRYID of the message
//		IN ULONG cbDestFolder - The size of lpDestFolder, in bytes
//		IN LPENTRYID lpDestFolder - The ENTRYID of the folder that incoming
//			messages are moved to
//		OUT ULONG *pulEventType - Combination of bit flags indicating the type if 
//			action performed on the message, deletion in this case
//		OUT MRCHANDLED *pHandled - Indicates the type of handling that occurred,
//			in this case we mark the message as handled, and do not pass it on 
//
// Return Values:  HRESULT depending on success of MAPI operations

// Side effects:  

// Description:  
//	This function deletes a given message from a given folder and 
//  sets the proper notification event  

HRESULT DeleteMessage(IMsgStore *pMsgStore, IMessage *pMsg, ULONG cbMsg, LPENTRYID lpMsg, ULONG cbDestFolder, 
                        LPENTRYID lpDestFolder, ULONG *pulEventType, MRCHANDLED *pHandled)
{
    HRESULT hr = S_OK;
    ENTRYLIST lst;
    SBinary sbin;
    IMAPIFolder *pFolder = NULL;
    
    // Delete it
    hr = pMsgStore->OpenEntry(cbDestFolder, lpDestFolder, NULL, 0, NULL, (LPUNKNOWN *) &pFolder);
    if (FAILED(hr))
    {
        RETAILMSG(TRUE, (TEXT("Couldn't get the folder!\r\n")));
        goto Exit;
    }
    
    lst.cValues = 1;
    sbin.cb = cbMsg;
    sbin.lpb = (LPBYTE) lpMsg;
    lst.lpbin = &sbin;

    hr = pFolder->DeleteMessages(&lst, NULL, NULL, 0); 
    if (FAILED(hr))
    {
        RETAILMSG(TRUE, (TEXT("Couldn't delete messages!\r\n")));
        goto Exit;
    }

    // Notification object lets listeners know we deleted this
	*pulEventType = fnevObjectDeleted;
	// Mark as handled and don't pass on
    *pHandled = MRC_HANDLED_DONTCONTINUE;
    
Exit:
    if (pFolder)
    {
        pFolder->Release();
    }
    
    return hr;
}

// ************************************************************
// Class CMailRuleClient - Implementation of IMailRuleClient
//
// Inheritance:
//     IMailRuleClient IUnknown (Abstract)
//
// Purpose:
//     This class serves as implementation for the IMailRuleClient
//	   interface and provides our Rule Client functionality.
//	   The Initialize method sets our permissions to interact
//	   with the message store, and the ProcesseMessage method
//	   defines how we handle incoming messages
//
// ************************************************************

class CMailRuleClient : public IMailRuleClient
{
public:
    CMailRuleClient();
    ~CMailRuleClient();
    
    // IUnknown
    STDMETHOD (QueryInterface)(REFIID iid, LPVOID *ppv);
    STDMETHOD_(ULONG, AddRef)();
    STDMETHOD_(ULONG, Release)();

    // IMailRuleClient
    MAPIMETHOD(Initialize)(
            IMsgStore *pMsgStore,
            MRCACCESS *pmaDesired
            );
            
    MAPIMETHOD(ProcessMessage)(
            IMsgStore *pMsgStore, 
            ULONG cbMsg,
            LPENTRYID lpMsg,
            ULONG cbDestFolder,
            LPENTRYID lpDestFolder,
            ULONG *pulEventType,
            MRCHANDLED *pHandled
            );

private:
    long m_cRef;
};


// ************************************************************
// Class CFactory - Class factory for CMailRuleClient objects
//
// Inheritance:
//     IClassFactory IUnknown
//
// Purpose:
//     This class provides a standard COM class factory implementation
//	   for CMailRuleClient
//
// ************************************************************
class CFactory : public IClassFactory
{
public:
    CFactory();
    ~CFactory();
    
    // IUnknown
    STDMETHOD (QueryInterface)(REFIID iid, LPVOID *ppv);
    STDMETHOD_(ULONG, AddRef)();
    STDMETHOD_(ULONG, Release)();

    // IClassFactory interfaces
    STDMETHOD (CreateInstance)(IUnknown *pUnknownOuter, const IID& iid, LPVOID *ppv);
    STDMETHOD (LockServer)(BOOL bLock);

private:
    long m_cRef;
};

// **************************************************************************
// Function Name: CFactory 

// Purpose: Initializes CFactory object

// Arguments:

// Return Values:

// Side effects:  

// Description: 
//	Constructor for CFactory class. Initializes class members.
 
CFactory::CFactory()
{
    m_cRef = 1;
}

// **************************************************************************

// Function Name: ~CFactory
//
// Purpose: Cleans up CFactory object

// Arguments:

// Return Values:

// Side effects:  

// Description:  
//	Destructor for CFactory object

CFactory::~CFactory()
{
}

// **************************************************************************
// Function Name: QueryInterface
// Purpose: Obtains caller's desired interface pointer if it is supported

// Arguments:
//	IN IID& iid - Identifier for desired interface
//	OUT LPVOID *ppv - pointer to desired interface pointer

// Return Values: HRESULT 
//	E_NOINTERFACE:  the requested interface is not supported
//	E_INVALIDARG: bad reference for out param

// Side effects:  

// Description:
//	Standard implementation of COM IUnknown::QueryInterface
  
STDMETHODIMP CFactory::QueryInterface(const IID& iid, LPVOID *ppv)
{
    HRESULT hr = E_NOINTERFACE;

    if (!ppv)
    {
        return E_INVALIDARG;
    }
    
    if ((iid == IID_IUnknown) || (iid == IID_IClassFactory))
    {
        *ppv = (LPVOID) this;
    }
    else
    {
        *ppv = NULL;
    }

    if (*ppv) 
    {
        ((LPUNKNOWN) *ppv)->AddRef();
        hr = S_OK;
    }
    
    return hr;
}

// **************************************************************************
// Function Name: AddRef
// Purpose: COM reference counting

// Description:  
//	Implements IUnknown::Addref by adding 1 to the object's reference count
ULONG CFactory::AddRef()
{
    RETAILMSG(TRUE, (TEXT("Factory Reference is now %d\r\n"), m_cRef + 1));
    return InterlockedIncrement(&m_cRef);
}

// **************************************************************************
// Function Name: Release
// Purpose: COM reference counting

// Return Value: ULONG - reference count after decrementing 

// Description:  
//	Implements IUnknown::Release by subtracting 1 from the object's reference count
ULONG CFactory::Release()
{
    InterlockedDecrement(&m_cRef);
    RETAILMSG(TRUE, (TEXT("Factory Reference is now %d\r\n"), m_cRef));
    
    int nLocal = m_cRef;

    if (!m_cRef) 
    {
        RETAILMSG(TRUE, (TEXT("CFactory Deleted!\r\n")));
        delete this; 
    }

    return nLocal; 
}

// **************************************************************************
// Function Name: CreateInstance

// Purpose: Create a new instance of a COM object and return the specified
//	interface

// Arguments:	IN LPUNKNOWN pUnknownOuter - controlling outer for aggregation
//				IN REFIID iid - interface identifier GUID reference
//				OUT LPVOID *ppv - pointer to newly created interface pointer

// Return Values: HRESULT, S_OK if successful, error otherwise

// Side effects:  

// Description:  
STDMETHODIMP CFactory::CreateInstance(LPUNKNOWN pUnknownOuter, REFIID iid, LPVOID *ppv)
{
    CMailRuleClient *pClient = NULL;
    HRESULT hr;

    // No aggregation
    if (pUnknownOuter)
    {
        hr = CLASS_E_NOAGGREGATION;
        goto Error;

    }

    // You heard 'em, create a component
    pClient = new CMailRuleClient();
    if (!pClient)
    {
        hr = E_OUTOFMEMORY;
        goto Error;
    }
    
    // Get the requested interface
    hr = pClient->QueryInterface(iid, ppv);

Error:
    // Release the unknown pointer
    if (pClient) 
    {
        pClient->Release();
    }
    
    return hr;
}

// **************************************************************************
// Function Name: LockServer
// Purpose: Increment or decrement the number of lock on a COM server

// Arguments: IN BOOL bLock - increment(TRUE) or decrement(FALSE) the lockcount

// Return Values: HRESULT - S_OK

// Side effects:  

// Description:  
STDMETHODIMP CFactory::LockServer(BOOL bLock)
{
    if (bLock)
    {
        g_cServerLocks++;
    }
    else
    {
        g_cServerLocks--;
    }

    return S_OK;
}


// **************************************************************************
// Function Name: CMailRuleClient
//
// Purpose: Initialize the CMailRuleClient object

// Arguments:	none
	
// Return Values:	none

// Side effects:  

// Description: CMailRuleClient Constructor 
CMailRuleClient::CMailRuleClient()
{
    m_cRef = 1;
}

// **************************************************************************
// Function Name: ~CMailRuleClient
//
// Purpose: Cleans up CMailRuleClient object

// Arguments:

// Return Values:

// Side effects:  

// Description:  
//	Destructor for CMailRuleClient object
CMailRuleClient::~CMailRuleClient()
{
}

// **************************************************************************
// Function Name: QueryInterface
// Purpose: Obtains caller's desired interface pointer if it is supported

// Arguments:
//	IN IID& iid - Identifier for desired interface
//	OUT LPVOID *ppv - pointer to desired interface pointer

// Return Values: HRESULT 
//	E_NOINTERFACE:  the requested interface is not supported
//	E_INVALIDARG: bad reference for out param

// Side effects:  

// Description:
//	Standard implementation of COM IUnknown::QueryInterface

HRESULT CMailRuleClient::QueryInterface(REFIID rif, void** ppobj)
{	
    HRESULT hr = E_NOINTERFACE;

⌨️ 快捷键说明

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