How do I implement an interface given a header file?












0















This is probably a silly question and I hesitated to post, but I'm a newbie at ATL.



I need to implement an interface in my code. I do not have a TLB or IDL for tha t interface, I just have a header file.



The header file defines the interface as follows:



EXTERN_C const IID IID_IExternalCon;
...

MIDL_INTERFACE("BCAC73A8-0226-4250-9D66-9656AA9BB86C")
IExternalCon: public IUnknown
{
public:
virtual HRESULT STDMETHODCALLTYPE GetName(
/* [in] */ __RPC__in ULONG *interface);
// ...

}


And I need to implement multiple instances of it. Unfortunately the loader that is looking for my interface is not finding my implementation of this interface.



I have done something like this:



// is this a proper forward reference?
[
object,
uuid(BCAC73A8-0226-4250-9D66-9656AA9BB86C), // is this uuid supposed to match the one in the header?
dual,
nonextensible,
pointer_default(unique)
]
interface IExternalCon: IUnknown
{
};


library ... {
[
uuid(d543911a-81b0-4de1-9511-d1f14caceed)
]

// the class implementing my interface
coclass ExternalConTest
{
[default] interface IExternalCon;
};
}


Any help, hints, or guidance on dealing with implementing interfaces just based on a header file would be appreciated.



Incidentally, my class is defined as follows:



class ATL_NO_VTABLE CExternalConTest :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CExternalConTest, &CLSID_ExternalConTest>,
public IExternalCon
{
public:
...

BEGIN_COM_MAP(CExternalConTest)
COM_INTERFACE_ENTRY(IExternalCon)
COM_INTERFACE_ENTRY(IUnknown)
END_COM_MAP()
..
}









