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. Casting

Casting

Scheduled Pinned Locked Moved COM
questionsysadmin
5 Posts 5 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.
  • J Offline
    J Offline
    Justin Turney
    wrote on last edited by
    #1

    I have an out-proc server i'm currently writing. In this server I have a number of interfaces. I was wondering if I could do the following. All the interfaces and implementations of the interfaces are in the same project. interface IUser {....}; // IUser definition interface IUsers {....}; // IUsers definitions class CUser : public IUser {....}; // The implementation of IUser class CUsers : public IUsers {...}; // The implementation of IUsers HRESULT CUsers::SomeFunc() { CComPtr pUser; pUser.CreateInstance(__uuidof(IUser)); ((CUser*)(pUser.p))->m_SomeData; // Can I do this cast from IUser to CUser? ... } That was my question, is the cast possible? Justin Turney

    P R M V 4 Replies Last reply
    0
    • J Justin Turney

      I have an out-proc server i'm currently writing. In this server I have a number of interfaces. I was wondering if I could do the following. All the interfaces and implementations of the interfaces are in the same project. interface IUser {....}; // IUser definition interface IUsers {....}; // IUsers definitions class CUser : public IUser {....}; // The implementation of IUser class CUsers : public IUsers {...}; // The implementation of IUsers HRESULT CUsers::SomeFunc() { CComPtr pUser; pUser.CreateInstance(__uuidof(IUser)); ((CUser*)(pUser.p))->m_SomeData; // Can I do this cast from IUser to CUser? ... } That was my question, is the cast possible? Justin Turney

      P Offline
      P Offline
      Paul M Watt
      wrote on last edited by
      #2

      Yes, but it is not safe. Since you are in your CUser class when you create the new object, it would be safer to call new, then cast the object to the interface if the interface needed to be exported from the function.

      HRESULT CUsers::SomeFunc()
      {
      CUser pUser = new CUser;
      pUser->m_SomeData;
      ...
      CComPtr piUser
      piUser = static_cast(pUser);
      ...
      }

      Good Luck


      Build a man a fire, and he will be warm for a day
      Light a man on fire, and he will be warm for the rest of his life!

      1 Reply Last reply
      0
      • J Justin Turney

        I have an out-proc server i'm currently writing. In this server I have a number of interfaces. I was wondering if I could do the following. All the interfaces and implementations of the interfaces are in the same project. interface IUser {....}; // IUser definition interface IUsers {....}; // IUsers definitions class CUser : public IUser {....}; // The implementation of IUser class CUsers : public IUsers {...}; // The implementation of IUsers HRESULT CUsers::SomeFunc() { CComPtr pUser; pUser.CreateInstance(__uuidof(IUser)); ((CUser*)(pUser.p))->m_SomeData; // Can I do this cast from IUser to CUser? ... } That was my question, is the cast possible? Justin Turney

        R Offline
        R Offline
        Rama Krishna Vavilala
        wrote on last edited by
        #3

        Justin Turney wrote: That was my question, is the cast possible? Why do you want to do that? You should never do anything like that. If a proxy or stub gets involved in between then the code will not be safe.

        1 Reply Last reply
        0
        • J Justin Turney

          I have an out-proc server i'm currently writing. In this server I have a number of interfaces. I was wondering if I could do the following. All the interfaces and implementations of the interfaces are in the same project. interface IUser {....}; // IUser definition interface IUsers {....}; // IUsers definitions class CUser : public IUser {....}; // The implementation of IUser class CUsers : public IUsers {...}; // The implementation of IUsers HRESULT CUsers::SomeFunc() { CComPtr pUser; pUser.CreateInstance(__uuidof(IUser)); ((CUser*)(pUser.p))->m_SomeData; // Can I do this cast from IUser to CUser? ... } That was my question, is the cast possible? Justin Turney

          M Offline
          M Offline
          mhamsa
          wrote on last edited by
          #4

          Can't you implement a propget on IUser that returns the contents of m_SomeData?

          1 Reply Last reply
          0
          • J Justin Turney

            I have an out-proc server i'm currently writing. In this server I have a number of interfaces. I was wondering if I could do the following. All the interfaces and implementations of the interfaces are in the same project. interface IUser {....}; // IUser definition interface IUsers {....}; // IUsers definitions class CUser : public IUser {....}; // The implementation of IUser class CUsers : public IUsers {...}; // The implementation of IUsers HRESULT CUsers::SomeFunc() { CComPtr pUser; pUser.CreateInstance(__uuidof(IUser)); ((CUser*)(pUser.p))->m_SomeData; // Can I do this cast from IUser to CUser? ... } That was my question, is the cast possible? Justin Turney

            V Offline
            V Offline
            Vi2
            wrote on last edited by
            #5

            pUser.CreateInstance(__uuidof(IUser));

            __uuidof(IUser) is the UUID of IUser interface, not the his coclass CLSID. So this call will be unsuccessful. You can use the local creation of COM object:

            CComObject < CUser >* pUser;
            HRESULT hr = pUser->CreateInstance( & pUser );
            if( SUCCEEDED(hr) )
            {
            pUser->m_SomeData;
            ...
            delete pUser;
            }

            With best wishes, Vita

            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