VC++ 5 & ATL: How add 4 interfaces to COM server? 
Author Message
 VC++ 5 & ATL: How add 4 interfaces to COM server?

I'm developing a COM server using ATL in VC++5.  I'd like this server to have 4
(or more) interfaces.  I'm not sure of the best way to do this.  

I've already created the project using the ATL COM Wizard, specifying no
ISupportErrorInfo, etc.  I then added an ATL Object;  unfortunately, the wizard
created an interface with the same name of the object;  I have no need for this.

I then tried to Add Class, selecting the ATL type.  I wanted this class to
include all the 4+ interfaces.  However, the wizard only allows a max. of 3.

The library section of the .IDL file now contains 2 coclass sections, one with
the name of the added Object & one with the name of the added Class.  I'm not
sure I understand exactly what a coclass is ... I have 2 ATL books & neither
gives much of a description;  neither does the MSDN ... can anybody explain what
a coclass is?

Ideally, I'd like just one coclass (pending an explanation of what a coclass
really is, of course).  Is there any good way to add the interfaces to the first
coclass, created when the object was added?

Why can't I included 4 interfaces in a new class?

Why does a newly added class always support ISupportErrorInfo?

I'm open to any suggestions!!!  Thanks!

=======================================================
Dave Morgenlender

=======================================================



Mon, 19 Feb 2001 03:00:00 GMT  
 VC++ 5 & ATL: How add 4 interfaces to COM server?
Forget the wizards. They are useful for creating the initial template. After
that, you can do it as you like by editing the IDL by hand. Now to add 4
interfaces to your object, you can edit IDL to be something like

[
    uuid(XXX), //generate an IID from guidgen.exe and put it here
    object, //needed to show that this is a COM interface not an RPC
interface
    dual or oleautomation, // if you need to use IDispatch derivative or
typelibrary marshalling resp.
    helpstring ("Something about interface")
]
interface IFoo1:IUnknown [or IDispatch] // if dual
{
    HRESULT FooMethod1();

Quote:
};

[
    uuid(XXX), //generate an IID from guidgen.exe and put it here
    object, //needed to show that this is a COM interface not an RPC
interface
    dual or oleautomation, // if you need to use IDispatch derivative or
typelibrary marshalling resp.
    helpstring ("Something about interface")
]
interface IFoo4:IUnknown [or IDispatch] // if dual
{
    HRESULT FooMethod4();
Quote:
};

[
    uuid(XXX), //generate an IID from guidgen.exe and put it here
    object, //needed to show that this is a COM interface not an RPC
interface
    dual or oleautomation, // if you need to use IDispatch derivative or
typelibrary marshalling resp.
    helpstring ("Something about interface")
]
interface IFoo2:IUnknown [or IDispatch] // if dual
{
    HRESULT FooMethod2();
Quote:
};

[
    uuid(XXX), //generate an IID from guidgen.exe and put it here
    object, //needed to show that this is a COM interface not an RPC
interface
    dual or oleautomation, // if you need to use IDispatch derivative or
typelibrary marshalling resp.
    helpstring ("Something about interface")
]
interface IFoo3:IUnknown [or IDispatch] // if dual
{
    HRESULT FooMethod3();

Quote:
};

[
uuid(XXX),
helpstring("")
]
library THENAMEOFLIBRARY
{
    importlib("stdXX.tlb");

    [
        uuid(XXX)
    ]
    coclass FooClass
    {
        [default] interface IFoo1;
        interface IFoo2;
        interface IFoo3;
        interface IFoo4;
    };

Quote:
};

And change your .H file which has the class definition to be
deriving from all those interfaces. and an entry for CComCoClass
somthing like
class ATL_NO_VTABLE CFooClass:
  public CComObjectRoot, //If its STA
  public CComCoClass<CFooClass, &CLSID_FooClass>,
  public IFoo1,
  public IFoo2,
  public IFoo3,
  public IFoo4
{
...
BEGIN_COM_MAP(CFooClass)
 COM_INTERFACE_ENTRY(IFoo1)
 COM_INTERFACE_ENTRY(IFoo2)
 COM_INTERFACE_ENTRY(IFoo3)
 COM_INTERFACE_ENTRY(IFoo4)
END_COM_MAP()
... // Here declare and/or implement those interface methods.

Quote:
};

Now about coclass:
coclass is the keyword that defines a COM class (similar to C++ class in
that it has methods but not data members (unless its dispinterface which is
a different thing)) whose GUID is  what COM libraries use to instantiate a
DLL or an EXE. Now, this is necessary to get to the right interface from a
client. Now in the above class we have defined, if a client application
needs to use ,say IFoo1::FooMethod1() method, it has to instantiate the
right component (DLL or EXE) first. In order to do that, it will use one of
the activation calls like CoCreateInstance[Ex], or CoGetClassObject() or
CoGetInstanceFromFile(). Now, in CoCreateInstanceEx() there is an argument
for CLSID

(class ID which is the uuid you would have defined in the IDL for the
coclass). The client will use that there and call that function will IID
(interface ID ) of the IFoo1. something like
IFoo* pFoo = NULL;
HRESULT hr = CoCreateInstance (CLSID_FooClass,NULL, CLSCTX_ALL, IID_IFoo1,
&pFoo);

At this point, COM libraries know what to instantiate by using
CLSID_Fooclass value and looking up the registry (or class store in case of
NT5) under HKEY_CLASSES_ROOT\Classes\CLSID and getting the name of the
executable component (DLL or EXE in the same machine or in different
machine) and instantiate that component and Query for IID_IFoo1 interface.
Now, if that component supports that interface, in this case we do, it will
return a pointer to that interface and that will be given back to the client
who can merrily call FooMethod1. Note that this is a simplified version of
what actually happens (like marshalling and security)..

