www.pudn.com > CCmc_src.zip > CCmc.cpp


/* 
Module : CCMC.CPP 
Purpose: Defines the implementation for an MFC wrapper class for sending an email using CMC (Common Messaging Calls) 
Created: PJN / 11-05-1999 
History: None 
 
Copyright (c) 1999 by PJ Naughter.   
All rights reserved. 
 
 
*/ 
 
 
 
/////////////////////////////////  Includes  ////////////////////////////////// 
 
#include "stdafx.h" 
#include "CCmc.h" 
#include  
 
 
 
 
//////////////////////////////// Statics / Macros ///////////////////////////// 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char BASED_CODE THIS_FILE[] = __FILE__; 
#define new DEBUG_NEW 
#endif 
 
 
 
 
////////////////////////////////// Implementation ///////////////////////////// 
 
CCMCSession::CCMCSession() 
{ 
  m_hSession = 0; 
  m_nLastError = 0; 
  m_hCMC = NULL; 
  m_lpfncmc_logon = NULL; 
  m_lpfncmc_logoff = NULL; 
  m_lpfncmc_send = NULL; 
  m_lpfncmc_look_up = NULL; 
  m_lpfncmc_free = NULL; 
  Initialise(); 
} 
 
CCMCSession::~CCMCSession() 
{ 
  //Logoff if logged on 
  Logoff(); 
 
  //Unload the CMC dll 
  Deinitialise(); 
} 
 
void CCMCSession::Initialise()  
{ 
  //First make sure the "WIN.INI" entry for CMC is present 
	BOOL bCMCInstalled = (::GetProfileInt(_T("MAIL"), _T("CMC"), 0) != 0); 
 
  //Determine the dll which houses CMC 
  TCHAR szDllName[_MAX_PATH]; 
  if (!::GetProfileString(_T("Mail"), _T("CMCDLLNAME32"), _T(""), szDllName, _MAX_PATH)) 
    _tcscpy(szDllName, _T("CMC.DLL")); 
 
  //Check to see can we find the dll 
  bCMCInstalled = (::SearchPath(NULL, szDllName, NULL, 0, NULL, NULL) != 0); 
     
  if (bCMCInstalled) 
  { 
    //Load up the CMC dll and get the function pointers we are interested in 
    m_hCMC = LoadLibrary(szDllName); 
    if (m_hCMC) 
    { 
      m_lpfncmc_logon   = (LPCMC_LOGON) GetProcAddress(m_hCMC, "cmc_logon"); 
      m_lpfncmc_logoff  = (LPCMC_LOGOFF) GetProcAddress(m_hCMC, "cmc_logoff"); 
      m_lpfncmc_send    = (LPCMC_SEND) GetProcAddress(m_hCMC, "cmc_send"); 
      m_lpfncmc_look_up = (LPCMC_LOOK_UP) GetProcAddress(m_hCMC, "cmc_look_up"); 
      m_lpfncmc_free    = (LPCMC_FREE) GetProcAddress(m_hCMC, "cmc_free"); 
   
      //If any of the functions are not installed then fail the load 
      if (m_lpfncmc_logon == NULL || 
          m_lpfncmc_logoff == NULL || 
          m_lpfncmc_send == NULL || 
          m_lpfncmc_look_up == NULL || 
          m_lpfncmc_free == NULL) 
      { 
        TRACE(_T("Failed to get one of the CMC functions pointers in %s\n"), szDllName); 
        Deinitialise(); 
      } 
    } 
  } 
  else 
    TRACE(_T("CMC is not installed on this computer\n")); 
} 
 
void CCMCSession::Deinitialise() 
{ 
  if (m_hCMC) 
  { 
    //Unload the CMC dll and reset the function pointers to NULL 
    FreeLibrary(m_hCMC); 
    m_hCMC = NULL; 
    m_lpfncmc_logon = NULL; 
    m_lpfncmc_logoff = NULL; 
    m_lpfncmc_send = NULL; 
    m_lpfncmc_look_up = NULL; 
    m_lpfncmc_free = NULL; 
  } 
} 
 
BOOL CCMCSession::Logon(const CString& sProfileName, const CString& sPassword, CWnd* pParentWnd) 
{ 
	//For correct operation of the T2A macro, see MFC Tech Note 59 
	USES_CONVERSION; 
 
  ASSERT(CMCInstalled());  //CMC must be installed 
  ASSERT(m_lpfncmc_logon); //Function pointer must be valid 
 
  //Initialise the function return value 
  BOOL bSuccess = FALSE; 
 
  //Just in case we are already logged in 
  Logoff(); 
 
  //Setup the ascii versions of the profile name and password 
  int nProfileLength = sProfileName.GetLength(); 
  int nPasswordLength = sPassword.GetLength(); 
  LPSTR pszProfileName = NULL; 
  LPSTR pszPassword = NULL; 
  if (nProfileLength) 
  { 
    pszProfileName = T2A((LPTSTR) (LPCTSTR) sProfileName); 
    pszPassword = T2A((LPTSTR) (LPCTSTR) sPassword); 
  } 
 
  //Setup the cmc_ui_id & CMC_flags parameters used in the cmc_logon call 
  CMC_ui_id ui_id = 0; 
  CMC_flags logon_flags = 0; 
  if (nProfileLength == 0) 
  { 
    //No profile name given, then we must interactively request a profile name 
    logon_flags = CMC_ERROR_UI_ALLOWED | CMC_LOGON_UI_ALLOWED; 
    if (pParentWnd) 
      ui_id = (CMC_ui_id) pParentWnd->GetSafeHwnd(); 
    else 
    { 
      //No CWnd given, just use the main window of the app as the parent window 
      ASSERT(AfxGetMainWnd()); 
      ui_id = (CMC_ui_id) AfxGetMainWnd()->GetSafeHwnd(); 
    } 
  } 
 
  //try to Logon to CMC 
  CMC_return_code nError = m_lpfncmc_logon(NULL, (CMC_string) pszProfileName, (CMC_string) pszPassword,  
                                           NULL, ui_id, CMC_VERSION, logon_flags, &m_hSession, NULL); 
  if (nError != CMC_SUCCESS) 
  { 
    m_nLastError = nError; 
    bSuccess = FALSE; 
  } 
  else 
  { 
    m_nLastError = CMC_SUCCESS; 
    bSuccess = TRUE; 
  } 
 
  return bSuccess; 
} 
 
BOOL CCMCSession::LoggedOn() const 
{ 
  return (m_hSession != 0); 
} 
 
BOOL CCMCSession::CMCInstalled() const 
{ 
  return (m_hCMC != NULL); 
} 
 
BOOL CCMCSession::Logoff() 
{ 
  ASSERT(CMCInstalled());   //CMC must be installed 
  ASSERT(m_lpfncmc_logoff); //Function pointer must be valid 
 
  //Initialise the function return value 
  BOOL bSuccess = FALSE; 
 
  if (m_hSession) 
  { 
    //Call the cmc_logoff function 
    CMC_return_code nError = m_lpfncmc_logoff(m_hSession, 0, 0, NULL);  
    if (nError != CMC_SUCCESS) 
    { 
      TRACE(_T("Failed in call to cmc_logoff, Error:%d"), nError); 
      m_nLastError = nError; 
      bSuccess = TRUE; 
    } 
    else 
    { 
      m_nLastError = CMC_SUCCESS; 
      bSuccess = TRUE; 
    } 
    m_hSession = 0; 
  } 
     
  return bSuccess; 
} 
 
BOOL CCMCSession::Send(CCMCMessage& message) 
{ 
	//For correct operation of the T2A macro, see MFC Tech Note 59 
	USES_CONVERSION; 
 
  ASSERT(CMCInstalled()); //CMC must be installed 
  ASSERT(m_lpfncmc_send); //Function pointer must be valid 
  ASSERT(m_lpfncmc_free); //Function pointer must be valid 
  ASSERT(LoggedOn());     //Must be logged on to CMC 
  ASSERT(m_hSession);     //CMC session handle must be valid 
 
  //Initialise the function return value 
  BOOL bSuccess = FALSE;   
 
  //Create the CMC_message structure to match the message parameter send into us 
  CMC_message cmcMessage; 
  ZeroMemory(&cmcMessage, sizeof(cmcMessage)); 
  cmcMessage.subject = (CMC_string) (T2A((LPTSTR) (LPCTSTR) message.m_sSubject)); 
  cmcMessage.text_note = (CMC_string) (T2A((LPTSTR) (LPCTSTR) message.m_sBody)); 
 
  //Allocate the recipients array 
  int nRecipients = message.m_To.GetSize() + message.m_CC.GetSize() + message.m_BCC.GetSize(); 
  ASSERT(nRecipients); //Must have at least 1 recipient! 
  cmcMessage.recipients = new CMC_recipient[nRecipients]; 
 
  //Setup the "To" recipients 
  int nRecipIndex = 0; 
  int nToSize = message.m_To.GetSize(); 
  for (int i=0; i