Use Microsoft CDO Example

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Jason (na_at_na.com)
Date: 05/15/04


Date: Fri, 14 May 2004 23:08:25 -0500

How to create a mailbox-enabled user with CDOEXM in Visual C++
http://support.microsoft.com/default.aspx?kbid=293339&product=edk

I am using Microsofts example. I have added a little error checking.
MSVC 6 on Windows XP. Compiles with the following:

warning C4192: automatically excluding 'ISupportErrorInfo' while importing type library
'C:\Program Files\Exchsrvr\BIN\cdoexm.dll'
warning C4146: unary minus operator applied to unsigned type, result still unsigned
Linking...

I then copy to the 2000 Exchange Server and it always fails at:

hr = pMailbox->CreateMailbox((BSTR)strCreateString);

My Code for completeness sake:

Help!

----------------------------------------------------------------------------------------------
#import "C:\Program Files\Common Files\Microsoft Shared\CDO\cdoex.dll" no_namespace
raw_interfaces_only rename("Folder","CDOEXFolder")
#import "C:\Program Files\Exchsrvr\BIN\cdoexm.dll" no_namespace raw_interfaces_only
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace
rename("EOF","adoEOF"), named_guids

#include <iostream>

int main()
{
    HRESULT hr;

 std::cout << "Starting\n";

 hr = CoInitialize(NULL);

 if (FAILED(hr))
 {
  std::cout << "CoInitialize Failed\n";
 }

 {
      IPersonPtr pPerson(__uuidof(Person));
      IMailboxStorePtr pMailbox;
      IDataSourcePtr pDataSource;
      FieldsPtr pFields;
      FieldPtr pField;

    /*
        strServerName - This is the server name.
        For example: "MyServer6"
    */
      _bstr_t strServerName = "SYSTEM1";

    /*
        strDomainName - This is the server's domain.
        For example: "DC=MYDOMAIN3,DC=microsoft,DC=com"
    */
      _bstr_t strDomainName = "dc=domain,dc=extest,dc=test,dc=org";

    /*
        strLoginName - This is the login name. Note that this is the same as the e-mail
name, but in some cases can be different.
        For example: "lname:
    */
      _bstr_t strLoginName = "larry";

    /*
        strExchangeOrg - This is the Exchange organization that houses the mailbox store.
        For example: "First Organization"
    */
      _bstr_t strExchangeOrg = "First Organization";

    /*
        strAdminGroup - This is the Exchange administrative group name.
        For example: "First Administrative Group"
    */
      _bstr_t strAdminGroup = "First Administrative Group";

    /*
      strStorageGroup - This is the storage group for the mailbox store.
        For example: "First Storage Group"
    */
      _bstr_t strStorageGroup = "First Storage Group";

    /*
      strStoreName - This is the mailbox store name.
      For example: "Mailbox Store (SERVERNAME)"
    */
      _bstr_t strStoreName = "Mailbox Store (SYSTEM1)";

    /*
      strEmailName - This is the user's e-mail name.
      For example: "FirsnameLastName"
    */
      _bstr_t strEmailName = "larry";

    /*
      strDNSSuffix - the server's DNS suffix.
      For example "MYDOMAIN3.microsoft.com
    */
      _bstr_t strDNSSuffix = "@domain.test.org";

    /*
        strFirstName - This is the user's first name.
        For example: "Firstname"
    */
      _bstr_t strFirstName = "Larry";

    /*
      strLastName - This is the user's last name.
      For example: "Lastname"
    */
      _bstr_t strLastName = "Whatever";

    /*
        strPassword - This is the user's password.
        For example: "password"
    */
      _bstr_t strPassword = "welcome";

    /*
        strUserAccountControl - This is the type of account.
        For example: "512"
    */
      _bstr_t strUserAccountControl = "512";

    /*
      _bstr_t strURL =
"LDAP://servername/CN=EmailPerson,CN=users,dc=mydomain,dc=extest,dc=microsoft,dc=com";
    */
      _bstr_t strURL = "LDAP://" + strServerName + "/CN=" + strEmailName +
                        ",CN=users," + strDomainName;

      /*
        _bstr_t strCreateString =
        "LDAP://servername/CN=Mailbox Store (SERVERNAME),"
        "CN=First Storage Group, CN=InformationStore, CN=servername,"
        "CN=Servers, CN=First Administrative Group, CN=Administrative "
        "Groups,CN=First Organization,CN=Microsoft Exchange, CN=Services,"
        "CN=Configuration, dc=mydomain, dc=extest, dc=microsoft, dc=com";
      */
      _bstr_t strCreateString =
          "LDAP://" + strServerName + "/CN=" + strStoreName + ",CN=" +
          strStorageGroup + ",CN=InformationStore,CN=" + strServerName +
          ",CN=Servers,CN=" + strAdminGroup + "," +
          "CN=Administrative Groups,CN=" + strExchangeOrg + "," +
          "CN=Microsoft Exchange, CN=Services," +
          "CN=Configuration," + strDomainName;

      //Get the fields from the person.

   std::cout << "All Data set\n";

   hr = pPerson->get_Fields(&pFields);
   if (FAILED(hr))
   {
    std::cout << "Fail\n";
   }

      //Set the first and last name.
      hr = pPerson->put_FirstName((BSTR)strFirstName);
   if (FAILED(hr))
   {
    std::cout << "put_FirstName Fail\n";
   }

      hr = pPerson->put_LastName((BSTR)strLastName);
   if (FAILED(hr))
   {
    std::cout << " put_LastNameFail\n";
   }

      //Set userPrincipalName.
      _bstr_t strUPN = strLoginName + "@" + strDNSSuffix;
      pField = pFields->GetItem("userPrincipalName");
      pField->PutValue(strUPN);

      //Set userAccountControl.
      pField = pFields->GetItem("userAccountControl");
      pField->PutValue(strUserAccountControl);

      //Set userPassword.
      pField = pFields->GetItem("userPassword");
      pField->PutValue(strPassword);

      //Save the changes.
      hr = pFields->Update();
   if (FAILED(hr))
   {
    std::cout << "pFields->Update Fail\n";
   }

      //Get the DataSource and create the user.
      hr = pPerson->get_DataSource(&pDataSource);
      hr = pDataSource->SaveTo((BSTR)strURL, NULL, adModeReadWrite, adCreateNonCollection,
(RecordOpenOptionsEnum) NULL,
             bstr_t(),
             bstr_t());

      //Get MailBoxStore and mailbox-enable the user.
      hr = pPerson->QueryInterface(__uuidof(IMailboxStore),(void**)&pMailbox);
   if (FAILED(hr))
   {
    std::cout << "QueryInterface Fail\n";
   }

   hr = pMailbox->CreateMailbox((BSTR)strCreateString);
   if (FAILED(hr))
   {
    std::cout << "CreateMailbox Fail\n";
    std::cout << GetLastError();
   }

      //Save the DataSource.
      hr = pDataSource->Save();
   if (FAILED(hr))
   {
    std::cout << "pDataSource Fail\n";
   }

     }

     CoUninitialize();
     return(0);

}



Relevant Pages

  • Re: Use Microsoft CDO Example
    ... > MSVC 6 on Windows XP. ... Compiles with the following: ... unary minus operator applied to unsigned type, ...
    (microsoft.public.win32.programmer.messaging)
  • Announce: Win32::GUI V1.04
    ... ClassData, GetKeyState, SetWindowPos (Tracker: 1469648). ... Other Features ... including the "standard" windows resources. ... maintained except for this warning. ...
    (comp.lang.perl.misc)
  • Announce: Win32::GUI V1.04
    ... ClassData, GetKeyState, SetWindowPos (Tracker: 1469648). ... Other Features ... including the "standard" windows resources. ... maintained except for this warning. ...
    (comp.lang.perl.modules)
  • Re: Windows XP and IE6 problems
    ... I see the warning above has something to do with Wireless Assistant - I ... 6.Go back to start menu and in all programs go to "windows update" you have ... Event Source: Application Error ... Event Type: Warning ...
    (microsoft.public.windowsxp.general)
  • Re: New Info on IRQ BSOD
    ... What programme is not immediately apparent. ... apparent from file version that Windows XP is not being updated in the ... warning is probably safely ignored. ... aldago wrote: ...
    (microsoft.public.windowsxp.help_and_support)