none
ATL RRS feed

  • Question

  • Using Visual Studio .Net 2003. I created a DLL ATL Project using the New Proj. Wizard. Noticed that the generated class is declared and defined solely in the .cpp file, i.e. there is no header.

    Three questions:

    1. From this, I gather that the methods of the class are to be implemented in the class itself? I like this because I like defining all class functionality in one file.

    2. Also, is no header is needed for ATL COM objects because the importer of the object gets the interface specification from the IDL file (as opposed to the analogous header file as would be used for a standard DLL)?

    3. This seems to have some commonality with C# strategy. Does C# and .NET build on and/or draw from COM and ATL technology?

    Thank You
    Monday, October 10, 2005 7:13 AM

Answers

  • 1. It depends on what you need and want. It is possible to move any class definition file from a header into a cpp file if you do not need in in other c++ source modules.

    2. From the idl file itself usually a include file is created. Either bei using the #import or by using the #include statement. It is needed to get the interface definitions.

    3. No I think the .NET strategy was a complete new archtiecture that borowed nothing from the old technologies except that it should do the job better than COM. Also you can not compare ATL and COM/NET.

    Just my 2 cents.
    Monday, October 10, 2005 8:27 AM
    Moderator
  • The generated class should have been declared in header and implemented in .cpp.

    .h


    class CSomeClass : public ISomeIf
    {
    // ... declarations
         virtual HRESULT STDMETHODCALLTYPE Foo();
    };

     


    .cpp


    HRESULT STDMETHODCALLTYPE CSomeClass::Foo()
    {
    }

     



    What you like is not recommended, actually. Why?
    1. You do not hide information. If someone needs that header someday, you give all implementation.
    2. Impelementation in header file may require #include'ing other header files for type definitions.
    3. In the 2nd case, if someone slightly changes any of #include'd files, compiler recompiles a number of headers due to complex and redundant header dependency.
    4. In the 2nd case, you cannot solve a cyclic dependency, where, say, class A defined in a.h needs class B defined in b.h and both A and B needs each other. In this case, you need to move declarations out of either of files, place somewhere else and use it. This is not reasonable, I think.

    2. Also, is no header is needed for ATL COM objects because the importer of the object gets the interface specification from the IDL file (as opposed to the analogous header file as would be used for a standard DLL)?

    No. It's not an ATL-specific issue. You need to embed your typelibrary into DLL's resources and you are done. Any client can consume it. Whether your DLL is written without ATL or MFC or another library; pure Win32 and C++, it works as long as it has got typelib.

    3. This seems to have some commonality with C# strategy. Does C# and .NET build on and/or draw from COM and ATL technology?

    Question sounds a bit irrelevant, sorry. Unfortunately, neither CLR nor C# love or follow ATL. I guess I shouldn't talk against .NET on microsoft.com. But I personally do not find any use of .NET. But people (forced to) love it! So I have had to join the big portrait of "smiling .NET developers" Big Smile
    Monday, October 10, 2005 11:12 AM
  • Hello Ismail. Thank you for the answer. Please see my response to Martin above as it applies in general.
    Monday, October 10, 2005 8:18 PM
  • 1. Ok. You need to add object. Right-click on project, click Add, click Add New Class. Choose object type you need. Usually, you need ATL Simple Object. Then fill the wizard.

    2. Yes. But ATL wizard will generate a .h for you. It's inevitable, AFAIK.

    It's one of the purposes of COM - reusing binary, not code.

    Yes, you are on the right track :)

    I didn't use ATL to build COM DLLs too much. Rolling your own idl, class factory is nicer. I am not an ATL guru. I think you can find more answers in news.microsoft.com, in microsoft.public.vc.atl newsgroup.

    Ismail
    Monday, October 10, 2005 11:56 PM

