The prototypical ATL COM project

by Matt 2. October 2006 16:14

Now there's a title to make your pulse race.

This post is ostensibly another in the how-to-write-a-Windows-Desktop-Search-protocol-handler series, but it's really just what-Matt-does-to-write-a-COM-object. I've done it for just about every COM object I've written; I have a feeling I'll be referring back to it.

And yes, that title does say ATL. It's C++ time.

I'm not going into the C++/.net COM object argument just now. I will at some point - I've got plenty to say about it - but for this, I'm using C++. Not everything is a .net shaped nail.

Of course, these tips make certain assumptions. I'm talking about COM objects that are in-process and that only implement other people's interfaces (think shell extensions, addins for Office, Visual Studio or Windows Desktop Search. Pretty much any kind of addins). Some of this won't work for situations such as scriptable components, custom objects for a custom application and so on. Your mileage may vary.

Right then. Fire up Visual Studio and create a new ATL project. We want a dll and we don't want it attributed. I'm a bit of a Luddite with regards to C++ attributes. I just know it's a code generation thing, and I'm old-skool enough to write this stuff long-hand, thanks. (That said, I'm reading the link above, and it does look rather interesting. Might have to have a play with this...)

I'm going to assume the project was called "dllname", and I'm going to leave it as an exercise to the reader to substitute "dllname" for the right name for the rest of this post.

1. Remove proxy stub project

I haven't once used this. I'm not implementing my own interfaces, so I don't need to provide any custom marshalling. Right click on the PS project and delete it.

2. Get rid of the typelib

Again, I'm not implementing my own interfaces, so I don't need to describe them to anyone. If I don't remove the typelib, ATL will register an essentially empty typelib in the registry for me, and will store the typelib as a resource in my dll. Both of these are unnecessary bloat. Trivial, yes, but easily prevented (we should still try to be as streamline as possible, even in this day of cheap memory and storage).

Firstly, open up the dllname.cpp file, and modify DllRegisterServer. Simply add a FALSE to the arguments, so that the typelib isn't registered:

HRESULT hr = _AtlModule.DllRegisterServer(FALSE);

Do a similar thing for DllUnregisterServer:

HRESULT hr = _AtlModule.DllUnregisterServer(FALSE);

Now we need to remove the typelib from the resources. Open dllname.rc, and in the resource view, right click on the dllname.rc node (not the dllname project node!). Select "Resource Includes". You're going to get a dialog with two big edit boxes. In the bottom one will be the line:

1 TYPELIB "dllname.tlb"

Delete this.

Don't delete the .idl file from the project! This does create the type library (.tlb) but the ATL wizards need this file.

3. Don't register an AppId

Again, never needed this. Open the dllname.rgs file and remove the AppId section. Easy peasy.

4. Enable QueryInterface debugging

This one's a corker. Saved the best til last. Once you've done this, every time a client calls one of your objects' QueryInterface methods, you'll get a string written via OutputDebugString with the class name, the name of the interface requested (from the registry) and a "failed" marker if the interface isn't supported.

This is an absolutely essential debugging aid when working with COM. Quite often the documentation will tell you what all the interfaces are, what each one does, but not which combination of interfaces to implement on a given object. Or, it only lists the interfaces for that particular API domain, and don't list the standard COM interfaces required, such as IPersistFile, etc.

Edit you stdafx.h and define _ATL_DEBUG_QI before including any atl header files.

Note that there's a bug in AtlDumpIID (the function that does all the magic) in atlbase.h. If the interface name isn't found in the registry, the code is supposed to output the raw IID, but the logic is wrong, and nothing gets output. It's fixed in Visual Studio 2005 and later, so if you have anything earlier, check out Craig Skibo's post on the problem. He offers a solution that you just have to copy into the file. It's a little verbose (I'd have just set a flag on success and checked that at the end of the file rather than have all those goto's...)

A top tip in conjunction with this is to use sysinternal's DebugView to capture all those messages to OutputDebugString, even when you're not in the debugger. Oh, and if you don't know what a raw IID actually refers to, don't forget Google. Or install the Platform SDK and Windows Desktop Search.

5. Cleanup object rgs scripts

Now, this one is for when you create a new object, rather than the first steps of a project like the rest. So, go on. Create an object. Now open it's .rgs file. The first thing we can get rid of is the typelib reference. We're not registering that typelib any more, so we can get rid of that. And I always get rid of "Programmable", and if you're not using ProgIds, get rid of those too.

And that's it. Just a couple of simple changes that I make every time I start a COM object.

PS. Whatever development you're doing (C++/.net/asp.net/whatever) you really, really want to use Microsoft's symbol server.

Tags:

Windows Desktop Search

Comments (55) -

cosmetic surgery
cosmetic surgery
1/25/2011 6:30:33 PM #

Interesting concept, design and article. Not too sure if I agree.

Reply

Rosamond Islas
Rosamond Islas
2/11/2011 5:17:39 AM #

got here form bing ... great article and I definitely can agree. Not sure if you heard, the new apple ipad might be coming out anytime from now .. I am looking forward to it, pretty interesting news.

Reply

omp photographer
omp photographer
3/17/2011 4:20:33 PM #

Nice post.  Ur on my favorites!

Reply

top wedding songs
top wedding songs
5/7/2011 3:33:35 PM #

Thanks a million for your post, I really like your site.  Long time reader, first time poster, so to speak.  I attempted to share this one time before, I don't believe it posted correctly...with any luck it will this time! Smile

Reply

AnotherStar82
AnotherStar82
5/20/2011 5:25:53 AM #

Sono pienamente d'accordo!  Ringrazio per l'articolo

Reply

Luminess
Luminess
7/8/2011 6:44:25 PM #

This can be a good reference in face treatments. Thanks for sharing.

Reply

Trademark
Trademark
7/11/2011 11:41:23 PM #

Can you  do another post  but compare it to standard - I didn't get it?  Awesome way to approach it, but perhaps I just haven't thought of it that way.

Reply

LAURENCE  Debora
LAURENCE Debora
7/16/2011 5:43:02 PM #

Création annuaire - annuaire Création annuaire sur annuaires-gratuit.com, vos avis svp.

Reply

astuce casino
astuce casino France
7/19/2011 10:58:10 AM #

I would like to thnkx an appreciation to the efforts you control toss in writing this blog. I am hoping the at any rate high-grade blog post from you in the upcoming as well. In actuality your fictional untried abilities has inspired me to have my own blog now. Patently the blogging is spreading its wings quickly. Your note up is a enthusiastic exemplar of it.

Reply

best suv reviews
best suv reviews
7/20/2011 10:24:28 PM #

Hi, just required you to know I he added your website to my Google bookmarks due to your layout. But seriously, I consider your net web-site has 1 in the freshest theme I??ve came across. It extremely helps make studying your blogging site significantly easier.

Reply

best suv
best suv
7/20/2011 10:39:54 PM #

Hi, just required you to know I he added your website to my Google bookmarks due to your layout. But seriously, I consider your net web-site has 1 in the freshest theme I??ve came across. It extremely helps make studying your blogging site significantly easier.

Reply

luxury suv
luxury suv
7/21/2011 4:41:15 PM #

Hi,what an excellent article this is,I found it on bing and I like it very much,I agree with what you have said, lots of things will be learned form your site,but I still have some questions with the last part,can you explain it for me ?I will appreciate your answer,and I will be back again!

Reply

LAURENCE  Debora
LAURENCE Debora
7/22/2011 10:43:09 PM #

Service de recherche en ligne sur Sukoga.com, recherche ebay, sports, images, wiki.

Reply

best ipad games
best ipad games
7/24/2011 4:47:51 AM #

Hi,what an excellent article this is,I found it on bing and I like it very much,I agree with what you have said, lots of things will be learned form your site,but I still have some questions with the last part,can you explain it for me ?I will appreciate your answer,and I will be back again!

Reply

best bicep exercises
best bicep exercises
8/3/2011 2:10:36 AM #

Hi, Enjoyed the info.  Good post.  I will implement with my training routine.

Reply

DEBORA Laurence
DEBORA Laurence
8/10/2011 8:26:59 AM #

Une belle Liste d'annuaires thématiques gratuits, avec liens durs pour référencement.

Reply

DEBORA Laurence
DEBORA Laurence
8/13/2011 8:53:52 PM #

Site gratuit pour la vente aux encheres Sibeys.com, vendez aux enchères vos objets.

Reply

Johnny Smith
Johnny Smith United States
9/5/2011 7:34:58 AM #

Rather educational many thanks, I believe your visitors will probably want way more items similar to this keep up the great content.

Reply

Tim Jefferson
Tim Jefferson United States
9/14/2011 3:25:02 AM #

Incredibly insightful cheers, I do believe your trusty readers might probably want a whole lot more articles such as this carry on the good effort.

Reply

paintball equipment
paintball equipment
10/4/2011 8:30:26 AM #

If you are into paintball. Check out Bidfrenzy.co.za. Paintball equipment at a fraction of retail.

Reply

led lighting on motorcycle
led lighting on motorcycle
10/12/2011 5:49:01 PM #

Wow, didn't realize you could get this on the internet now.

Reply

hi
i realy liket your blog keep up the good work Awesome

Reply

Robert
Robert
10/22/2011 9:45:06 AM #

Hello

interresting, post here will came back
keep update

Reply

Donald Rufer
Donald Rufer
10/23/2011 5:14:51 AM #

Today, I went to the beach front with my kids. I found a sea shell and gave it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is completely off topic but I had to tell someone!

Reply

Stephane
Stephane
10/23/2011 11:58:51 AM #

Hi

Nice, post here will came back
Keep like this

Reply

Reno Carpet Cleaners
Reno Carpet Cleaners
11/2/2011 3:03:39 PM #

I seemed to be aware of this previously, nevertheless there are several helpful bits which finished the picture for me personally, thanks for your time!

Reply

One of the best posts i have read so far!

Reply

iPhone blogs
iPhone blogs
11/16/2011 4:55:53 AM #

Good job   , amazing Post !!!

Reply

having read your post, I believe the points produced are quite robust and interesting and need to concur inside the most portion along with your thoughts. <a href=\"www.floridatoday.com/.../section\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\">melbourne cup live stream tips</a>

Reply

Philix
Philix United States
11/19/2015 3:40:39 PM #

wonderful web site

Reply

Philix
Philix United States
11/20/2015 5:54:54 AM #

Good Website

Reply

Philix
Philix United States
11/20/2015 6:57:03 PM #

Great Site

Reply

Philix
Philix United States
11/21/2015 2:01:17 AM #

ace site.

Reply

Philix
Philix United States
11/22/2015 2:01:52 AM #

good site

Reply

Philix
Philix United States
11/22/2015 2:02:03 AM #

Good Site

Reply

Philix
Philix United States
11/22/2015 5:42:46 PM #

nice web site.

Reply

Philix
Philix United States
11/22/2015 7:24:25 PM #

good site

Reply

Micheline Trafton
Micheline Trafton United States
1/6/2016 3:16:42 PM #

Congrats! So glad to see this site getting the recognition it deserves, the world can always use a little more awesome.

Reply

Elinor Gupta
Elinor Gupta United States
1/15/2016 8:32:32 AM #

yeah, except the "dolphin saving you" one

Reply

Tommy Burows
Tommy Burows United States
1/15/2016 8:55:34 AM #

Congrats! This is such a great website, I know it will translate well into book form. Which is AWESOME!

Reply

Tyson F. Gautreaux
Tyson F. Gautreaux United States
10/18/2016 2:37:59 AM #

Je suis souvent votre site et je le trouve parfait ! ;) continuez comme �a !

