All pastes #900186 Raw Edit

Unnamed

public text v1 · immutable
·published 2008-02-11 18:50 UTC
rendered paste body
// CreateProfileWithIProfAdmin function: This uses the MAPI IProfAdmin to 
// programmatically create a profile. No UI is displayed.
BOOL CMAPIEx::CreateMessageStore(LPTSTR szPSTPath, LPTSTR szProfileName, LPTSTR szDisplayName)
{
    HRESULT         hRes = S_OK;            // Result from MAPI calls.
    LPPROFADMIN     lpProfAdmin = NULL;     // Profile Admin object.
    LPSERVICEADMIN  lpSvcAdmin = NULL;      // Service Admin object.
    LPMAPITABLE     lpMsgSvcTable = NULL;   // Table to hold services.
    LPSRowSet       lpSvcRows = NULL;       // Rowset to hold results of table query.
    SPropValue      rgval[2];               // Property structure to hold values we want to set.
    SRestriction    sres;                   // Restriction structure.
    SPropValue      SvcProps;               // Property structure for restriction.

    // This indicates columns we want returned from HrQueryAllRows.
    enum {iSvcName, iSvcUID, cptaSvc};
    SizedSPropTagArray(cptaSvc,sptCols) = { cptaSvc, PR_SERVICE_NAME, PR_SERVICE_UID };

    // Get an IProfAdmin interface.
    if (FAILED(hRes = MAPIAdminProfiles(0, &lpProfAdmin))) // Pointer to new IProfAdmin.
    {
        goto error;
    }

	// Create a new profile.
    /*if (FAILED(hRes = lpProfAdmin->CreateProfile(szProfileName,     // Name of new profile.
                                                 NULL,          // Password for profile.
                                                 0,          // Handle to parent window.
                                                 0)))        // Flags.
    {
        //goto error;
    }*/

    // Get an IMsgServiceAdmin interface off of the IProfAdmin interface.
	// Assumption: the ProfileName already exits
    if (FAILED(hRes = lpProfAdmin->AdminServices(szProfileName,     // Profile that we want to modify.
                                                 NULL,          // Password for that profile.
                                                 NULL,          // Handle to parent window.
                                                 0,             // Flags.
                                                 &lpSvcAdmin))) // Pointer to new IMsgServiceAdmin.
    {
        goto error;
    }

    // Create the new message service for PST.
    if (FAILED(hRes = lpSvcAdmin->CreateMsgService("MSPST MS",     // Name of service.
                                                   NULL,        // Display name of service.
                                                   NULL,        // Handle to parent window.
                                                   NULL)))      // Flags.
    {
        goto error;
    }
        
    // You now have to obtain the entry id for the new service.
    // You can do this by getting the message service table
    // and getting the entry that corresponds to the new service.

    if (FAILED(hRes = lpSvcAdmin->GetMsgServiceTable(0,                 // Flags.
                                                     &lpMsgSvcTable)))  // Pointer to table.
    {
        goto error;
    }

    // Set up restriction to query table.
    sres.rt = RES_CONTENT;
    sres.res.resContent.ulFuzzyLevel = FL_FULLSTRING;
    sres.res.resContent.ulPropTag = PR_SERVICE_NAME;
    sres.res.resContent.lpProp = &SvcProps;

    SvcProps.ulPropTag = PR_SERVICE_NAME;
    SvcProps.Value.lpszA = "MSPST MS";

    // Query the table to obtain the entry for the newly created message service.

    if (FAILED(hRes = HrQueryAllRows(lpMsgSvcTable,
                                     (LPSPropTagArray)&sptCols,
                                     &sres,
                                     NULL,
                                     0,
                                     &lpSvcRows)))
    {
        goto error;
    }

    // Set up a SPropValue array for the properties that you have to configure.

    // First, display name
    ZeroMemory(&rgval[1], sizeof(SPropValue) );
    rgval[1].ulPropTag = PR_DISPLAY_NAME;
    rgval[1].Value.lpszA = szDisplayName;

    // Next, the PST Path
    ZeroMemory(&rgval[0], sizeof(SPropValue) );
    rgval[0].ulPropTag = PR_PST_PATH; 
    rgval[0].Value.lpszA = szPSTPath;

    // Configure the message service by using the previous properties.

        if (FAILED(hRes = lpSvcAdmin->ConfigureMsgService(
        (LPMAPIUID)lpSvcRows->aRow->lpProps[iSvcUID].Value.bin.lpb, // Entry ID of service to configure.
        NULL,                                                       // Handle to parent window.
        0,                                                          // Flags.
        2,                                                          // Number of properties we are setting.
        rgval)))                                                    // Pointer to SPropValue array.
    {
        goto error;
    }

    goto cleanup;

error:
    return FALSE;

cleanup:
    // Clean up.
    if (lpSvcRows) FreeProws(lpSvcRows);
    if (lpMsgSvcTable) lpMsgSvcTable->Release();
    if (lpSvcAdmin) lpSvcAdmin->Release();
    if (lpProfAdmin) lpProfAdmin->Release();

    return TRUE;
}