Create a Session for a Specific Device

The following code example shows how to create a session with a buddy on a specific device whose presence status is set to AWAY. The operations in the Initialize RTC and Create a Session and Make a Call code examples must be performed before using this example.

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

C++ Code Example

//
// Creating an multipoint IM session to a particular device for a buddy.

HRESULT hr = S_OK;
BSTR bstrDeviceID = NULL;
RTC_PRESENCE_STATUS pStatus;

// Get the IRTCEnumPresenceDevices interface.
IRTCEnumPresenceDevices* ppEnumDevices = NULL;
hr = pBuddy->EnumeratePresenceDevices(&ppEnumDevices);
// If (hr != S_OK), process the error here.
  
ULONG celtFetched = 0;
IRTCPresenceDevice *pDevice = NULL;
IRTCPresenceDevice2 *pDevice2 = NULL;

// Get all the presence devices for this buddy.
for (; (hr = ppEnumDevices->Next(1, &pDevice, &celtFetched)) == S_OK;)
{
    // Query for the IRTCPresenceDevice2 interface.
    hr = pDevice->QueryInterface(IID_IRTCPresenceDevice2,
         (LPVOID **) &pDevice2);
    // If (hr != S_OK), process the error here.

    hr = pDevice2->get_Status(&pStatus);
    // If (hr != S_OK), process the error here.

 

    // Get the ID for a device that is AWAY.
    if (pStatus == RTCXS_PRESENCE_AWAY)
    {
        hr = pDevice2->get_DeviceID(&bstrDeviceID);
        // If (hr != S_OK), process the error here.
    }

    if (pDevice)
        pDevice->Release();

    if (pDevice2)
        pDevice2->Release();
}

if (ppEnumDevices)
    ppEnumDevices->Release();

IRTCSession  *pIRTCSession = NULL;
IRTCSession3  *pIRTCSession3 = NULL;
IRTCParticipant *pIRTCParty  = NULL;
BSTR   bstrDestURI  = SysAllocString(L"someone@microsoft.com");
BSTR   bstrDestName = SysAllocString(L"John");

// When you create the session, you can specify a profile 
// if one is required. In this code example, no profile is 
// specified. The RTC Client API will choose the best profile.
hr = pIRTCClient->CreateSession(RTCST_MULTIPARTY_IM, 
                                NULL,
                                NULL, 
                                0,
                                &pIRTCSession);
// If (hr != S_OK), process the error here. 

 

// Query for the IRTCSession3 interface.
hr = pIRTCSession->QueryInterface(IID_IRTCSession3),
                                  (void **) &pIRTCSession3);

 

// Add the participant.
hr = pIRTCSession3->AddParticipantEx(bstrDestURI,
                                  bstrDestName,
                                  bstrDeviceID,
                                  &pIRTCParty);
// If (hr != S_OK), process the error here. 

 

// Wait for the RTCE_SESSION_STATE_CHANGE event of  
// type RTCSS_CONNECTED before attempting to perform 
// operations on the session. 


 

Visual Basic Code Example

Dim i As Integer
Dim strDeviceID As String
Dim strDestinationURI As String
Dim objBuddy As IRTCBuddy2
Dim objSession As IRTCSession
Dim objSession3 As IRTCSession3
Dim objCollection As IRTCCollection
Dim objPDevice As IRTCPresenceDevice2
Dim objParticipant2 As IRTCParticipant2

' Set the error handling routine here.
' On Error GoTo MyErrorRoutine
       
'
' Get the DeviceID of a buddy.

' Enumerate all presence devices of a buddy
Set objCollection = objBuddy.PresenceDevices

For i = 1 To objCollection.Count
    Set objPDevice = objCollection.Item(i)
    
    If (objPDevice.Status = RTCXS_PRESENCE_AWAY) Then
        strDeviceID = objPDevice.DeviceID ' storing the Device ID to make a call to it later.
    End If
    
    Set objPDevice = Nothing
Next i


'
' Create a session to intended endpoint
'

Set objSession = g_objRTCClient2.CreateSession(RTCST_MULTIPARTY_IM, vbNullString, Nothing, 0)
 

' Querying for IRTCSession3 from IRTCSession
Set objSession3 = objSession


' Adding a participant with the desired DeviceID
Set objParticipant2 = objSession3.AddParticipantEx(strDestinationURI, "ToDestination", strDeviceID)