none
How to create COM object in ATL Service in vc6 RRS feed

  • Question

  • Hello every one ;

    I have created one COM Dll using ATL in which their are ten classes. Now i want to use these COM dll in the ATL service and want to create an object of the classes which resides in my COM dll . So please tell me how to instantiate or create COM class objects in ATL Service.

    Thanks in Advance.
    Logic is my Greatest Asset !
    Monday, January 11, 2010 10:38 AM

Answers

  • The additional information is helpful.

    Stick with one compiler, in this case VS2008. I suggest building a very simple basic service. There is a code project article that you might want to look at. I'm sure you can find additional articles if you Google/Bing.

    Then proceed in the manner I described in a earlier post. Make use of the #import directive to create a tlh file. Make use of the smart pointers to instantiate and call into the COM object.

    Good luck with your project.
    • Marked as answer by Wesley Yao Monday, January 18, 2010 3:25 AM
    Thursday, January 14, 2010 4:50 PM

All replies

  • In my view the best approach is to convert your COM DLL to an ATL Service. Create an ATL Service from scratch using the wizard and then copy/paste the appropriate code from the COM DLL. Even with 10 classes it probably will only take an hour or two, longer than you have been waiting for a response.
    Monday, January 11, 2010 4:46 PM
  • HI Brian Muth

    Thanks for reply. But why you  suggest me to convert the COM dll to ATL Service?
    Is it not possible to use COM dll ( its Interfaces or classes) in the ATL service in VC6 , VC++.NET(VS2008)?
    Logic is my Greatest Asset !
    Wednesday, January 13, 2010 4:25 AM

  • Is it not possible to use COM dll ( its Interfaces or classes) in the ATL service in VC6 , VC++.NET(VS2008)?

    Indeed it is possible. I was just trying to save you a few steps, since creating a COM class inside an ATL service is performed in exactly the same manner as creating an ATL DLL.

    If your COM dll is already defined elsewhere and you want to instantiate it in the ATL service, I would use the #import directive. This creates a *.tlh file in the Debug/Release folder. It will create smart pointers that you can use to help with the instantiation of the COM object. For example, if the COM dll defines an interface called IMyInterface, then you can use:

    #import MyComObject.dll no_namespace
    ...
    IMyInterfacePtr p;
    HRESULT hr = p.CreateInstance (__uuidof (MyComObject));

    where MyComObject is the name of the class (check the contents of the tlh file)
    Wednesday, January 13, 2010 4:39 AM

  • Indeed it is possible. I was just trying to save you a few steps, since creating a COM class inside an ATL service is performed in exactly the same manner as creating an ATL DLL.

    If your COM dll is already defined elsewhere and you want to instantiate it in the ATL service, I would use the #import directive. This creates a *.tlh file in the Debug/Release folder. It will create smart pointers that you can use to help with the instantiation of the COM object. For example, if the COM dll defines an interface called IMyInterface, then you can use:

    #import MyComObject.dll no_namespace
    ...
    IMyInterfacePtr p;
    HRESULT hr = p.CreateInstance (__uuidof (MyComObject ));

    where MyComObject is the name of the class (check the contents of the tlh file)
    Hi Thanks for quick reply.
    I my COM dll their is one main interface say MyInterface.
    I just want to create the instance of that interface. and then this interface returns the another interfaces.
    For example , i have implemented an method on MyInterface as GetMyInterface2(). this returns the IDispatch**
    to the client. these returned object i want to assign to the MyInterface2 object.
    I have tried your solution but still it gives me an error like can't start service on local computer , your service not responding in timely fashion like that something.
    Please help me.
    Thanks in advance.


    Logic is my Greatest Asset !
    Wednesday, January 13, 2010 5:02 AM

  • I my COM dll their is one main interface say MyInterface.
    I just want to create the instance of that interface. and then this interface returns the another interfaces.
    For example , i have implemented an method on MyInterface as GetMyInterface2(). this returns the IDispatch**
    to the client. these returned object i want to assign to the MyInterface2 object.
    I have tried your solution but still it gives me an error like can't start service on local computer , your service not responding in timely fashion like that something.
    Please help me.
    Thanks in advance.
    Well, your goal is achievable. But you have a bug. So, you have to do what every C++ programmer is familiar with: you have to debug your program.

    If you get stuck, then I suggest starting over with a basic ATL service generated by the wizard. Compile and make sure it runs. Then slowly repopulate it with your code compiling and confirming correct functionality as you go. When it stops working, you know you have done something wrong (and you know where to look).

    There is no other advice one can offer given the dearth of information.

    Wednesday, January 13, 2010 5:09 AM
  • Just initializing the COM object as in other clients, call CoCreateInstance() in the Run() and release it at the end of the message loop.  Have you tried that?

    Sincerely,
    Wesley


    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, January 13, 2010 7:09 AM
  • Just initializing the COM object as in other clients, call CoCreateInstance() in the Run() and release it at the end of the message loop.  Have you tried that?

    Sincerely,
    Wesley


    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    hi , when i use CLSID of my class ( interface ) like CLSID_MyClass in the ::CoCreateInstance(..) then it gives me an error undeclared indentifier.

    thanks.

    Logic is my Greatest Asset !
    Wednesday, January 13, 2010 7:46 AM
  • Hi,

    Have you registered the DLL with regsvr32.exe, and also imported its type library as Brian told?
    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, January 13, 2010 8:09 AM
  • Hi,

    Have you registered the DLL with regsvr32.exe, and also imported its type library as Brian told?
    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    Yes i have Registered the DLL with RegSvr32.exe and also imported its type libarary., but still NOT Working.
    One more thing to tell is that my COM DLL was developed using VC++.NET (VS2008) and now i am writing the ATL service in VC6. so please tell me is this wrong or not ?

    Logic is my Greatest Asset !
    Wednesday, January 13, 2010 9:45 AM
  • Please try Brian's method:

    IMyInterfacePtr p;
    HRESULT hr = p.CreateInstance (__uuidof (MyComObject));

    Use __uuidof to get the CLSID, .tlh and .tli will not contain the definition of CLSID as the format CLSID_MyClass, because they may be included in many .cpp files and the multiple definitions will cause error.

    Else you can define it manually:
    CLSID CLSID_MyClass = {<GUID of MyClass>};

    Sincerely,
    Wesley
    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, January 13, 2010 11:20 AM
  • You may find it useful to write a simple C++ executable rather than a service, to instantiate the COM object in the manner described earlier in this thread. It's simpler for debugging services. Once you have successfully instantiated the object and tested one of the method calls, its a simple matter to replicate the code inside the service (which is trickier to debug).
    Wednesday, January 13, 2010 5:05 PM
  • You may find it useful to write a simple C++ executable rather than a service, to instantiate the COM object in the manner described earlier in this thread. It's simpler for debugging services. Once you have successfully instantiated the object and tested one of the method calls, its a simple matter to replicate the code inside the service (which is trickier to debug).

    Hello sir , I agree with you. I have done it before starting to write the service.
    I have written an C++ console application as i required, and it WORKS!
    Now simply i am copying the code from console application to service application, but still giving me problem.
    I have some questions:

    1. Is ATL service compulsory required one class or interface to start its working.
       (  In my service application i am not including any class or interface. my service only uses another COM dll that already registered.)

    2.is this possible to use COM dll developed in .NET (VS2008) in the VC6 ATL service application.



    Logic is my Greatest Asset !
    Thursday, January 14, 2010 4:05 AM

  • Hello sir , I agree with you. I have done it before starting to write the service.
    I have written an C++ console application as i required, and it WORKS!
    Now simply i am copying the code from console application to service application, but still giving me problem.
    I have some questions:

    1. Is ATL service compulsory required one class or interface to start its working.
       (  In my service application i am not including any class or interface. my service only uses another COM dll that already registered.)
    No it's not compulsory, although it begs the question why you would choose to create an ATL service in the first place. Why not just create a regular service?

    2.is this possible to use COM dll developed in .NET (VS2008) in the VC6 ATL service application.




    I see no reason why not, other than I would recommend that you statically link the COM dll to avoid problems with two runtime DLL's.

    Clearly you have chosen a very unconventional design for no reason that is apparent to this thread. I suspect that the problem may be related to something about this design or how you have assembled the pieces.

    Can you explain to me why you don't simply build a ATL service using the wizard and add an ATL simple object that implements the code that is currently implemented in your existing COM dll? Or at least stick with one version of the compiler for all the pieces of this executable?
    Thursday, January 14, 2010 4:26 AM
  • Can you explain to me why you don't simply build a ATL service using the wizard and add an ATL simple object that implements the code that is currently implemented in your existing COM dll? Or at least stick with one version of the compiler for all the pieces of this executable?
         No sir i cant do that because my COM dll code was to big and it will take long to copy and paste.
        I also want to stick with one version of compiler but i don't know any information about how to write
       ATL service in VS2008.Will you please tell me which functions i have to override in my service application
    in ATL service. i will tell you what i want to do in my service application is that
              i have implemented an code that reads parameters from the device connected to the computer. i just want to set the timers for different devices during initialization of service or during run  time but it should happen only ones not again, means only on start up.and for this purpose i am creating the COM object of my COM dll class.
    So please tell where should i implement  this code?  

    Logic is my Greatest Asset !
    Thursday, January 14, 2010 4:45 AM
  • The additional information is helpful.

    Stick with one compiler, in this case VS2008. I suggest building a very simple basic service. There is a code project article that you might want to look at. I'm sure you can find additional articles if you Google/Bing.

    Then proceed in the manner I described in a earlier post. Make use of the #import directive to create a tlh file. Make use of the smart pointers to instantiate and call into the COM object.

    Good luck with your project.
    • Marked as answer by Wesley Yao Monday, January 18, 2010 3:25 AM
    Thursday, January 14, 2010 4:50 PM