CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2000
    Location
    Ottawa, Ontario
    Posts
    356

    Howto pass COM object as param in event

    How do you instance a new COM object and pass it as a parameter in an event. For example...

    Code:
    // Event declare in IDL
    [id(1), helpstring("method GotServer")] HRESULT GotServer(IServerInfo** pServer);
    
    // ------------- My code to attempt to instance a ServerInfo object and fire and event passing it ------------
    
    CComPtr< IServerInfo > pNewServer;
    HRESULT hr = pNewServer.CoCreateInstance(__uuidof(ServerInfo));
    if(SUCCEEDED(hr))
    {
          // Populate COM object properties here.....
          // Now Fire Event.
          Fire_GotServer(pNewServer);   <- This doesn't work.
    }
    Error message

    error C2664: 'Fire_GotServer' : cannot convert parameter 1 from 'class ATL::CComPtr<struct IServerInfo>' to 'struct IServerInfo ** '

  2. #2
    Join Date
    Feb 2005
    Location
    Pasadena, MD, USA
    Posts
    105

    Re: Howto pass COM object as param in event

    Hi,

    Code:
    IServerInfo* pNewServer = NULL;
    HRESULT hr = pNewServer->CoCreateInstance(__uuidof(ServerInfo));
    if(SUCCEEDED(hr))
    {
    	 // Populate COM object properties here.....
    	 // Now Fire Event.
    	 Fire_GotServer( &pNewServer );
    }
    Do you need an IServerInfo** ? I would expect you to need the double indirection if you were going to instantiate in the method. Does IServerInfo have mutators and accessors?
    Last edited by [email protected]; December 13th, 2005 at 07:07 PM.

  3. #3
    Join Date
    Sep 2000
    Location
    Ottawa, Ontario
    Posts
    356

    Re: Howto pass COM object as param in event

    I don't know.. I am new to COM, I know how to pass it as a return item in a method but not in an event.


    I tried your code and get this..

    error C2039: 'CoCreateInstance' : is not a member of 'IServerInfo'
    Last edited by Jean-Guy2000; December 14th, 2005 at 09:49 AM.

  4. #4
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Howto pass COM object as param in event

    You are almost there... See the methods parameters... It takes a pointer to an interface pointer.
    Code:
    CComPtr< IServerInfo > pNewServer;
    HRESULT hr = pNewServer.CoCreateInstance(__uuidof(ServerInfo));
    if(SUCCEEDED(hr))
    {
    	  // Populate COM object properties here.....
    	  // Now Fire Event.
    	  Fire_GotServer(&pNewServer);
    }

  5. #5
    Join Date
    Sep 2000
    Location
    Ottawa, Ontario
    Posts
    356

    Re: Howto pass COM object as param in event

    Almost there, The code compiles now... but when I

    Fire_GotServer( &pNewServer );

    I get a crash here....

    T** operator&()
    {
    ATLASSERT(p==NULL); <----- Debugger stops here.
    return &p;
    }
    _NoAddRefReleaseOnCComPtr<T>* operator->() const

    ------ Callstack ----

    ATL::CComPtr<IServerInfo>:perator&() line 469 + 40 bytes
    CGuiClient::OnGotServer(ServerData * 0x02ac43d0) line 118 + 8 bytes <- call Fire_ method here...


    thanks for the help.

  6. #6
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Howto pass COM object as param in event

    Actually... You need to review the purpose.

    You get the ASSERT because the pointer is NOT NULL (It is valid).

    Is the purpose of Fire_GotServer to return a valid pointer to the caller as [out, retval] seems to imply? Then, you should not be creating an instance of it.

    If the purpose of Fire_GotServer is NOT to return a valid pointer rather to accept one, then the signature should be simply [in], and the method should accept merely a pointer and not a pointer to a pointer.

  7. #7
    Join Date
    Sep 2000
    Location
    Ottawa, Ontario
    Posts
    356

    Re: Howto pass COM object as param in event

    I want to raise an event (in Vb application) and pass a populated COM object to it. This is so I don't have to pass 9 diffrent short/string parameters.

    I am new to COM so I don't understand exactly what I am doing, just looking at example code and replicating it in hopes of getting what I want.

    I need know how to create an instance of the com object, populate it and fire the event passing this newly contrusted object (with it's 9 properties populated) back to Visual Basic application.

  8. #8
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Howto pass COM object as param in event

    Quote Originally Posted by Jean-Guy2000
    I am new to COM so I don't understand exactly what I am doing, just looking at example code and replicating it in hopes of getting what I want.
    Well... Even if you are replicating example code... Do so after understanding what it does.

    Simply copy-pasting will not help you, and mostly harass the developer who has to fix bugs in your code long after.

    Quote Originally Posted by Jean-Guy2000
    I need know how to create an instance of the com object, populate it and fire the event passing this newly contrusted object (with it's 9 properties populated) back to Visual Basic application.
    This is a pretty simple requirement, and needs you to pass an object and not get one - therefore, basic C++ tells that you don't need to pass a pointer to the object, rather the object (IServerInfo*) itself.

    You are almost there. You don't need a double pointer.

    Your IDL definition should be -
    Code:
    [id(1), helpstring("method GotServer")] HRESULT GotServer([in] IServerInfo* pServer);
    Change your method's signature appropriately.

    The usage should be...
    Code:
    CComPtr< IServerInfo > pNewServer;
    HRESULT hr = pNewServer.CoCreateInstance(__uuidof(ServerInfo));
    if(SUCCEEDED(hr))
    {
    	  // Populate COM object properties here.....
    	  // Now Fire Event.
    	  Fire_GotServer (pNewServer); 
    }

  9. #9
    Join Date
    Sep 2000
    Location
    Ottawa, Ontario
    Posts
    356

    Re: Howto pass COM object as param in event

    Thanks that worked perfectly

  10. #10
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Howto pass COM object as param in event

    You are welcome...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured