Thread: Connecting to Oracle Problem

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    10

    Connecting to Oracle Problem

    Let me preface by saying Im not really a C programmer. More of a vb programmer doing .net stuff, but someone got word i did a little c back in college some 10 years ago and threw this at me. so im not really comfortable or sure i know what im doing.

    they are trying to connect to an oracle database we have here. using that data source, username, and pass in the connstring i can connect to the database using sql+. The code they gave me was a mess, so ive cleaned it up a bit and finally got it to compile. it crashes on the pConnection->Open line, and my catch message simple says "Invalid Pointer". Ive found a lot of very similar code on the net so I know im not doing anything too unordinary. Any ideas or can someone please point me in the right direction?

    Thanks

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <windows.h>
    
    #import "C:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF", "EndOfFile")
    
    //#include "c:\oracle\ora92\oo4o\CPP\include\ORACL.H"
    //#include "c:\oracle\ora92\oledb\include\oraoledb.h"
    
    using namespace std;
    
    void main()
    {    
    	HRESULT hr = S_OK;
    
    	_ConnectionPtr pConnection = NULL;
    
    	_bstr_t strCnn("Provider=OraOLEDB.Oracle;PLSQLRSet=1;USER Id=UPTS_TEST;Password=UPTS_TEST;Data Source=DVLP9;");
    
        try
        {
            /* create an instance of connection object */
    
            //TESTHR (pConnection.CreateInstance(__uuidof(Connection)));      
    
            /* open the connection */
    		hr = pConnection.CreateInstance(__uuidof(Connection));
    		cout << "before connect\n";
            hr = pConnection->Open(strCnn,"","",adConnectUnspecified);  
    		cout << "after connect\n";
            if (hr == S_OK)
                cout << "connected\n";
            else
                cout << "not connected\n";  
    		pConnection->Close();
    	}
    	catch(_com_error &ce)
        {
    		printf("Description = %s\n", (char*) ce.ErrorMessage()); 
        }
    }

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Code:
    hr = pConnection.CreateInstance(__uuidof(Connection));
    Set a breakpoint on the line after this one, debug and tell me what the values of hr and pConnection are.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Does this even compile?
    hr = pConnection.CreateInstance // using dot
    hr = pConnection->Open // using arrow

    Also, it's C++, not C.
    C doesn't have try/catch, namespaces or cout. Moved to C++ board.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Quote Originally Posted by Salem
    Does this even compile?
    Welcome to the wacky world of COM objects.

    RvS, after looking through some old code of mine, I believe you are missing:-
    Code:
    CoInitialize(NULL);
    At the start of main and
    Code:
    CoUninitialize();
    at the end.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    10
    very good. i will try that first thing in the morning. thanks so much

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    The reason why you are getting an invalid pointer, is that the pointer isn't initiated via createinstance():
    HRESULT hr = pConnection->CreateInstance(__uuidof(ADODB::Connection));

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    10
    Quote Originally Posted by SMurf
    Welcome to the wacky world of COM objects.

    RvS, after looking through some old code of mine, I believe you are missing:-
    Code:
    CoInitialize(NULL);
    At the start of main and
    Code:
    CoUninitialize();
    at the end.
    SMurf,
    worked like a charm, thanks so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM