Figure 3: LDAP COM API method connectAnonymously declaration

STDMETHODIMP 
CLdapConnection::connectAnonymously(BSTR host, long port, 
    VARIANT_BOOL* pError){
    
    //locals
    unsigned int len = _bstr_t(host).length()+1;
    char* stHost = new char[len];
    
    //error flags with implied error
    *pError = VARIANT_FALSE;
    HRESULT hr = S_FALSE; 
 
    //convert BSTRs to chars
    if(wcstombs( stHost, host, len ) >0 && len >1){
    
        //standar d LDAP connection
        if(port == LDAP_PORT)
            if ( (ld = ldap_init(stHost, port )) != NULL ) 
                hr = S_OK;
        
        //LDAP over SSL
        if(port == LDAPS_PORT){
            //get the cert7db path from the user variable
            char* certvar = NULL;
            certvar = GetCert7dbPath();
            
            if(certvar != NULL){
            //get SSL server authentication & open LDAP connection
            if(ldapssl_client_init( certvar, NULL ) == 0)
                if((ld = ldapssl_init( stHost, port, 1 )) != NULL ) 
                    hr = S_OK;
            DeleteCert7dbPath(certvar);
            }
        }
    }

    delete[] stHost;

    //set  out parameter error flags
    if(hr == S_FALSE)
        *pError = VARIANT_TRUE;

    return hr;    
}