这个问题曾经折磨了我好几天,最终放弃了。
最后在其他同事的不懈努力下最终解决:

extern "C" __declspec(dllexport) bool CreateProfileWithIProfAdmin( LPTSTR szProfile,LPTSTR szMailbox,LPTSTR szServer)
{
    HRESULT         hr = 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 };

    try
    {
        // Initialize MAPI.
        if (FAILED(hr = MAPIInitialize(NULL)))
        {
            outputResult("MAPIInitialize",hr);
            throw -1;
        }

        if(DoesProfileExist(szProfile))
        {
            cout<<"Profile Existed"<<endl;
            return TRUE;
        }
        // Get an IProfAdmin interface.

        if (FAILED(hr = MAPIAdminProfiles(0,              // Flags.
            &lpProfAdmin))) // Pointer to new IProfAdmin.
        {
            outputResult("MAPIAdminProfiles",hr);
            throw -1;
        }

        // Create a new profile.

        if (FAILED(hr = lpProfAdmin->CreateProfile(szProfile,     // Name of new profile.
            NULL,          // Password for profile.
            NULL,          // Handle to parent window.
            NULL)))        // Flags.
        {
            outputResult("CreateProfile",hr);
            throw -1;
        }

        // Get an IMsgServiceAdmin interface off of the IProfAdmin interface.

        if (FAILED(hr = lpProfAdmin->AdminServices(szProfile,     // Profile that we want to modify.
            NULL,          // Password for that profile.
            NULL,          // Handle to parent window.
            0,             // Flags.
            &lpSvcAdmin))) // Pointer to new IMsgServiceAdmin.
        {
            outputResult("AdminServices",hr);
            throw -1;
        }

        // Create the new message service for Exchange.

        if (FAILED(hr = lpSvcAdmin->CreateMsgService("MSEMS",     // Name of service from MAPISVC.INF.
            NULL,        // Display name of service.
            NULL,        // Handle to parent window.
            NULL)))      // Flags.
        {
            outputResult("CreateMsgService",hr);
            throw -1;
        }
       

        // 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(hr = lpSvcAdmin->GetMsgServiceTable(0,                 // Flags.
            &lpMsgSvcTable)))  // Pointer to table.
        {
            outputResult("GetMsgServiceTable",hr);
            throw -1;
        }

        // 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 = "MSEMS";

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

        if (FAILED(hr = HrQueryAllRows(lpMsgSvcTable,
            (LPSPropTagArray)&sptCols,
            &sres,
            NULL,
            0,
            &lpSvcRows)))
        {
            outputResult("HrQueryAllRows",hr);
            throw -1;
        }

        // Set up a SPropValue array for the properties that you have to configure.
   
        // First, the server name.
        ZeroMemory(&rgval[1], sizeof(SPropValue) );
        rgval[1].ulPropTag = PR_PROFILE_UNRESOLVED_SERVER;
        rgval[1].Value.lpszA = szServer;

        // Next, the mailbox name.
        ZeroMemory(&rgval[0], sizeof(SPropValue) );
        rgval[0].ulPropTag = PR_PROFILE_UNRESOLVED_NAME;
        rgval[0].Value.lpszA = szMailbox;

        // Configure the message service by using the previous properties.
   
        if (FAILED(hr = 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.
        {
            outputResult("ConfigureMsgService",hr);
            throw -1;
        }
    }
    catch(…)
    {
        if (lpSvcRows) FreeProws(lpSvcRows);
        if (lpMsgSvcTable) lpMsgSvcTable->Release();
        if (lpSvcAdmin) lpSvcAdmin->Release();
        if (lpProfAdmin) lpProfAdmin->Release();
        MAPIUninitialize();
        return FALSE;
    }

    if (lpSvcRows) FreeProws(lpSvcRows);
    if (lpMsgSvcTable) lpMsgSvcTable->Release();
    if (lpSvcAdmin) lpSvcAdmin->Release();
    if (lpProfAdmin) lpProfAdmin->Release();
    MAPIUninitialize();

    return TRUE;

}

需要的参数比较简单,就是profile的名字,mailbox的名字和exchange server的地址。

(Visited 113 times, 1 visits today)