CoInitialize

This is a discussion on CoInitialize within the C++ Programming forums, part of the General Programming Boards category; Hello I am having a problem... okay in the following code there is a function called CoInitialize. this function is ...

  1. #1
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41

    CoInitialize

    Hello I am having a problem... okay in the following code there is a function called CoInitialize. this function is giving me an error saying it is undefined.
    okay i know it has something to do with the dll and i think it has something to do with my compiler...

    is there something equal to CoInitialize in C++ Borland 6?

    Any help would be great!



    Code:
    //**************************************
    //     
    // Name: Access MDB
    // Description:To open MS Access or Ms S
    //     QL server database using C/C++ or VC++
    // By: Mokarrabin A Rahman
    //
    // Returns:Shows some recordsets
    //
    // Assumes:Change the name of the databa
    //     se to any database and place it in the s
    //     ame directory as the .exe. You shoul als
    //     o change the SQL to your particular data
    //     base. You may change the connect string 
    //     to connect to MS SQL Server.
    //
    // Side Effects:Great
    //
    //This code is copyrighted and has// limited warranties.Please see http://
    //     www.Planet-Source-Code.com/xq/ASP/txtCod
    //     eId.1371/lngWId.3/qx/vb/scripts/ShowCode
    //     .htm//for details.//**************************************
    //     
    
    #import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
    no_namespace rename("EOF", "EndOfFile")
    // This code comes from : www.geocities.
    //     com/mokarrabin
    #include <stdio.h>
    #include <iostream.h>
    void main(void)
    
    
        {
        CoInitialize(NULL);
        try 
    
    
            {
            _RecordsetPtr pRst("ADODB.Recordset");
            // Connection String
            _bstr_t strCnn("DRIVER={Microsoft Access Driver (*.mdb)};UID=admin;DBQ=GBOOK.mdb");
            	 // Open table
            	pRst->Open("SELECT * FROM ProductService where ProductService like '%samir%';", strCnn, adOpenStatic, adLockReadOnly, adCmdText);
            	 
            	 pRst->MoveFirst();
    
    
                	 while (!pRst->EndOfFile) {
                		 cout<<(char*) ((_bstr_t) pRst->GetFields()->GetItem("ProductService")->GetValue())<<endl;
                		 pRst->MoveNext();
                	 }
                	 pRst->Close(); 
                	 
            }
            catch (_com_error &e)
    
    
                {
                cout<<(char*) e.Description();
            }
            ::CoUninitialize();
        }

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    CoInitialize is declared in objbase.h but is superceded by CoInitializeEx. At a minimum you should #define _WIN32_DCOM prior to #including <objbase.h>; you may also have to #include <windows.h> first. If CoInitializeEx doesn't work for you, the older CoInitialize should still work ok.

    Hope that helps.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I think Ken is right. Some compilers (namely older mingw's) are retarded about linking COM stuff. Since you are using Borland 6 I doubt this is the cause. But if Ken's doesn't work check the docs on Borland 6.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    #import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
    Since I haven't tinkered with SQL, I don't know if the same thing holds true there or not, but if this is a C or C++ string, you'll need double backslashes to make that a valid path. \ is an escape sequence when in strings (or single characters) in C++.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Originally posted by quzah
    Since I haven't tinkered with SQL, I don't know if the same thing holds true there or not, but if this is a C or C++ string, you'll need double backslashes to make that a valid path. \ is an escape sequence when in strings (or single characters) in C++.

    Quzah.
    You can get away with this in MSVC++ as its just a command to the compiler and is snatched away by the preprocessor

    As Ken says, include windows.h .....you might also neede to include ole32.lib for it to link

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CoInitialize() recognized but undefined
    By @nthony in forum Windows Programming
    Replies: 4
    Last Post: 08-05-2006, 02:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21