I've written a .dll that can be used to send and receive mail through the
GroupWise Object API to a GroupWise server.

This .dll is used by an existing Service, and it ends up that the Service
calls a Disconnect and Connect function after 15 minutes of inactivity. For
instance,

1.Service starts. Calls Connect function.
2. Check for new mail and process all send/receive requests every 1 minute.
3. After 15 minutes of inactivity, call disconnect function.
4. 15 minutes later: Call Connect function and check for new mail, etc.

Problem: After 1 or 2 reconnects/disconnects, it crashes/cores out.

I'm going to work on pinpointing the problem, it likely is my own error, but
I wanted to verify that I'm creating/destroying all of the Object API
objects correctly. Here is my disconnect code that destorys the objects
(which I really suspect as the problem), followed by my connect code (which
I suspect less, since it is patterned after the samples.)

One thing I'm thinking off the bat is that I don't need to destroy/re-create
the Session object...

int CGroupWiseInterface::DisconnectFromMailSrv()
{
try
{
// Release messages object
if (m_pIGWMessages)
m_pIGWMessages->Release();
if (m_pIGWMessage)
m_pIGWMessage->Release();
if (m_pDIGWMessage)
m_pDIGWMessage->Release();
if(m_pIGWMessagesList)
m_pIGWMessagesList->Release();
if (m_pIGWAccount)
m_pIGWAccount->Release();
if (m_pIGWSession)
m_pIGWSession->Release();

m_pIGWMessages = NULL;
m_pIGWMessage = NULL;
m_pDIGWMessage = NULL;
m_pIGWMessagesList = NULL;
m_pIGWAccount = NULL;
m_pIGWSession = NULL;
}
catch(...)
{
m_pIGWMessages = NULL;
m_pIGWMessage = NULL;
m_pDIGWMessage = NULL;
m_pIGWAccount = NULL;
m_pIGWSession = NULL;
m_pIGWMessagesList = NULL;
}

return 1;
}

BOOL CGroupWiseInterface::CreateSession(CString csUserID, CString
csServerName, CString csPort,
CString csTrustAppName, CString csTrustAppKey)
{
DIGWAccount* pDIGWAccount = NULL; BOOL toReturn;
VARIANT vUserId, vCmdLine, vPassword, vWhenToPrompt, vReserved;
BSTR bstrUserID = NULL; BSTR bstrCmdLine = NULL;

try
{
// Create the Session Object
if(FAILED(CoCreateInstance(CLSID_GroupWare, NULL, CLSCTX_INPROC_SERVER |
CLSCTX_INPROC_HANDLER | CLSCTX_LOCAL_SERVER, IID_IGWSession4,
(void**)&m_pIGWSession))) {
ErrorMessage(_T("Could not create GroupWise session object. Please
confirm that Groupwise is installed on this computer."));
return FALSE;
}

if(!SUCCEEDED(m_pIGWSession->SetTrustedApplicationCredentials(csTrustAppName.A llocSysString(),
csTrustAppKey.AllocSysString())))
{
ErrorMessage(_T("Error starting GroupWise Session: Couldn't establish
Trusted Application Credentials. Check Trusted App Name and Trusted App
Key."));
return FALSE;
}

//Calculate the CmdLine using this example: "/ipa-199.99.99.99 /ipp-1677"
CString csCmdLine = _T("");
csCmdLine.Format("/ipa-%s /ipp-%s", csServerName, csPort);

bstrUserID = csUserID.AllocSysString();
bstrCmdLine = csCmdLine.AllocSysString();

VariantInit(&vUserId);
VariantInit(&vCmdLine);
VariantInit(&vPassword);
VariantInit(&vWhenToPrompt);
VariantInit(&vReserved);

V_VT(&vUserId) = VT_BSTR;
V_VT(&vCmdLine) = VT_BSTR;
V_VT(&vPassword) = VT_EMPTY;
V_VT(&vWhenToPrompt) = VT_I2;
V_VT(&vReserved) = VT_EMPTY;

V_I2(&vWhenToPrompt) = egwNeverPrompt;
V_BSTR(&vUserId) = bstrUserID;
V_BSTR(&vCmdLine) = bstrCmdLine;

// Login
if(!SUCCEEDED(m_pIGWSession->MultiLogin(bstrUserID, vCmdLine, vPassword,
vWhenToPrompt, vReserved, &pDIGWAccount))) {
CString csErrMsg = _T("");
csErrMsg.Format("Could not login to GroupWise with userid: %s and
command: %s", csUserID, csCmdLine);
ErrorMessage(csErrMsg);
toReturn = FALSE;
}
else
{
// Get the Root Account's Interface
if(FAILED(pDIGWAccount->QueryInterface(IID_IGWAccount3,
(LPVOID*)&m_pIGWAccount)))
{
ErrorMessage(_T("Error starting GroupWise Session: Couldn't obtain
Account from Dispatch Interface"));
toReturn = FALSE;
}
else
{
pDIGWAccount->Release();
toReturn = TRUE;
}
}
}
catch(CException* exp)
{
CString csErrMsg = _T("");
csErrMsg.Format("Error connecting. User: %s Server:%s Port:%s Error:",
csUserID, csServerName, csPort);
HandleException(csErrMsg, exp);
toReturn = FALSE;
}
catch(...)
{
ErrorMessage(_T("Uknown Exception ocurred while sending mail."));
toReturn = FALSE;
}
return toReturn;

}