Reply

Luigi Fulk
Luigi Fulk United States
10/18/2016 2:37:59 AM #

Bonjour � vous, j'appr�cie votre site et je tenais � vous le dire ! bonne continuation !

Reply

Andrew A. Sailer
Andrew A. Sailer United States
10/18/2016 2:38:00 AM #

Bonjour � vous, j'appr�cie votre site et je tenais � vous le dire ! bonne continuation !

Reply

Cathi Duban
Cathi Duban United States
12/5/2016 10:56:10 PM #

This handy free Android app also allows you to make your portfolio (s) track. You can upload photos directly to a number of social network sites from here. A heart rate monitor not only lets you know how hard you are working, but most models will measure your "time in range" to let you know exactly how many minutes you can count as exercise. This interactive feature allows a user to shift the view of a photo so that its perspective changes.

Reply

como eliminar puntos negros
como eliminar puntos negros United States
10/5/2017 2:03:38 PM #

I was very pleased to search out this internet-site.I needed to thanks for your time for this wonderful read!! I positively enjoying each little bit of it and I've you bookmarked to take a look at new stuff you blog post.

Reply

como eliminar el acne
como eliminar el acne United States
10/5/2017 11:03:43 PM #

It's appropriate time to make some plans for the future and it's time to be happy. I've read this post and if I could I want to suggest you some interesting things or suggestions. Perhaps you can write next articles referring to this article. I desire to read more things about it!

Reply

remedios naturales
remedios naturales United States
10/6/2017 6:28:11 AM #

It's the best time to make some plans for the future and it's time to be happy. I have read this post and if I could I wish to suggest you some interesting things or tips. Maybe you could write next articles referring to this article. I want to read more things about it!

Reply

mascarilla
mascarilla United States
10/6/2017 5:47:34 PM #

Thank you for any other informative web site. Where else could I am getting that type of information written in such an ideal approach? I've a venture that I'm simply now operating on, and I have been at the glance out for such information.

Reply

Carroll B. Merriman
Carroll B. Merriman United States
12/6/2017 8:35:19 AM #

Hands down, Apple's app store wins by a mile. It's a huge selection of all sorts of apps vs a rather sad selection of a handful for Zune. Microsoft has plans, especially in the realm of games, but I'm not sure I'd want to bet on the future if this aspect is important to you. The iPod is a much better choice in that case.

Reply

apple tools
apple tools United States
4/4/2018 10:36:06 AM #

I dugg some of you post as I thought   they were  very beneficial   invaluable

Reply

Funny Website
Funny Website United States
5/23/2018 3:45:18 PM #

Have a good laugh at this site.

Reply

Chang Marker
Chang Marker United States
6/20/2018 11:31:23 PM #

Black on black in the Charg I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Sarah Macugay
Sarah Macugay United States
7/20/2018 1:25:29 AM #

I have been checking out many of your stories and i can state pretty

Reply

Julius Tayloe
Julius Tayloe United States
7/20/2018 9:47:59 PM #

Awesome! Its in fact amazing paragraph, I have got much clear idea regarding from this piece of

Reply

Pa Pennick
Pa Pennick United States
7/20/2018 10:53:43 PM #

Hello. impressive job. I did not anticipate this.

Reply

Add comment

biuquote
  • Comment
  • Preview
Loading

Rel=Me

Month List

RecentComments

Comment RSS