About ISupportErrorInfo:
This is the interface you need to support if you want to throw exceptions
from your server and give rich error information. Normally, a HRESULT with
the right value can be enough but in some cases where an irrevocable error
has occured and you want to return a detailed description to that error and
also possibly a help file location the client can use to look up to get more
info. So, here is where it will come to use. You support this interface
which will tell the clients that this server supports rich error
information. And then when an error occurs, you can use CreateErrorInfo and
SetErrorInfo function calls to set up the error and throw it back to client.
ATL has AtlThrowError() which makes it simple and MFC has
AfxThrowOleException () or something like that to support it.
This can be used in VB and Java and C++ unlike c++ exceptions which can only
be used in C++ clients.

Hope this helps,
BTW: Check out "Essential COM" book by Don Box, And books by Dr. Richard
Grimes which have some detailed information about all these.
--
Girish Bharadwaj [VC++/MVP].
Dont send email queries to me directly.Please put
them on the newsgroups.
Thank you


Quote:
>I'm developing a COM server using ATL in VC++5.  I'd like this server to
have 4
>(or more) interfaces.  I'm not sure of the best way to do this.

>I've already created the project using the ATL COM Wizard, specifying no
>ISupportErrorInfo, etc.  I then added an ATL Object;  unfortunately, the
wizard
>created an interface with the same name of the object;  I have no need for
this.

>I then tried to Add Class, selecting the ATL type.  I wanted this class to
>include all the 4+ interfaces.  However, the wizard only allows a max. of
3.

>The library section of the .IDL file now contains 2 coclass sections, one
with
>the name of the added Object & one with the name of the added Class.  I'm
not
>sure I understand exactly what a coclass is ... I have 2 ATL books &
neither
>gives much of a description;  neither does the MSDN ... can anybody explain
what
>a coclass is?

>Ideally, I'd like just one coclass (pending an explanation of what a
coclass
>really is, of course).  Is there any good way to add the interfaces to the
first
>coclass, created when the object was added?

>Why can't I included 4 interfaces in a new class?

>Why does a newly added class always support ISupportErrorInfo?

>I'm open to any suggestions!!!  Thanks!

>=======================================================
>Dave Morgenlender

>=======================================================



Tue, 20 Feb 2001 03:00:00 GMT  
 VC++ 5 & ATL: How add 4 interfaces to COM server?
David,

For me, the easiest way is to go over to the class view list window,
right-click mouse on the interface name and then selected "add method"
menu item.  Not only we are IDL automatically be updated, but the
method will be added to your classes as well.
(either in IDL or )

Charles Steinhardt[MVP]
(To Email: remove NO_SPAM_NO from return address)



Thu, 22 Feb 2001 03:00:00 GMT  
 VC++ 5 & ATL: How add 4 interfaces to COM server?

Quote:

> David,

> For me, the easiest way is to go over to the class view list window,
> right-click mouse on the interface name and then selected "add method"
> menu item.  Not only we are IDL automatically be updated, but the
> method will be added to your classes as well.
> (either in IDL or )

> Charles Steinhardt[MVP]
> (To Email: remove NO_SPAM_NO from return address)

I thought David was asking about adding more interfaces ??
--
Girish Bharadwaj [vc++/mvp]


Fri, 23 Feb 2001 03:00:00 GMT  
 VC++ 5 & ATL: How add 4 interfaces to COM server?
Girish,

Quote:
>I thought David was asking about adding more interfaces ??

Yes, I was!

=======================================================
Dave Morgenlender

=======================================================



Sat, 24 Feb 2001 03:00:00 GMT  
 VC++ 5 & ATL: How add 4 interfaces to COM server?
Girish,

Thanks for all that info.  I'll make the changes by hand, as you suggest.

Is there any reason I should not delete the default interface created by the
wizard?

=======================================================
Dave Morgenlender

=======================================================



Sat, 24 Feb 2001 03:00:00 GMT  
 VC++ 5 & ATL: How add 4 interfaces to COM server?
Girish,

Quote:
>Forget the wizards. They are useful for creating the initial template. After
>that, you can do it as you like by editing the IDL by hand.

So, now I won't be using the New Class wizard.  One thing this did was generate
a .RGS file for each interface; then it updated various areas to use the .RGS
file, including  the .RC file, the project, the interface .H file to use the
IDR_x created in the .RC file (in DECLARE_REGISTRY_RESOURCEID).  

Do I now need to generate all this by hand?

Should I generate the CLSID using the utility you mentioned?

=======================================================
Dave Morgenlender

=======================================================



Sat, 24 Feb 2001 03:00:00 GMT  
 VC++ 5 & ATL: How add 4 interfaces to COM server?
Girish,

Oops ... I now realize the registry stuff is for each coclass, not each
interface.  So by using the ATL Object Wizard, I have this taken care of.  Sorry
for bothering everybody with this!

=======================================================
Dave Morgenlender

=======================================================



Sat, 24 Feb 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Adding ATL COM support to VC application.

2. ATL COM - Passing ATL COM classes through another ATL COM class in the same app

3. Accessing a collection from a VB com server in a VC/ATL Client

4. Creating Server-side Session using VC++ & COM

5. Creating Server-side Session using VC++ & COM

6. COM/ATL novice:passing object pointers through methods on a COM interface

7. IE4 Preview2 COM interface != VC++ 5.0 native COM (#import compile error)

8. Any updates in ATL/ATL Server in VC 7.1

9. problems passing ADO Recordset from VB-COM client to VC-COM-Server dll

10. VB COM MIGRATION TO ATL VC COM

11. Adding interface to local server

12. ATL local server implementing 2 interfaces derrived from IUnknown

 

 
Powered by phpBB® Forum Software