⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 safearrays.cpp

📁 vc ADO 连接数据库
💻 CPP
字号:
#include <stdio.h>
#import  <msado15.dll> rename("EOF", "adoEOF")
#include <eh.h>

struct InitOle {
InitOle()  { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize();   }
} _init_InitOle_;


// Class for Win32 Structured Exception Handling
class SEH_Exception {
private:
    unsigned int m_uSECode;
public:
    SEH_Exception(unsigned int uSECode) : m_uSECode(uSECode) {}
    SEH_Exception() {}
    ~SEH_Exception() {}
    unsigned int getSeHNumber() { return m_uSECode; }
};

// Raise Win32 Exception within a C++ Class SEH_Exception
static void MappingSEHtoCPPExceptions
(
    unsigned int uExceptionCode, 
    _EXCEPTION_POINTERS*
)
{
    throw SEH_Exception( uExceptionCode );
}

// Sets up mapping between Win32 SEH & C++ Exception Handling
void LogEnable()
{
    _set_se_translator(MappingSEHtoCPPExceptions);
}


void main( void )
{
    ADODB::_ConnectionPtr Conn1 = NULL;
    ADODB::_RecordsetPtr  Rs1   = NULL;
    HRESULT               hr    = S_OK;
    _variant_t  vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);

    try
    {
        hr = Conn1.CreateInstance( __uuidof( ADODB::Connection ) );
        Conn1->Open( L"DSN=AdoDemo;UID=sa;PWD=;", L"", L"", -1 );

        // Create elements used in the array
        _variant_t varCriteria[4];
        varCriteria[0] = vtEmpty;
        varCriteria[1] = vtEmpty;
        varCriteria[2] = L"Authors";
        varCriteria[3] = vtEmpty;
        const int nCrit = sizeof varCriteria / 
                          sizeof varCriteria[0];

        // Create SafeArray Bounds and initialize the array
        SAFEARRAYBOUND rgsabound[1];
        rgsabound[0].lLbound   = 0;   
        rgsabound[0].cElements = nCrit;
        SAFEARRAY *psa         = SafeArrayCreate( VT_VARIANT, 1, rgsabound );

        // Set the values for each element of the array
        for( long i = 0 ; i < nCrit && SUCCEEDED( hr );i++)
        {          
           hr  = SafeArrayPutElement(psa, &i,&varCriteria[i]); 
        }

        // Initialize and fill the SafeArray
        VARIANT vsa;
        vsa.vt = VT_VARIANT | VT_ARRAY;
        V_ARRAY(&vsa) = psa;

        // call OpenSchema        
        Rs1 = Conn1->OpenSchema(ADODB::adSchemaTables,vsa);

        Rs1->Close();
        Conn1->Close();

        ::MessageBox( NULL, "Success!", "", MB_OK );
    }
    catch( _com_error &e )
    {
        _bstr_t bstrSource(e.Source());
        _bstr_t bstrDescription(e.Description());

        printf( "#import encountered failed HRESULT\n" );
        printf( "\tCode = %08lx\n",      e.Error());
        printf( "\tCode meaning = %s\n",   e.ErrorMessage());
        printf( "\tSource = %s\n",         (LPCTSTR) bstrSource);
        printf( "\tDescription = %s\n",    (LPCTSTR) bstrDescription);
    }
    catch( SEH_Exception &e )
    {
        printf( "Win32 Structured Exception Raised\n" );
        printf( "\t Error Code %08lx\n", e.getSeHNumber() );
    }
    catch(...)
    {
        printf( "Caught an exception of unknown type\n" );
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -