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. Using a COM Server in an Visual Studio .vcproj

Using a COM Server in an Visual Studio .vcproj

Scheduled Pinned Locked Moved COM
tutorialc++questioncsharpvisual-studio
9 Posts 3 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
    Juergen_80
    wrote on last edited by
    #1

    I have (I think for some of you) a simple question. I have an .vcproj project (no ATL support and no MFC support) and a I have an .exe file. Within this .exe file should be an COM Server that runs on WinVista. Now I need an connection from my .vcproj project to that COM server. Is there anywhere an step by step tutorial with that I can do such something? I’ve heard in several forums that it is the easiest way to create an ATL project and implement this .exe file. This works also if I create a new ATL project. A new .h file will be created with all the Methods and so on out of the .exe file. But what should I do with an non ATL/MFC supported project? Is there also a way to work with such an project and an COM server? The main problem I have is that I need to work later with some events of the COM server, and this should be also very complicated with no ATL support? It would be nice if someone could send me some information (Step by Step tutorial / simple example how events could be called and so on). I’ve found some things but no complete running example with an event handling. Thanks for any hints. System: WinVista, Visual Studio 2005

    S V 2 Replies Last reply
    0
    • J Juergen_80

      I have (I think for some of you) a simple question. I have an .vcproj project (no ATL support and no MFC support) and a I have an .exe file. Within this .exe file should be an COM Server that runs on WinVista. Now I need an connection from my .vcproj project to that COM server. Is there anywhere an step by step tutorial with that I can do such something? I’ve heard in several forums that it is the easiest way to create an ATL project and implement this .exe file. This works also if I create a new ATL project. A new .h file will be created with all the Methods and so on out of the .exe file. But what should I do with an non ATL/MFC supported project? Is there also a way to work with such an project and an COM server? The main problem I have is that I need to work later with some events of the COM server, and this should be also very complicated with no ATL support? It would be nice if someone could send me some information (Step by Step tutorial / simple example how events could be called and so on). I’ve found some things but no complete running example with an event handling. Thanks for any hints. System: WinVista, Visual Studio 2005

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Just add the ATL header files in - that's all you need - that's all that "ATL Support" actually means. Doing stuff with COM without ATL is just too much like hard work. You remember that Excel connection point sample of mine I linked to? That consists of a single .cpp file, whose 'ATL Support' consists of these three lines:

      #include <atlbase.h>
      #include <atlstr.h>
      #include <atlcom.h>

      And the atlstr.h isn't needed for COM.

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      modified on Thursday, September 10, 2009 11:35 AM

      J 1 Reply Last reply
      0
      • S Stuart Dootson

        Just add the ATL header files in - that's all you need - that's all that "ATL Support" actually means. Doing stuff with COM without ATL is just too much like hard work. You remember that Excel connection point sample of mine I linked to? That consists of a single .cpp file, whose 'ATL Support' consists of these three lines:

        #include <atlbase.h>
        #include <atlstr.h>
        #include <atlcom.h>

        And the atlstr.h isn't needed for COM.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        modified on Thursday, September 10, 2009 11:35 AM

        J Offline
        J Offline
        Juergen_80
        wrote on last edited by
        #3

        Thanks for your help, Stuart. I try now to implement MFC + ATL support and then I can implement all the COM classes out of the .exe file directly with Visual Studio. Regards, Jürgen

        S 1 Reply Last reply
        0
        • J Juergen_80

          Thanks for your help, Stuart. I try now to implement MFC + ATL support and then I can implement all the COM classes out of the .exe file directly with Visual Studio. Regards, Jürgen

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          Ignore MFC - you don't need it for a simple connection point sink.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          J 1 Reply Last reply
          0
          • S Stuart Dootson

            Ignore MFC - you don't need it for a simple connection point sink.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            J Offline
            J Offline
            Juergen_80
            wrote on last edited by
            #5

            ok, thanks. Now I made the following steps in Visual Studio: -> Add Class --->Simple ATL-Object with Connection Points. With that the ATL support was added to my project. After that the project could be compiled without any problem. Now I click with the right mouse on the new class I've created with the ATL OBject and choose "Implement interface". With that I can now navigate to the .exe file and implement all the interfaces I need. And now after that I got the following message after I tried to compile it: Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\atlcom.h(1868) : error C2259: 'ATL::CComObject<Base>': Instance of abstract class could not be built1> with 1> [ 1> Base=CEGR_COM 1> ] 1> because of the following member: 1> "HRESULT ICOLE::cSetLanguage(BSTR,VARIANT_BOOL *)": is abstract 1> c:\dev\cw_source\vitra scene\release\configurator.tlh(605): Siehe Deklaration von 'ICOLE::cSetLanguage' 1> "HRESULT ICOLE::cSetDefaultCurrency(BSTR,VARIANT_BOOL *)": ist abstract . . . . . Ok, all these function bodys are now in an TEST_COM.h and in the configurator.tlh file. The TEST_COM.cpp is empty. I don't know why this error message now will come up. The error message will come only for a few of the functions in the .tlh file, not for all. . . . virtual HRESULT __stdcall get_DimensionZ ( /*[out,retval]*/ double * Value ) = 0; virtual HRESULT __stdcall put_DimensionZ ( /*[in]*/ double Value ) = 0; virtual HRESULT __stdcall get_ErrorCode ( /*[out,retval]*/ short * Value ) = 0; virtual HRESULT __stdcall put_ErrorCode ( /*[in]*/ short Value ) = 0; virtual HRESULT __stdcall cInitializeProperties ( ) = 0; // All the functions below will cause an error message virtual HRESULT __stdcall cSetLanguage ( /*[in]*/ BSTR pLanguage, /*[out,retval]*/ VARIANT_BOOL * Result ) = 0; virtual HRESULT __stdcall cSetDefaultCurrency ( /*[in]*/ BSTR pCurrency, /*[out,retval]*/ VARIANT_BOOL * Result ) = 0; virtual HRESULT __stdcall cShowCatalogChooser ( /*[out,retval]*/ VARIANT_BOOL * Result ) = 0; . . . Any ideas?

            S 1 Reply Last reply
            0
            • J Juergen_80

              ok, thanks. Now I made the following steps in Visual Studio: -> Add Class --->Simple ATL-Object with Connection Points. With that the ATL support was added to my project. After that the project could be compiled without any problem. Now I click with the right mouse on the new class I've created with the ATL OBject and choose "Implement interface". With that I can now navigate to the .exe file and implement all the interfaces I need. And now after that I got the following message after I tried to compile it: Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\atlcom.h(1868) : error C2259: 'ATL::CComObject<Base>': Instance of abstract class could not be built1> with 1> [ 1> Base=CEGR_COM 1> ] 1> because of the following member: 1> "HRESULT ICOLE::cSetLanguage(BSTR,VARIANT_BOOL *)": is abstract 1> c:\dev\cw_source\vitra scene\release\configurator.tlh(605): Siehe Deklaration von 'ICOLE::cSetLanguage' 1> "HRESULT ICOLE::cSetDefaultCurrency(BSTR,VARIANT_BOOL *)": ist abstract . . . . . Ok, all these function bodys are now in an TEST_COM.h and in the configurator.tlh file. The TEST_COM.cpp is empty. I don't know why this error message now will come up. The error message will come only for a few of the functions in the .tlh file, not for all. . . . virtual HRESULT __stdcall get_DimensionZ ( /*[out,retval]*/ double * Value ) = 0; virtual HRESULT __stdcall put_DimensionZ ( /*[in]*/ double Value ) = 0; virtual HRESULT __stdcall get_ErrorCode ( /*[out,retval]*/ short * Value ) = 0; virtual HRESULT __stdcall put_ErrorCode ( /*[in]*/ short Value ) = 0; virtual HRESULT __stdcall cInitializeProperties ( ) = 0; // All the functions below will cause an error message virtual HRESULT __stdcall cSetLanguage ( /*[in]*/ BSTR pLanguage, /*[out,retval]*/ VARIANT_BOOL * Result ) = 0; virtual HRESULT __stdcall cSetDefaultCurrency ( /*[in]*/ BSTR pCurrency, /*[out,retval]*/ VARIANT_BOOL * Result ) = 0; virtual HRESULT __stdcall cShowCatalogChooser ( /*[out,retval]*/ VARIANT_BOOL * Result ) = 0; . . . Any ideas?

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              I don't think you've gone around this quite right - I thought you wanted to catch events raised by the COM server in the EXE file? If this understanding is correct, then you don't need to re-implement the interfaces exposed by the EXE's COM server to do that - instead, you just need to instantiate the COM object implemented in the EXE and implement a connection point sink that you link to the instantiated object. Please let me know if I've understood your situation correctly (although I'll be mostly off-line over the weekend).

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              J 1 Reply Last reply
              0
              • J Juergen_80

                I have (I think for some of you) a simple question. I have an .vcproj project (no ATL support and no MFC support) and a I have an .exe file. Within this .exe file should be an COM Server that runs on WinVista. Now I need an connection from my .vcproj project to that COM server. Is there anywhere an step by step tutorial with that I can do such something? I’ve heard in several forums that it is the easiest way to create an ATL project and implement this .exe file. This works also if I create a new ATL project. A new .h file will be created with all the Methods and so on out of the .exe file. But what should I do with an non ATL/MFC supported project? Is there also a way to work with such an project and an COM server? The main problem I have is that I need to work later with some events of the COM server, and this should be also very complicated with no ATL support? It would be nice if someone could send me some information (Step by Step tutorial / simple example how events could be called and so on). I’ve found some things but no complete running example with an event handling. Thanks for any hints. System: WinVista, Visual Studio 2005

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

                Juergen_80 wrote:

                Now I need an connection from my .vcproj project to that COM server. Is there anywhere an step by step tutorial with that I can do such something?

                I think that "#import directive" is only what you need if you are simply a client of this COM exe server. Also don't forget about CoInitialize/CoInitializeEx functions before using COM objects and CoUninitialize after.

                With best wishes, Vita

                1 Reply Last reply
                0
                • S Stuart Dootson

                  I don't think you've gone around this quite right - I thought you wanted to catch events raised by the COM server in the EXE file? If this understanding is correct, then you don't need to re-implement the interfaces exposed by the EXE's COM server to do that - instead, you just need to instantiate the COM object implemented in the EXE and implement a connection point sink that you link to the instantiated object. Please let me know if I've understood your situation correctly (although I'll be mostly off-line over the weekend).

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  J Offline
                  J Offline
                  Juergen_80
                  wrote on last edited by
                  #8

                  Yes, you understand it correct. I want to catch events from this server in the .exe file. At first I worked with an .c / .h / .idl file that have been created from the .exe server. With these I could create an ComPtr and I could call some methods with this pointer. This step works in my old project: CComPtr tCOLE; hr = CoInitialize(0); hr = CoCreateInstance(CLSID_COLEv2, 0, CLSCTX_LOCAL_SERVER, IID_ICOLE2, (void**)&tCOLE); tCole->.... //here I could call the methods out of the ICOLE2. This worked also perfect. Now I had the problem that I must catch also some events from the server. And here I didn't know how I should go on. Some people on different forums called me to work with atl because this should be easier. So now I tried to do that all with ATL. The main problem now is, that I didn't exactly know what I must do. I didn't found anywhere an example / tutorial that explains everything step by step.

                  S 1 Reply Last reply
                  0
                  • J Juergen_80

                    Yes, you understand it correct. I want to catch events from this server in the .exe file. At first I worked with an .c / .h / .idl file that have been created from the .exe server. With these I could create an ComPtr and I could call some methods with this pointer. This step works in my old project: CComPtr tCOLE; hr = CoInitialize(0); hr = CoCreateInstance(CLSID_COLEv2, 0, CLSCTX_LOCAL_SERVER, IID_ICOLE2, (void**)&tCOLE); tCole->.... //here I could call the methods out of the ICOLE2. This worked also perfect. Now I had the problem that I must catch also some events from the server. And here I didn't know how I should go on. Some people on different forums called me to work with atl because this should be easier. So now I tried to do that all with ATL. The main problem now is, that I didn't exactly know what I must do. I didn't found anywhere an example / tutorial that explains everything step by step.

                    S Offline
                    S Offline
                    Stuart Dootson
                    wrote on last edited by
                    #9

                    Here's a full example using ATL and #import - the comments should be informative, I hope :-) It references the Excel COM server, obviously running in a separate process. It tells Excel it wants to catch application-specific events (as defined by the interface AppEvents), by telling it what object to send the events to.

                    #include <iostream>
                    #include <atlcom.h>

                    // Reference the COM server - you can put the .exe's path in the string instead
                    // of the libid.
                    #import "libid:00020813-0000-0000-C000-000000000046" version("1.6") auto_search no_dual_interfaces raw_dispinterfaces rename("DialogBox", "excelDialogBox") rename("RGB", "excelRGB") rename("DocumentProperties", "excelDocumentProperties") rename("SearchPath", "excelSearchPath") rename("CopyFile", "excelCopyFile") rename("ReplaceText", "excelReplaceText")

                    // This defines the signature of the event I want to handle
                    _ATL_FUNC_INFO SheetChangeInfo = { CC_CDECL, VT_EMPTY, 2, { VT_DISPATCH, VT_DISPATCH } };

                    // This is the connection point sink class. The event interface is
                    // Excel::AppEvents
                    class ExcelAppEventHandler : public IDispEventSimpleImpl<1, ExcelAppEventHandler, &__uuidof(Excel::AppEvents)>
                    {
                    public:
                    ExcelAppEventHandler(bool& doneFlag) : done_(doneFlag) { done_ = false; }
                    BEGIN_SINK_MAP(ExcelAppEventHandler)
                    SINK_ENTRY_INFO(1, __uuidof(Excel::AppEvents), 0x0000061c, &ExcelAppEventHandler::SheetChange, &SheetChangeInfo)
                    END_SINK_MAP()

                    void _stdcall SheetChange(IDispatch * Sh, struct Excel::Range * Target)
                    {
                    done_ = true;
                    }
                    private:
                    bool& done_;
                    };

                    // This function uses the connection point sink class
                    _bstr_t GetActiveWorkbookName(Excel::_ApplicationPtr xl)
                    {
                    if (Excel::_WorkbookPtr wb = xl->ActiveWorkbook)
                    {
                    try
                    {
                    return wb->FullName;
                    }
                    catch(_com_error& e)
                    {
                    std::cout << "EXCEPTION!!!\n";
                    std::cerr << CT2CA(e.ErrorMessage()) << std::endl;

                         bool done;
                         // Instantiate the connection point sink
                         ExcelAppEventHandler app(done);
                         // Tell the COM server that you want to catch events
                         if (SUCCEEDED(app.DispEventAdvise(xl)))
                         {
                            // Put in a message loop to allow COM to work - we're waiting
                            // until an event's been received (indicated by done)
                            MSG msg;
                            while (!done && GetMessage(&msg, NULL, 0, 0) > 0) {
                               TranslateMessage(&a
                    
                    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