All replies

  • 1. It depends on what you need and want. It is possible to move any class definition file from a header into a cpp file if you do not need in in other c++ source modules.

    2. From the idl file itself usually a include file is created. Either bei using the #import or by using the #include statement. It is needed to get the interface definitions.

    3. No I think the .NET strategy was a complete new archtiecture that borowed nothing from the old technologies except that it should do the job better than COM. Also you can not compare ATL and COM/NET.

    Just my 2 cents.
    Monday, October 10, 2005 8:27 AM
    Moderator
  • The generated class should have been declared in header and implemented in .cpp.

    .h


    class CSomeClass : public ISomeIf
    {
    // ... declarations
         virtual HRESULT STDMETHODCALLTYPE Foo();
    };

     


    .cpp


    HRESULT STDMETHODCALLTYPE CSomeClass::Foo()
    {
    }

     



    What you like is not recommended, actually. Why?
    1. You do not hide information. If someone needs that header someday, you give all implementation.
    2. Impelementation in header file may require #include'ing other header files for type definitions.
    3. In the 2nd case, if someone slightly changes any of #include'd files, compiler recompiles a number of headers due to complex and redundant header dependency.
    4. In the 2nd case, you cannot solve a cyclic dependency, where, say, class A defined in a.h needs class B defined in b.h and both A and B needs each other. In this case, you need to move declarations out of either of files, place somewhere else and use it. This is not reasonable, I think.

    2. Also, is no header is needed for ATL COM objects because the importer of the object gets the interface specification from the IDL file (as opposed to the analogous header file as would be used for a standard DLL)?

    No. It's not an ATL-specific issue. You need to embed your typelibrary into DLL's resources and you are done. Any client can consume it. Whether your DLL is written without ATL or MFC or another library; pure Win32 and C++, it works as long as it has got typelib.

    3. This seems to have some commonality with C# strategy. Does C# and .NET build on and/or draw from COM and ATL technology?

    Question sounds a bit irrelevant, sorry. Unfortunately, neither CLR nor C# love or follow ATL. I guess I shouldn't talk against .NET on microsoft.com. But I personally do not find any use of .NET. But people (forced to) love it! So I have had to join the big portrait of "smiling .NET developers" Big Smile
    Monday, October 10, 2005 11:12 AM
  • 1. When I created a basic ATL Project in Visual Sutdio .NET 2003 called "Test_ATL", the Wizard generated class CTest_ATLModule in Test_ATL.cpp . There was no header file.

    2. Can I use the cpp file for class method code and expose the interface using another means than a header file? Does the .tlb file come into play? For example, I am able to add a Reference to Test_ATL to an MFC project and it shows up in the References section of the MFC project in Solution Explorer. By clicking on the Test_ATL Reference there, I can browse in the Object Browser and the Test_ATL methods show up - so apparently, I can expose and use the interface without the need of a header file.

    My goal in using COM (among other purposes) is to be able to control what I expose as an interface without revealing the functionality to the COM object's client. If this also means I can define the class in the cpp file because a header is not needed (as would be for a standard DLL) to expose the COM interface then that's a plus.

    You see, how I got here is this. I created a class and exported it from a DLL. What I don't like is that the client of the DLL needs the header file for import which also shows some of the inner workings such as member variables. So, I thought COM would be a great way to expose a more pure interface based only on what I want the COM object's client to use. It seems that COM could allow exposure of only those parts of a class/object that are needed and other methods/properties/variables could e part of the class but not exposed. Am I on the right track?

    I hope this clarifies.  
    Monday, October 10, 2005 8:14 PM
  • Hello Ismail. Thank you for the answer. Please see my response to Martin above as it applies in general.
    Monday, October 10, 2005 8:18 PM
  • 1. Ok. You need to add object. Right-click on project, click Add, click Add New Class. Choose object type you need. Usually, you need ATL Simple Object. Then fill the wizard.

    2. Yes. But ATL wizard will generate a .h for you. It's inevitable, AFAIK.

    It's one of the purposes of COM - reusing binary, not code.

    Yes, you are on the right track :)

    I didn't use ATL to build COM DLLs too much. Rolling your own idl, class factory is nicer. I am not an ATL guru. I think you can find more answers in news.microsoft.com, in microsoft.public.vc.atl newsgroup.

    Ismail
    Monday, October 10, 2005 11:56 PM