Share via


Create an IM Session

The following code example illustrates how to create an IM session and send an IM message. The operations in the Initialize RTC code example must be performed before using this example.

Note  This example does not contain error checking or releases appropriate for real code.

C++ Code Example

IRTCSession     *pIRTCSession        = NULL;
IRTCParticipant *pIRTCParty          = NULL;
BSTR            bstrDestURI          = SysAllocString(_T("someone@example.com"));
// If ( bstrDestURI == NULL ) process the error here.   
BSTR            bstrDestName         = SysAllocString(_T("Jeff Smith"));
// If ( bstrDestName == NULL ) process the error here.   
BSTR            bstrMsgHeader        = // specify message header;
BSTR            bstrMsg              = // specify message ;
LONG            lCookie              = // set cookie value ;

// When you create the session, you can specify a profile if required.
// In this code example, no profile is specified.
hr = pIRTCClient->CreateSession( RTCST_IM, 
                                 NULL,
                                 NULL, 
                                 0,
                                 &pIRTCSession );

// If (hr != S_OK), process the error here. 

// Add an IM session participant.
hr = pIRTCSession->AddParticipant( bstrDestURI,
                                   bstrDestName,
                                   m_pIRTCParty );

// If (hr != S_OK), process the error here. 

// Send the initial message upon which participant [end-point]
// will receive the RTCE_SESSION_STATE_CHANGE incoming event
// from which the end-point needs to retrieve its session pointer.
// The session initiator will be in the connected state after the 
// end-point successfully receives the initial message.

hr = pIRTCSession->SendMessage( bstrMsgHeader,
                                bstrMsg,
                                lCookie );

// If (hr != S_OK), process the error here. 

Visual Basic Code Example

'Set the error handling routine here. 
' On Error GoTo MyErrorRoutine 

'Usually declared globally
Private g_objSession As IRTCSession
Private g_objParticipant As IRTCParticipant

Private strDestURI      As String   '(for example, someone@example.com)
Private strDestName     As String   '(for example, Jeff Smith)
Private strMsgHeader    As String   '(specify message header)
Private strMsg          As String   '(specify message)
Private lCookie         As Long     '(Application-provided cookie used for pairing the notifications with the messages sent)

'Create an IM session.
Set g_objSession = g_objRTCClient.CreateSession(RTCST_IM, vbNullString, Nothing, 0)

'Add a participant to the IM session.
Set g_objParticipant = g_objSession.AddParticipant(strDestURI, strDestName)

'Send a message.
Call g_objSession.SendMessage(strMsgHeader, strMsg, lCookie)