Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. QueryInterface -- How come this code doesn't work?

QueryInterface -- How come this code doesn't work?

Scheduled Pinned Locked Moved COM
questiondatabasedebugginghelp
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Popeye Doyle Murray
    wrote on last edited by
    #1

    Here is the code:

    // this line gets a valid ADO database connection object
    _ConnectionPtr pConnection = m_AdoDatabase.GetActiveConnection();

    // Now I will get a pUknown pointing to the database connection
    IUnknown* pUnk;

    // this is successful
    HRESULT hr = pConnection->QueryInterface(IID_IDispatch, (void**) &pUnk);

    // Now I will attempt to get an ADO connection object from pUnknown
    _ConnectionPtr pConnection2;

    // this line fails
    hr = pConnection->QueryInterface(__uuidof(Connection), (void**) &pConnection2);

    if (FAILED(hr))
    {
    // this is where the failure is!
    // hr is returned with a value E_NOINTERFACE
    TRACE("Failed to get pointer the second time.\n");
    TRACE("Error: %s\n\n", GetComError(hr));
    }

    Shouldn't I be able to go from an ADO Connection object, to IUnknown, and then back again? How do I get back my ADO Connection from IUnkown?

    S 1 Reply Last reply
    0
    • P Popeye Doyle Murray

      Here is the code:

      // this line gets a valid ADO database connection object
      _ConnectionPtr pConnection = m_AdoDatabase.GetActiveConnection();

      // Now I will get a pUknown pointing to the database connection
      IUnknown* pUnk;

      // this is successful
      HRESULT hr = pConnection->QueryInterface(IID_IDispatch, (void**) &pUnk);

      // Now I will attempt to get an ADO connection object from pUnknown
      _ConnectionPtr pConnection2;

      // this line fails
      hr = pConnection->QueryInterface(__uuidof(Connection), (void**) &pConnection2);

      if (FAILED(hr))
      {
      // this is where the failure is!
      // hr is returned with a value E_NOINTERFACE
      TRACE("Failed to get pointer the second time.\n");
      TRACE("Error: %s\n\n", GetComError(hr));
      }

      Shouldn't I be able to go from an ADO Connection object, to IUnknown, and then back again? How do I get back my ADO Connection from IUnkown?

      S Offline
      S Offline
      Stephane Rodriguez
      wrote on last edited by
      #2

      Haven't checked, but may be __uuidof(Connection) returns the CLSID of the Connection object, while what is expected in queryinterface() is the IID of the interface.


      RSS feed

      G S P 3 Replies Last reply
      0
      • S Stephane Rodriguez

        Haven't checked, but may be __uuidof(Connection) returns the CLSID of the Connection object, while what is expected in queryinterface() is the IID of the interface.


        RSS feed

        G Offline
        G Offline
        geo_m
        wrote on last edited by
        #3

        right!

        1 Reply Last reply
        0
        • S Stephane Rodriguez

          Haven't checked, but may be __uuidof(Connection) returns the CLSID of the Connection object, while what is expected in queryinterface() is the IID of the interface.


          RSS feed

          S Offline
          S Offline
          Steve S
          wrote on last edited by
          #4

          And as an aside, you're not asking for IUnknown in the first QueryInterface, but IDispatch. In the case you have, both should work, of course... Steve S [This signature space available for rent]

          1 Reply Last reply
          0
          • S Stephane Rodriguez

            Haven't checked, but may be __uuidof(Connection) returns the CLSID of the Connection object, while what is expected in queryinterface() is the IID of the interface.


            RSS feed

            P Offline
            P Offline
            Popeye Doyle Murray
            wrote on last edited by
            #5

            So how do I get the IID?

            S 1 Reply Last reply
            0
            • P Popeye Doyle Murray

              So how do I get the IID?

              S Offline
              S Offline
              Stephane Rodriguez
              wrote on last edited by
              #6

              I believe either __uuidof(_Connection) or __uuidof(_ConnectionPtr) is better. Make sure to check that the return IID is non-NULL by the way.


              RSS feed

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups