none
C++ Programmatically get certificate to Outlook contacts RRS feed

  • Question

  • Hi

    I'm trying to read x509v3 certificate form AB, I can enumerate contacts and read properties as a VARIANT array, however I can't get the certificate, the error is 0x80070057.

    What I'm doing wrong?

    pContact=pItems->GetNext();			<- SUCCESS
    
    CString 		cUserCertificate;
    HRESULT			hr = NULL;
    VARIANT			vaUserCertificates,vItemCertificate;
    _bstr_t			UserCertificate;
    _PropertyAccessorPtr	pPropAccPtr;
    
    pPropAccPtr = pContact->GetPropertyAccessor();
    vaUserCertificates = pPropAccPtr->GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3A701102");	//{ PidTagUserX509Certificate, "0x3A701102" }
    
    if(vaUserCertificates.vt != VT_EMPTY)		// VT_ARRAY | VT_VARIANT
    {
    	LONG lstart, lend;
    	SAFEARRAY *psa = V_ARRAY( &vaUserCertificates );
    	hr = SafeArrayGetLBound( psa, 1, &lstart );		// <- Succeess
    	hr = SafeArrayGetUBound( psa, 1, &lend );		// <- Succeess
    
    	for ( long idx=lstart; idx <= lend; idx++ ) 
    	{
    		VariantInit(&vItemCertificate);
    		hr = SafeArrayGetElement( psa, &idx, &vItemCertificate);	// <- Error 0x80070057
    		if (FAILED(hr))
    			cUserCertificate.Format("Error = 0x%08X ", hr);	
    		else 
    			cUserCertificate.Format("OK V_Type=%08X", vItemCertificate.vt);
    
    		MessageBox(cUserCertificate, "Info");
    		VariantClear(&vItemCertificate);
    	}
    	SafeArrayDestroy(psa);
    }

    Saturday, June 1, 2013 2:16 PM

Answers

  • I added the variable initialization, but the error is the same res=0x80070057

    if(vaUserCertificates.vt == (VT_ARRAY | VT_VARIANT))	
    {
        long lstart, lend, idx[1];
        LPSAFEARRAY psa = vaUserCertificates.parray;      
        idx[0] = idx[1] = 1;
        hr = SafeArrayGetLBound( psa, 1, &lstart );
        hr = SafeArrayGetUBound( psa, 1, &lend );
        for ( idx[0]=lstart; idx[0] <= lend; idx[0]++ ) 
    	{
    		VariantInit(&vItemCertificate);
    		hr = SafeArrayGetElement( psa, &idx[0], &vItemCertificate);
                       printf("Result = 0x08X\n",hr); // hr=0x80070057
             }
    .....
    }	

    How to use  SafeArrayGetElement in proper way ?

    O maybe you how some sample code to retrieve Certificates from AB ?

    • Marked as answer by JacekN Tuesday, July 23, 2013 8:01 PM
    Friday, June 7, 2013 9:15 PM
  • I found source of the problem, small change:

    VARIANT vaUserCertificates,vItemCertificate;
    to

    _variant_t   vaUserCertificates;

    VARIANT vItemCertificate;

    • Marked as answer by JacekN Tuesday, July 23, 2013 8:02 PM
    Tuesday, July 23, 2013 8:01 PM

All replies

  • SafeArrayGetElement second parameter is a pointer to an array of integers. You are passing a pointer to an integer.

    Keep in mind that PR_USER_X509_CERTIFICATE is PT_MV_BINARY property, so you have an a multidimensional array, not a simple 1D array.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!


    Saturday, June 1, 2013 6:16 PM
  • >SafeArrayGetElement second parameter is a pointer to an array of integers. You are passing a pointer to an integer.

    I did change:

    if(vaUserCertificates.vt == (VT_ARRAY | VT_VARIANT))	
    {
        long lstart, lend, idx[1];
        LPSAFEARRAY psa = vaUserCertificates.parray;      
        hr = SafeArrayGetLBound( psa, 1, &lstart );
        hr = SafeArrayGetUBound( psa, 1, &lend );
        for ( idx[0]=lstart; idx[0] <= lend; idx[0]++ ) 
    	{
    		VariantInit(&vItemCertificate);
    		hr = SafeArrayGetElement( psa, &idx, &vItemCertificate);
             }
    .....
    }	

    but then I got error:

    error C2664: 'SafeArrayGetElement' : cannot convert parameter 2 from 'long (*)[1]' to 'LONG *' 

     

     
    • Edited by JacekN Saturday, June 1, 2013 8:24 PM
    Saturday, June 1, 2013 8:10 PM
  • Change it to &idx[0]

    But the problem is that you never initialize the idx array. What does SafeArrayGetDim return?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, June 3, 2013 2:06 AM
  • I added the variable initialization, but the error is the same res=0x80070057

    if(vaUserCertificates.vt == (VT_ARRAY | VT_VARIANT))	
    {
        long lstart, lend, idx[1];
        LPSAFEARRAY psa = vaUserCertificates.parray;      
        idx[0] = idx[1] = 1;
        hr = SafeArrayGetLBound( psa, 1, &lstart );
        hr = SafeArrayGetUBound( psa, 1, &lend );
        for ( idx[0]=lstart; idx[0] <= lend; idx[0]++ ) 
    	{
    		VariantInit(&vItemCertificate);
    		hr = SafeArrayGetElement( psa, &idx[0], &vItemCertificate);
                       printf("Result = 0x08X\n",hr); // hr=0x80070057
             }
    .....
    }	

    How to use  SafeArrayGetElement in proper way ?

    O maybe you how some sample code to retrieve Certificates from AB ?

    • Marked as answer by JacekN Tuesday, July 23, 2013 8:01 PM
    Friday, June 7, 2013 9:15 PM
  • I found source of the problem, small change:

    VARIANT vaUserCertificates,vItemCertificate;
    to

    _variant_t   vaUserCertificates;

    VARIANT vItemCertificate;

    • Marked as answer by JacekN Tuesday, July 23, 2013 8:02 PM
    Tuesday, July 23, 2013 8:01 PM