|
-
December 13th, 2005, 04:10 PM
#1
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 ** '
-
December 13th, 2005, 07:02 PM
#2
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.
-
December 14th, 2005, 09:45 AM
#3
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.
-
December 14th, 2005, 09:55 AM
#4
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);
}
-
December 14th, 2005, 10:39 AM
#5
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.
-
December 14th, 2005, 11:06 AM
#6
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.
-
December 14th, 2005, 11:47 AM
#7
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.
-
December 14th, 2005, 01:42 PM
#8
Re: Howto pass COM object as param in event
 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.
 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);
}
-
December 14th, 2005, 01:58 PM
#9
Re: Howto pass COM object as param in event
Thanks that worked perfectly
-
December 14th, 2005, 01:59 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|