SQL Server Developer Center > SQL Server Forums > SQL Server Compact > Problem deploying application, with C++ and OLEDB
Ask a questionAsk a question
 

AnswerProblem deploying application, with C++ and OLEDB

  • Monday, November 02, 2009 5:18 PMSlimDeluxe Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,
    we have an C++ application that used flatfiles for storing data.
    From the MSDN site I was able to piece up this code, but it does not seem to work on the target machine (Windows Embedded). On the target machine I installed SQLCERuntime and VC++ Redistributable is there also. Note that everything works like a charm on my workstation (WinXP SP3).
    The following is the routine I use to test the db, later it will just connect and create a session.

    #include <sqlce_oledb.h>
    #include <sqlce_err.h>
    #include <sqlce_sync.h>
    #define DATABASE_NAME _T("D:\\states.sdf")

    bool Database::connect()
    {
        const WCHAR    dbFilename[] = DATABASE_NAME;

        if(!Database::fileExists(dbFilename)){
            a_debug(_T("DB-ERROR: Database file not found!"));
            goto Exit;
        } else {
            a_debug(_T("DB: File found!"));
        }
           
        // Object declarations
        HRESULT          hr              = NOERROR;
        DBPROPSET        dbpropset[2];
        DBPROP           dbprop[1];
        DBPROP           sscedbprop[1];
        BSTR             pwdPassword = _T("0x00-0xFF"); // user input password

        // Declare the provider interfaces.
        IDBInitialize *  pIDBInitialize  = NULL;
        IDBProperties *  pIDBProperties  = NULL;

        // Initialize the data source.
        CoInitialize(NULL);
        hr = CoCreateInstance(CLSID_SQLSERVERCE, 0, CLSCTX_INPROC_SERVER, IID_IDBInitialize, (void**) &pIDBInitialize);
        if (FAILED(hr)){
            WCHAR* buff = new WCHAR[30];
            swprintf(buff, _T("DB: error code %.2x"), hr);
            a_debug(buff);
            goto Exit;
        }

        // Initialize property structures.
        VariantInit(&dbprop[0].vValue);
        VariantInit(&sscedbprop[0].vValue);

        // Initialize Property with name of database.
        dbprop[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
        dbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
        dbprop[0].vValue.vt = VT_BSTR;
        dbprop[0].vValue.bstrVal = SysAllocString(DATABASE_NAME);
        if(NULL == dbprop[0].vValue.bstrVal){
            hr = E_OUTOFMEMORY; a_debug(_T("DB-ERROR: #55"));
            goto Exit;
        }

        // Second property set has one property containing the
        // provider-specific database password in the pwdPassword variable
        // that was obtained from a dialog box (not shown).
        sscedbprop[0].dwPropertyID = DBPROP_SSCE_DBPASSWORD;
        sscedbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
        sscedbprop[0].vValue.vt = VT_BSTR;
        sscedbprop[0].vValue.bstrVal = SysAllocString(pwdPassword);
        if(NULL == sscedbprop[0].vValue.bstrVal){
            hr = E_OUTOFMEMORY; a_debug(_T("DB-ERROR: #68"));
            goto Exit;
        }

        // Initialize property set.
        dbpropset[0].guidPropertySet = DBPROPSET_DBINIT;
        dbpropset[0].rgProperties = dbprop;
        dbpropset[0].cProperties = sizeof(dbprop)/sizeof(dbprop[0]);

        // Initialize the provider-specific property set.
        dbpropset[1].guidPropertySet = DBPROPSET_SSCE_DBINIT;
        dbpropset[1].rgProperties = sscedbprop;
        dbpropset[1].cProperties = sizeof(sscedbprop)/sizeof(sscedbprop[0]);

        // Set the properties into the provider's data source object.
        hr = pIDBInitialize->QueryInterface(IID_IDBProperties,(void**)&pIDBProperties);
        if (FAILED(hr)){
            a_debug(_T("DB-ERROR: #85"));
            goto Exit;
        }

        hr = pIDBProperties->SetProperties(sizeof(dbpropset)/sizeof(dbpropset[0]), dbpropset);
        if(FAILED(hr)){
            a_debug(_T("DB-ERROR: #91"));
            goto Exit;
        }

        // Initialize the data source.
        hr = pIDBInitialize->Initialize();
        if(FAILED(hr)){
            a_debug(_T("DB-ERROR: #98"));
           goto Exit;
        }
        //-----------------------------------------------------------------

        IDBCreateSession* pIDBCSession = NULL;
        IUnknown* pDBSession = NULL;
        IDBCreateCommand* pIDBCreateCommand = NULL;
        IUnknown* pCommand = NULL;
        ICommandText* pICommandText = NULL;
        ICommand* pICommand = NULL;


        pIDBInitialize->QueryInterface(IID_IDBCreateSession, (void **) &pIDBCSession);
        if (FAILED(hr)){
            a_debug(_T("DB-ERROR: #113"));
            goto Exit;
        }
        pIDBCSession->CreateSession(NULL, IID_IUnknown, (LPUNKNOWN *) &pDBSession);
        if (FAILED(hr)){
            a_debug(_T("DB-ERROR: #118"));
            goto Exit;
        }
        pDBSession->QueryInterface(IID_IDBCreateCommand, (void **)&pIDBCreateCommand);
        if (FAILED(hr)){
            a_debug(_T("DB-ERROR: #123"));
            goto Exit;
        }

        hr = pIDBCreateCommand->CreateCommand(NULL, IID_IUnknown, (IUnknown **)&pCommand);
        if (FAILED(hr)){
            a_debug(_T("DB-ERROR: #129"));
            goto Exit;
        }
        hr = pCommand->QueryInterface(IID_ICommandText, (void **)&pICommandText);
        if (FAILED(hr)){
            a_debug(_T("DB-ERROR: #134"));
            goto Exit;
        }
        // -.-
        WCHAR query[] = _T("INSERT INTO counters (description, value, nnId) VALUES ('test counter 3', 599, 545)");
        //WCHAR query[] = _T("SELECT counters.* FROM counters");

        hr = pICommandText->SetCommandText(DBGUID_DBSQL, (LPCOLESTR)query);
        if (FAILED(hr)){
            a_debug(_T("DB-ERROR: #143"));
            goto Exit;
        }
        hr = pCommand->QueryInterface(IID_ICommand, (void **)&pICommand);
        if (FAILED(hr)){
            a_debug(_T("DB-ERROR: #148"));
            goto Exit;
        }
        hr = pICommand->Execute(NULL, IID_NULL, NULL, NULL, NULL);
        //LONG cRowsAffected =0;
        //IRowset* results = NULL;
        //hr = pICommand->Execute(NULL, IID_IRowset, NULL, &cRowsAffected, (IUnknown**)&results);
        if (FAILED(hr)){
            //WCHAR* buff = new WCHAR[30];
            //swprintf(buff, _T("DB-ERROR: at execution code %lu"), hr);
            //a_debug(buff);
            a_debug(_T("APP-DB: execution error!"));
            goto Exit;
        } else {
            //WCHAR* buff = new WCHAR[30];
            //swprintf(buff, _T("DB: rows affected %lu"), cRowsAffected);
            //a_debug(buff);
        }

        //-----------------------------------------------------------------
        return true;

        Exit:
        if(pICommandText){
            pICommandText->Release();
            pICommandText = NULL;
        }
        if(pICommand){
            pICommand->Release();
            pICommand = NULL;
        }   
        // Clean up resources here.
        return false;
    }

    I was able to trace the error to this block. The application fails on this operation:

    hr = CoCreateInstance(CLSID_SQLSERVERCE, 0, CLSCTX_INPROC_SERVER, IID_IDBInitialize, (void**) &pIDBInitialize);

    The handle gets a value of: 0x800736B1.
    Google seems to point in the direction that I need to register used DLLs. How can this be done in a C++ project in VS2008? Is there something else I should do?
    Thanks and best regards.

Answers

  • Tuesday, November 03, 2009 10:03 AMSlimDeluxe Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Joao,
    when I tried to create an instance of explicitly CLSID_SQLSERVERCE_3_5 something changed since I was getting an access violation exception (sure, because of the GOTO), but in the mean time, I continued browsing the forums and found a solution suggested by a MSFT individual: http://social.msdn.microsoft.com/Forums/en-US/sqlce/thread/4cbde97a-5441-4140-9f8b-6edd48eb17e5/

    I tried reinstalling SP1 for the last time and then I finally installed SLQ Compact SP2 Beta 2 (only) on the target machine and the problem is now solved . My "branch" will be shipping to production in about a year, so I guess SP2 will be gold by then.

    Thank you for your help! =)