share|improve this question



























    0















    This is probably a silly question and I hesitated to post, but I'm a newbie at ATL.



    I need to implement an interface in my code. I do not have a TLB or IDL for tha t interface, I just have a header file.



    The header file defines the interface as follows:



    EXTERN_C const IID IID_IExternalCon;
    ...

    MIDL_INTERFACE("BCAC73A8-0226-4250-9D66-9656AA9BB86C")
    IExternalCon: public IUnknown
    {
    public:
    virtual HRESULT STDMETHODCALLTYPE GetName(
    /* [in] */ __RPC__in ULONG *interface);
    // ...

    }


    And I need to implement multiple instances of it. Unfortunately the loader that is looking for my interface is not finding my implementation of this interface.



    I have done something like this:



    // is this a proper forward reference?
    [
    object,
    uuid(BCAC73A8-0226-4250-9D66-9656AA9BB86C), // is this uuid supposed to match the one in the header?
    dual,
    nonextensible,
    pointer_default(unique)
    ]
    interface IExternalCon: IUnknown
    {
    };


    library ... {
    [
    uuid(d543911a-81b0-4de1-9511-d1f14caceed)
    ]

    // the class implementing my interface
    coclass ExternalConTest
    {
    [default] interface IExternalCon;
    };
    }


    Any help, hints, or guidance on dealing with implementing interfaces just based on a header file would be appreciated.



    Incidentally, my class is defined as follows:



    class ATL_NO_VTABLE CExternalConTest :
    public CComObjectRootEx<CComSingleThreadModel>,
    public CComCoClass<CExternalConTest, &CLSID_ExternalConTest>,
    public IExternalCon
    {
    public:
    ...

    BEGIN_COM_MAP(CExternalConTest)
    COM_INTERFACE_ENTRY(IExternalCon)
    COM_INTERFACE_ENTRY(IUnknown)
    END_COM_MAP()
    ..
    }









    share|improve this question

























      0












      0








      0








      This is probably a silly question and I hesitated to post, but I'm a newbie at ATL.



      I need to implement an interface in my code. I do not have a TLB or IDL for tha t interface, I just have a header file.



      The header file defines the interface as follows:



      EXTERN_C const IID IID_IExternalCon;
      ...

      MIDL_INTERFACE("BCAC73A8-0226-4250-9D66-9656AA9BB86C")
      IExternalCon: public IUnknown
      {
      public:
      virtual HRESULT STDMETHODCALLTYPE GetName(
      /* [in] */ __RPC__in ULONG *interface);
      // ...

      }


      And I need to implement multiple instances of it. Unfortunately the loader that is looking for my interface is not finding my implementation of this interface.



      I have done something like this:



      // is this a proper forward reference?
      [
      object,
      uuid(BCAC73A8-0226-4250-9D66-9656AA9BB86C), // is this uuid supposed to match the one in the header?
      dual,
      nonextensible,
      pointer_default(unique)
      ]
      interface IExternalCon: IUnknown
      {
      };


      library ... {
      [
      uuid(d543911a-81b0-4de1-9511-d1f14caceed)
      ]

      // the class implementing my interface
      coclass ExternalConTest
      {
      [default] interface IExternalCon;
      };
      }


      Any help, hints, or guidance on dealing with implementing interfaces just based on a header file would be appreciated.



      Incidentally, my class is defined as follows:



      class ATL_NO_VTABLE CExternalConTest :
      public CComObjectRootEx<CComSingleThreadModel>,
      public CComCoClass<CExternalConTest, &CLSID_ExternalConTest>,
      public IExternalCon
      {
      public:
      ...

      BEGIN_COM_MAP(CExternalConTest)
      COM_INTERFACE_ENTRY(IExternalCon)
      COM_INTERFACE_ENTRY(IUnknown)
      END_COM_MAP()
      ..
      }









      share|improve this question














      This is probably a silly question and I hesitated to post, but I'm a newbie at ATL.



      I need to implement an interface in my code. I do not have a TLB or IDL for tha t interface, I just have a header file.



      The header file defines the interface as follows:



      EXTERN_C const IID IID_IExternalCon;
      ...

      MIDL_INTERFACE("BCAC73A8-0226-4250-9D66-9656AA9BB86C")
      IExternalCon: public IUnknown
      {
      public:
      virtual HRESULT STDMETHODCALLTYPE GetName(
      /* [in] */ __RPC__in ULONG *interface);
      // ...

      }


      And I need to implement multiple instances of it. Unfortunately the loader that is looking for my interface is not finding my implementation of this interface.



      I have done something like this:



      // is this a proper forward reference?
      [
      object,
      uuid(BCAC73A8-0226-4250-9D66-9656AA9BB86C), // is this uuid supposed to match the one in the header?
      dual,
      nonextensible,
      pointer_default(unique)
      ]
      interface IExternalCon: IUnknown
      {
      };


      library ... {
      [
      uuid(d543911a-81b0-4de1-9511-d1f14caceed)
      ]

      // the class implementing my interface
      coclass ExternalConTest
      {
      [default] interface IExternalCon;
      };
      }


      Any help, hints, or guidance on dealing with implementing interfaces just based on a header file would be appreciated.



      Incidentally, my class is defined as follows:



      class ATL_NO_VTABLE CExternalConTest :
      public CComObjectRootEx<CComSingleThreadModel>,
      public CComCoClass<CExternalConTest, &CLSID_ExternalConTest>,
      public IExternalCon
      {
      public:
      ...

      BEGIN_COM_MAP(CExternalConTest)
      COM_INTERFACE_ENTRY(IExternalCon)
      COM_INTERFACE_ENTRY(IUnknown)
      END_COM_MAP()
      ..
      }






      com atl






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 19 at 3:08









      Will I AmWill I Am

      1,23421842




      1,23421842
























          1 Answer
          1






          active

          oldest

          votes


















          1














          What you have to do to implement an external interface in an ATL object is




          • add the interface to the list of implemented interface (you did that). This will modify the class binary layout (vtable, etc.)

          • add the interface to BEGIN_COM_MAP list. This will instruct the ATL underlying QueryInterface implementation to answer positively for queries for that interface id (you did that). IUnknown is no necessary here.

          • add the interface declaration to the .h file. Linkage and annotation are optional there, I personally remove them. I also always add a remark with the interface name so we know what method is used by what interface. When you have a lot it's useful...

          • add the interface implementation to the .cpp file.


          So in your case it would be this for the .h file:



          class ATL_NO_VTABLE CExternalConTest :
          public CComObjectRootEx<CComSingleThreadModel>,
          public CComCoClass<CExternalConTest, &CLSID_ExternalConTest>,
          public IExternalCon // change the class layout
          {
          public:
          ...

          BEGIN_COM_MAP(CExternalConTest)
          COM_INTERFACE_ENTRY(IExternalCon) // answer to QueryInterface calls
          END_COM_MAP()
          ...

          public:
          // IExternalCon
          HRESULT GetName(ULONG *interface);
          ...
          // IWhatever
          HRESULT Blabla( ... );
          HRESULT Blabla2( ... );

          ...


          and this for the .cpp file:



          // CExternalConTest
          ...
          // IExternalCon
          HRESULT CExternalConTest::GetName(ULONG *interface)
          {
          // TODO : implement this
          return S_OK;
          }
          ...
          // IWhatever
          HRESULT CExternalConTest::Blabla(...)
          {
          // TODO : implement this
          return S_OK;
          }

          HRESULT CExternalConTest::Blabla2(...)
          {
          // TODO : implement this
          return S_OK;
          }


          Concerning the .idl, you don't have to do anything because Visual Studio uses it as the base for code generation. Usually, when using Visual Studio, you first change the .idl (using wizards or not) and then fill the gaps.






          share|improve this answer


























          • Thanks. This helps clarify things, but I'm still stuck on what to do with the IDL that the wizard generated. Since the interface is defined in code, how do I handle the library/coclass declaration in IDL (see my existing snippet in my question where it creates the ExternalConTest coclass and declares the interface.)

            – Will I Am
            Jan 19 at 22:44











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54263732%2fhow-do-i-implement-an-interface-given-a-header-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          What you have to do to implement an external interface in an ATL object is




          • add the interface to the list of implemented interface (you did that). This will modify the class binary layout (vtable, etc.)

          • add the interface to BEGIN_COM_MAP list. This will instruct the ATL underlying QueryInterface implementation to answer positively for queries for that interface id (you did that). IUnknown is no necessary here.

          • add the interface declaration to the .h file. Linkage and annotation are optional there, I personally remove them. I also always add a remark with the interface name so we know what method is used by what interface. When you have a lot it's useful...

          • add the interface implementation to the .cpp file.


          So in your case it would be this for the .h file:



          class ATL_NO_VTABLE CExternalConTest :
          public CComObjectRootEx<CComSingleThreadModel>,
          public CComCoClass<CExternalConTest, &CLSID_ExternalConTest>,
          public IExternalCon // change the class layout
          {
          public:
          ...

          BEGIN_COM_MAP(CExternalConTest)
          COM_INTERFACE_ENTRY(IExternalCon) // answer to QueryInterface calls
          END_COM_MAP()
          ...

          public:
          // IExternalCon
          HRESULT GetName(ULONG *interface);
          ...
          // IWhatever
          HRESULT Blabla( ... );
          HRESULT Blabla2( ... );

          ...


          and this for the .cpp file:



          // CExternalConTest
          ...
          // IExternalCon
          HRESULT CExternalConTest::GetName(ULONG *interface)
          {
          // TODO : implement this
          return S_OK;
          }
          ...
          // IWhatever
          HRESULT CExternalConTest::Blabla(...)
          {
          // TODO : implement this
          return S_OK;
          }

          HRESULT CExternalConTest::Blabla2(...)
          {
          // TODO : implement this
          return S_OK;
          }


          Concerning the .idl, you don't have to do anything because Visual Studio uses it as the base for code generation. Usually, when using Visual Studio, you first change the .idl (using wizards or not) and then fill the gaps.






          share|improve this answer


























          • Thanks. This helps clarify things, but I'm still stuck on what to do with the IDL that the wizard generated. Since the interface is defined in code, how do I handle the library/coclass declaration in IDL (see my existing snippet in my question where it creates the ExternalConTest coclass and declares the interface.)

            – Will I Am
            Jan 19 at 22:44
















          1














          What you have to do to implement an external interface in an ATL object is




          • add the interface to the list of implemented interface (you did that). This will modify the class binary layout (vtable, etc.)

          • add the interface to BEGIN_COM_MAP list. This will instruct the ATL underlying QueryInterface implementation to answer positively for queries for that interface id (you did that). IUnknown is no necessary here.

          • add the interface declaration to the .h file. Linkage and annotation are optional there, I personally remove them. I also always add a remark with the interface name so we know what method is used by what interface. When you have a lot it's useful...

          • add the interface implementation to the .cpp file.


          So in your case it would be this for the .h file:



          class ATL_NO_VTABLE CExternalConTest :
          public CComObjectRootEx<CComSingleThreadModel>,
          public CComCoClass<CExternalConTest, &CLSID_ExternalConTest>,
          public IExternalCon // change the class layout
          {
          public:
          ...

          BEGIN_COM_MAP(CExternalConTest)
          COM_INTERFACE_ENTRY(IExternalCon) // answer to QueryInterface calls
          END_COM_MAP()
          ...

          public:
          // IExternalCon
          HRESULT GetName(ULONG *interface);
          ...
          // IWhatever
          HRESULT Blabla( ... );
          HRESULT Blabla2( ... );

          ...


          and this for the .cpp file:



          // CExternalConTest
          ...
          // IExternalCon
          HRESULT CExternalConTest::GetName(ULONG *interface)
          {
          // TODO : implement this
          return S_OK;
          }
          ...
          // IWhatever
          HRESULT CExternalConTest::Blabla(...)
          {
          // TODO : implement this
          return S_OK;
          }

          HRESULT CExternalConTest::Blabla2(...)
          {
          // TODO : implement this
          return S_OK;
          }


          Concerning the .idl, you don't have to do anything because Visual Studio uses it as the base for code generation. Usually, when using Visual Studio, you first change the .idl (using wizards or not) and then fill the gaps.






          share|improve this answer


























          • Thanks. This helps clarify things, but I'm still stuck on what to do with the IDL that the wizard generated. Since the interface is defined in code, how do I handle the library/coclass declaration in IDL (see my existing snippet in my question where it creates the ExternalConTest coclass and declares the interface.)

            – Will I Am
            Jan 19 at 22:44














          1












          1








          1







          What you have to do to implement an external interface in an ATL object is




          • add the interface to the list of implemented interface (you did that). This will modify the class binary layout (vtable, etc.)

          • add the interface to BEGIN_COM_MAP list. This will instruct the ATL underlying QueryInterface implementation to answer positively for queries for that interface id (you did that). IUnknown is no necessary here.

          • add the interface declaration to the .h file. Linkage and annotation are optional there, I personally remove them. I also always add a remark with the interface name so we know what method is used by what interface. When you have a lot it's useful...

          • add the interface implementation to the .cpp file.


          So in your case it would be this for the .h file:



          class ATL_NO_VTABLE CExternalConTest :
          public CComObjectRootEx<CComSingleThreadModel>,
          public CComCoClass<CExternalConTest, &CLSID_ExternalConTest>,
          public IExternalCon // change the class layout
          {
          public:
          ...

          BEGIN_COM_MAP(CExternalConTest)
          COM_INTERFACE_ENTRY(IExternalCon) // answer to QueryInterface calls
          END_COM_MAP()
          ...

          public:
          // IExternalCon
          HRESULT GetName(ULONG *interface);
          ...
          // IWhatever
          HRESULT Blabla( ... );
          HRESULT Blabla2( ... );

          ...


          and this for the .cpp file:



          // CExternalConTest
          ...
          // IExternalCon
          HRESULT CExternalConTest::GetName(ULONG *interface)
          {
          // TODO : implement this
          return S_OK;
          }
          ...
          // IWhatever
          HRESULT CExternalConTest::Blabla(...)
          {
          // TODO : implement this
          return S_OK;
          }

          HRESULT CExternalConTest::Blabla2(...)
          {
          // TODO : implement this
          return S_OK;
          }


          Concerning the .idl, you don't have to do anything because Visual Studio uses it as the base for code generation. Usually, when using Visual Studio, you first change the .idl (using wizards or not) and then fill the gaps.






          share|improve this answer















          What you have to do to implement an external interface in an ATL object is




          • add the interface to the list of implemented interface (you did that). This will modify the class binary layout (vtable, etc.)

          • add the interface to BEGIN_COM_MAP list. This will instruct the ATL underlying QueryInterface implementation to answer positively for queries for that interface id (you did that). IUnknown is no necessary here.

          • add the interface declaration to the .h file. Linkage and annotation are optional there, I personally remove them. I also always add a remark with the interface name so we know what method is used by what interface. When you have a lot it's useful...

          • add the interface implementation to the .cpp file.


          So in your case it would be this for the .h file:



          class ATL_NO_VTABLE CExternalConTest :
          public CComObjectRootEx<CComSingleThreadModel>,
          public CComCoClass<CExternalConTest, &CLSID_ExternalConTest>,
          public IExternalCon // change the class layout
          {
          public:
          ...

          BEGIN_COM_MAP(CExternalConTest)
          COM_INTERFACE_ENTRY(IExternalCon) // answer to QueryInterface calls
          END_COM_MAP()
          ...

          public:
          // IExternalCon
          HRESULT GetName(ULONG *interface);
          ...
          // IWhatever
          HRESULT Blabla( ... );
          HRESULT Blabla2( ... );

          ...


          and this for the .cpp file:



          // CExternalConTest
          ...
          // IExternalCon
          HRESULT CExternalConTest::GetName(ULONG *interface)
          {
          // TODO : implement this
          return S_OK;
          }
          ...
          // IWhatever
          HRESULT CExternalConTest::Blabla(...)
          {
          // TODO : implement this
          return S_OK;
          }

          HRESULT CExternalConTest::Blabla2(...)
          {
          // TODO : implement this
          return S_OK;
          }


          Concerning the .idl, you don't have to do anything because Visual Studio uses it as the base for code generation. Usually, when using Visual Studio, you first change the .idl (using wizards or not) and then fill the gaps.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 20 at 8:09

























          answered Jan 19 at 11:44









          Simon MourierSimon Mourier

          97.8k12178228




          97.8k12178228













          • Thanks. This helps clarify things, but I'm still stuck on what to do with the IDL that the wizard generated. Since the interface is defined in code, how do I handle the library/coclass declaration in IDL (see my existing snippet in my question where it creates the ExternalConTest coclass and declares the interface.)

            – Will I Am
            Jan 19 at 22:44



















          • Thanks. This helps clarify things, but I'm still stuck on what to do with the IDL that the wizard generated. Since the interface is defined in code, how do I handle the library/coclass declaration in IDL (see my existing snippet in my question where it creates the ExternalConTest coclass and declares the interface.)

            – Will I Am
            Jan 19 at 22:44

















          Thanks. This helps clarify things, but I'm still stuck on what to do with the IDL that the wizard generated. Since the interface is defined in code, how do I handle the library/coclass declaration in IDL (see my existing snippet in my question where it creates the ExternalConTest coclass and declares the interface.)

          – Will I Am
          Jan 19 at 22:44





          Thanks. This helps clarify things, but I'm still stuck on what to do with the IDL that the wizard generated. Since the interface is defined in code, how do I handle the library/coclass declaration in IDL (see my existing snippet in my question where it creates the ExternalConTest coclass and declares the interface.)

          – Will I Am
          Jan 19 at 22:44


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54263732%2fhow-do-i-implement-an-interface-given-a-header-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Homophylophilia

          Updating UILabel text programmatically using a function

          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage