type safe issue

This is a discussion on type safe issue within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, My question is whether my understanding of the solution to type safe issue in CoCreateInstance is correct. There ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    type safe issue

    Hello everyone,


    My question is whether my understanding of the solution to type safe issue in CoCreateInstance is correct. There is type safe issue in CoCreateInstance,

    http://msdn2.microsoft.com/en-us/library/ms686615.aspx

    STDAPI CoCreateInstance(
    REFCLSID rclsid,
    LPUNKNOWN pUnkOuter,
    DWORD dwClsContext,
    REFIID riid,
    LPVOID * ppv
    );

    which means the riid and ppv may not conform to each other, for example, we have a IY* variable pIY and wants to get IZ type interface, CoCreateInstance (rclsid, pUnkOuter, dwClsContext, IID_IZ, &pIY);

    To solve this issue, the solution from Inside COM is,

    1. Define a template class smart pointer IComPtr<T, IID* iid>, where T is type of interface to wrap, iid is its related interface IID;
    2. Define a function in the template class called CreateInstance, the implementation is,

    2.1 invoke Release(); // release previous held pointer
    2.2 invoke CoCreateInstance (rclsid, pUnkOuter, dwClsContext, *iid, &m_pI); // m_pI the wrapped interface pointer member variable for the smart pointer template class.

    I think why the solution works, is because in the creation of the template class, the interface type T and iid is matched (example, through constructor or assignment operator), so when using the internal variable *iid and m_pI to invoke CoCreateInstance, the type always match (Interface ID and Interface type).

    Is my understanding correct?


    thanks in advance,
    George

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No. The smart pointer is really no more typesafe than the core COM: if the programmer has a mismatch in T and the IID, you get a wrong answer. COM simply is not typesafe, and that's it.

    The closest solution is VC++'s com_ptr_t or whatever it's called (VC++ comes with its own COM smart pointer), which doesn't pass the IID explicitly. Instead, it makes use of VC++'s __guidof and attributes extensions. The latter allows you to associate an IID with the interface at interface declaration. (Hopefully you don't make a mistake there.) The former allows you to retrieve that IID. This way, you can't pass a wrong IID to the smart pointer.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    Quote Originally Posted by CornedBee View Post
    No. The smart pointer is really no more typesafe than the core COM: if the programmer has a mismatch in T and the IID, you get a wrong answer. COM simply is not typesafe, and that's it.
    You mean we may define,

    IComPtr<IX*, IID_IY> sIX;

    which the smart poiner allows, but not type safe?


    regards,
    George

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yep.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    My question is answered.

    Quote Originally Posted by CornedBee View Post
    Yep.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  3. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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