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

📄 mapirule.cpp

📁 采用cemapi库
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    if (!ppobj)
    {
        return E_INVALIDARG;
    }
    
	*ppobj = NULL;
	if ((rif == IID_IUnknown) || (rif == IID_IMailRuleClient))
	{
	 	*ppobj = (LPVOID) this;
 	}

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

// **************************************************************************
// Function Name: AddRef

// Purpose: COM reference counting

// Arguments: none

// Return Values: current ref count (after adding)

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

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

// Arguments: none

// Return Values: current ref count (after subracting)

// Side effects:  

// Description:
//	Implements IUnknown::Addref by subtracting 1 from the object's reference count  

ULONG CMailRuleClient::Release()
{
    InterlockedDecrement(&m_cRef);
    RETAILMSG(TRUE, (TEXT("CMailRuleClient reference is now %d\r\n"), m_cRef));
    
    int nLocal = m_cRef;

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

    return nLocal; 
}

// **************************************************************************
// Function Name: Initialize
//
// Purpose: determines how the mail rule client will process incoming messages.

// Arguments: IN IMsgStore * pMsgStore - represests message store which contains
//	the incoming messages
//			  OUT MRCACCESS *pmaDesired - desired message store access level	

// Return Values: HRESULT - S_OK

// Side effects:  

// Description:  
//	This function is called by the system to initialize rule clients. Since we
//	eventually may want to delete messages, we request write access here
HRESULT CMailRuleClient::Initialize(IMsgStore *pMsgStore, MRCACCESS *pmaDesired)
{
  
    *pmaDesired = MRC_ACCESS_WRITE;
    return S_OK;
}

// **************************************************************************
// Function Name: ProcessMessage
//
// Purpose: process incoming messages, which can be moved, modified, or deleted 

// Arguments: IN IMsgStore * pMsgStore - represests message store which contains
//				the incoming messages
//			  IN ULONG cbMsg - The size of lpMsg in bytes
//			  IN LPENTRYID - The ENTRYID of the message
//			  IN ULONG cbDestFolder - The size of lpDestFolder in bytes
//			  IN LPENTRYID lpDestFolder - The ENTRYID of the the folder that 
//				incoming messages are moved to.
//			  OUT ULONG * pulEventType - bit flag that indicates the type of 
//				action the client performed on the message 
//			  OUT MRCHANDLED * pHandled - The type of message handling that 
//				occured during the processing
//			  

// Return Values: HRESULT 
//	This method returns S_OK if the processing was successful, and appropriate 
//	errors if not. 

// Side effects:  

// Description: 
//	This function is called by the system when an incoming message is received
//  This is where all the plugin-defined processing happens. You can filter 
//	messages based on content, delete or move messages, and report whether the
//  message has been handled or not.

HRESULT CMailRuleClient::ProcessMessage(IMsgStore *pMsgStore, ULONG cbMsg, LPENTRYID lpMsg, 
            ULONG cbDestFolder, LPENTRYID lpDestFolder, ULONG *pulEventType, MRCHANDLED *pHandled)
{
    HRESULT hr = S_OK;
    SizedSPropTagArray(1, sptaSubject) = { 1, PR_SUBJECT}; 
	SizedSPropTagArray(1, sptaEmail) = { 1, PR_SENDER_EMAIL_ADDRESS}; 
    ULONG cValues = 0;
    SPropValue *pspvSubject = NULL;
	SPropValue *pspvEmail = NULL;
    IMessage *pMsg = NULL;
    HRESULT hrRet = S_OK;

    // Get the message from the entry ID
    hr = pMsgStore->OpenEntry(cbMsg, lpMsg, NULL, 0, NULL, (LPUNKNOWN *) &pMsg);
    if (FAILED(hr))
    {
		RETAILMSG(TRUE, (TEXT("Unable to get the message!\r\n")));
		goto Exit;
    }
    
    // For SMS, the subject is also the message body
    hr = pMsg->GetProps((SPropTagArray *) &sptaSubject, MAPI_UNICODE, &cValues, &pspvSubject);
	if (FAILED(hr))
    {
        
		RETAILMSG(TRUE, (TEXT("Unable to get the message body!\r\n")));
        goto Exit;
    }
	// get the sender's address or phone number
	hr = pMsg->GetProps((SPropTagArray *) &sptaEmail, MAPI_UNICODE, &cValues, &pspvEmail);

    if (FAILED(hr))
    {
        RETAILMSG(TRUE, (TEXT("Couldn't get the sender's address!\r\n")));
        goto Exit;
    }
	
	// Here we filter the message on some predetermined string. For sample purposes
	// here we use "zzz". What happens when the filter condition(s) are met is up to
	// you. You can send WM_COPYDATA messages to other app windows for light IPC, send
	// an SMS message in response, or whatever you need to do. Here, we just play a
	// sound and show the message in a standard message box.
	if (wcsstr(pspvSubject->Value.lpszW, L"zzz") != NULL)

	{
		MessageBeep(MB_ICONASTERISK);
		MessageBox(NULL, pspvSubject->Value.lpszW, pspvEmail->Value.lpszW, MB_OK);
		
		// Delete the message and mark it as handled so it won't show up in Inbox
		hr = DeleteMessage(pMsgStore, pMsg, cbMsg, lpMsg, cbDestFolder, lpDestFolder, pulEventType, pHandled);
	}
	else 
	{
		// a 'normal' message, pass it on
		*pHandled = MRC_NOT_HANDLED;	
	}

// Clean up
Exit:
    if (pspvEmail)
    {
        MAPIFreeBuffer(pspvEmail);
    }
	if (pspvSubject)
    {
        MAPIFreeBuffer(pspvSubject);
    }
    if (pMsg)
    {
        pMsg->Release();
    }
    

    return hr;
}

// **************************************************************************
// Function Name: DllMain
// Purpose: DLL Entry point

// Arguments: IN HANDLE hinst - Handle to the DLL
//			  IN DWORD dwReason - flag indicating why entry-point was called
//			  IN LPVOID lpv - specifies further aspects of initialization and
//				cleanup 

// Return Values: TRUE if initialization succeeds, FALSE otherwise

// Side effects:  

// Description: 
//	Called by system when a thread or process loads/unloads the dll 
BOOL WINAPI DllMain(HANDLE hinst, DWORD dwReason, LPVOID lpv)
{
   

	switch (dwReason)
    {
        case DLL_PROCESS_ATTACH:
            break;

        case DLL_PROCESS_DETACH:
            break;

        default:
            break;
    }

    return TRUE;
}

// **************************************************************************
// Function Name: DllGetClassObject
// Purpose: Retrieves the class object from the DLL object

// Arguments: IN CLSID& clsid - CLSID for desired class object
//			  IN REFIID iid - ref to interface id, usually IClassFactory
//			  OUT LPVOID *ppv - address of requested interface pointer

// Return Values: HRESULT

// Side effects:  

// Description:  
STDAPI DllGetClassObject(const CLSID& clsid, REFIID iid, LPVOID *ppv)
{
    HRESULT hr;
    
    // We only understand this rule's class
    if (clsid != CLSID_MapiRuleSample)
    {
        return CLASS_E_CLASSNOTAVAILABLE;
    }

    CFactory *pFactory = new CFactory;
    if (!pFactory)
    {
        return E_OUTOFMEMORY;
    }

    // Get the requested interface
    hr = pFactory->QueryInterface(iid, ppv);
    pFactory->Release();
    
    return hr;
}

// **************************************************************************
// Function Name: DllCanUnloadNow
//
// Purpose: Notify caller if dll can safely be unloaded

// Arguments: none

// Return Values: HRESULT, S_OK if safe to unload, S_FALSE otherwise

// Side effects:  

// Description:  A call to DllCanUnloadNow determines whether the DLL from 
//	which it is exported is still in use. A DLL is no longer in use when it 
//	is not managing any existing objects (the reference count on all of its 
//	objects is zero). 

STDAPI DllCanUnloadNow()
{
    if (!g_cServerLocks)
    {
     
        return S_OK;
    }
    else
    {
        return S_FALSE;
    }
}

// **************************************************************************
// Function Name: DllRegisterServer
//
// Purpose: provide DLL with the ability to register its COM objects

// Arguments: none

// Return Values: HRESULT - S_OK if registration succeeds, E_FAIL otherwise

// Side effects: In order to fully remove the plugin, both registry keys should
//	be removed (see Description below). DllUnregisterServer does this.

// Description:  
//  In addition to standard COM object registration, the function also must
//  register our rule client handler with Inbox.  We are registering our 
//	DLL in two places, HKEY_CLASSES_ROOT\CLSID\, and HKEY_LOCAL_MACHINE\Software
//	\Microsoft\Inbox\Svc\SMS\Rules
STDAPI DllRegisterServer()
{
    LRESULT lr;
    HRESULT hr = E_FAIL;
    HKEY hKey = NULL;
    HKEY hSubKey = NULL;
    DWORD dwDisposition;
    TCHAR wszValue[20];

    // Set up registry keys
    // Register with COM:
    //    [HKEY_CLASSES_ROOT\CLSID\{3AB4C10E-673C-494c-98A2-CC2E91A48115}\InProcServer32]
    //    @="mapirule.dll"
	

    lr = RegCreateKeyEx(HKEY_CLASSES_ROOT, TEXT("\\CLSID\\{3AB4C10E-673C-494c-98A2-CC2E91A48115}"),
	                              0, NULL, 0, 0, NULL, 
	                              &hKey, &dwDisposition);
    if (lr != ERROR_SUCCESS)
    {
        goto Exit;
    }

    lr = RegCreateKeyEx(hKey, TEXT("InprocServer32"),
	                              0, NULL, 0, 0, NULL, 
	                              &hSubKey, &dwDisposition);
    if (lr != ERROR_SUCCESS)
    {
        goto Exit;
    }

    lstrcpy(wszValue, TEXT("mapirule.dll"));
    lr = RegSetValueEx(hSubKey, NULL, 0, REG_SZ, (LPBYTE) wszValue, (lstrlen(wszValue) + 1) * sizeof(TCHAR));
    if (lr != ERROR_SUCCESS)
    {
        goto Exit;
    }

    RegCloseKey(hSubKey);
    hSubKey = NULL;
    RegCloseKey(hKey);
    hKey = NULL;

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

    lr = RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("\\Software\\Microsoft\\Inbox\\Svc\\SMS\\Rules"),
	                              0, NULL, 0, 0, NULL, 
	                              &hKey, &dwDisposition);
    if (lr != ERROR_SUCCESS)
    {
        goto Exit;
    }

    dwDisposition = 1;
    lr = RegSetValueEx(hKey, TEXT("{3AB4C10E-673C-494c-98A2-CC2E91A48115}"), 0, REG_DWORD, 
                          (LPBYTE) &dwDisposition, sizeof(DWORD));
    if (lr != ERROR_SUCCESS)
    {
        goto Exit;
    }
 
    hr = S_OK;

Exit:
    if (hSubKey)
    {
        RegCloseKey(hSubKey);
    }
    if (hKey)
    {
        RegCloseKey(hKey);
    }

    return hr;
}

// **************************************************************************
// Function Name: DllUnregisterServer
// Purpose: rovide DLL with the ability to un-register its COM objects


// Arguments: none

// Return Values: HRESULT - S_OK if registration succeeds, E_FAIL otherwise

// Side effects: 

// Description:  Deletes both the COM registry key and the key used to register
// the rule client with Inbox
STDAPI DllUnregisterServer()
{
    HKEY hKey = NULL;
    HRESULT hr = E_FAIL;
    LRESULT lr;
    DWORD dwDisposition;

    // Delete registry keys
    RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("\\CLSID\\{3AB4C10E-673C-494c-98A2-CC2E91A48115}"));
    
    lr = RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("\\Software\\Microsoft\\Inbox\\Svc\\SMS\\Rules"),
	                              0, NULL, 0, 0, NULL, 
	                              &hKey, &dwDisposition);
    if (lr != ERROR_SUCCESS)
    {
        goto Exit;
    }

    RegDeleteValue(hKey, TEXT("{3AB4C10E-673C-494c-98A2-CC2E91A48115}"));

    hr = S_OK;

Exit:
    if (hKey)
    {
        RegCloseKey(hKey);
    }

    return hr;
}

⌨️ 快捷键说明

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