All Replies

  • Monday, November 02, 2009 9:40 PMJoão Paulo FigueiraMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    This means that you don't have the appropriate OLE DB provider correctly installed. Please note that each version of SQL Compact has a different CLSID value so you will get this error is you installed 3.5 and try to open 3.0. Also note that these can be installed side by side. In any case, please make sure that you have the correct provider installed.

    João Paulo Figueira (Device Application Development MVP)
  • Tuesday, November 03, 2009 8:30 AMSlimDeluxe Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Joao,
    I checked again and both installations are 3.5 SP1 and the DLL versions are all the same.
  • Tuesday, November 03, 2009 8:33 AMJoão Paulo FigueiraMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    That looks like a faulty installation to me. Remove and install again.
    João Paulo Figueira (Device Application Development MVP)
  • Tuesday, November 03, 2009 9:12 AMJoão Paulo FigueiraMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Also, please make sure that CLSID_SQLSERVERCE resolves to CLSID_SQLSERVERCE_3_5.

    João Paulo Figueira (Device Application Development MVP)
  • Tuesday, November 03, 2009 10:03 AMSlimDeluxe Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Joao,
    when I tried to create an instance of explicitly CLSID_SQLSERVERCE_3_5 something changed since I was getting an access violation exception (sure, because of the GOTO), but in the mean time, I continued browsing the forums and found a solution suggested by a MSFT individual: http://social.msdn.microsoft.com/Forums/en-US/sqlce/thread/4cbde97a-5441-4140-9f8b-6edd48eb17e5/

    I tried reinstalling SP1 for the last time and then I finally installed SLQ Compact SP2 Beta 2 (only) on the target machine and the problem is now solved . My "branch" will be shipping to production in about a year, so I guess SP2 will be gold by then.

    Thank you for your help! =)