{"id":5653,"date":"2007-06-12T20:42:00","date_gmt":"2007-06-12T20:42:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2007\/06\/12\/fixing-com-interface-redefinition-error\/"},"modified":"2019-02-18T18:54:27","modified_gmt":"2019-02-18T18:54:27","slug":"fixing-com-interface-redefinition-error","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/fixing-com-interface-redefinition-error\/","title":{"rendered":"Fixing COM Interface Redefinition Error"},"content":{"rendered":"<p class=\"MsoNormal\"><span>Hello my name is Steven Toscano and I work on the Visual C++ IDE team.&nbsp; Recently we had a customer visit that exposed a common problem people are seeing when migrating to VS2005 from older versions of VS.<\/span><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><span>You will encounter this problem if you have defined an interface with the same name as one of the interfaces introduced in the Visual Studio 2005 VC++ or Platform SDK headers.&nbsp; There are different approaches to this problem depending on what you are trying to upgrade.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><b><span>Upgrading a server without touching existing clients<\/p>\n<p><\/span><\/b><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>Scenario:<\/span><\/p>\n<p class=\"MsoNormal\"><span><\/span><span>Your server defines an interface which conflicts with one that is pulled in by standard windows headers.&nbsp; Your clients depend on this interface name.&nbsp; For example let&rsquo;s say you&rsquo;ve named your interface IContext, it will conflict with the interface defined in ObjIdl.h that comes with the Platform SDK.&nbsp; When you build you get errors about your COM object not implementing enough methods in IContext it&rsquo;s because the compiler is picking up the interface from the Platfom SDK headers first instead of yours.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>Solution #1:<\/span><\/p>\n<p class=\"MsoNormal\"><span><\/span><span>Usually the headers that are pulling in these duplicate names are in your stdafx.h file.&nbsp; To prevent the compiler from pulling in the interface definition from the VS2005 headers insert the following code at the top of your stdafx.h:<\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p><\/span>&nbsp;<\/p>\n<p class=\"MsoNormal\"><span>#define<\/span><span> __IContext_FWD_DEFINED__<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>#define<\/span><span> __IContext_INTERFACE_DEFINED__<\/span><\/p>\n<p class=\"MsoNormal\"><span><\/span>&nbsp;<\/p>\n<p class=\"MsoNormal\"><span>Then at the end of your stdafx.h file insert this code:<\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p><\/span>&nbsp;<\/p>\n<p class=\"MsoNormal\"><span>#undef<\/span><span> __IContext_FWD_DEFINED__<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>#undef<\/span><span> __IContext_INTERFACE_DEFINED__<\/span><\/p>\n<p class=\"MsoNormal\"><span><\/span>&nbsp;<\/p>\n<p class=\"MsoNormal\"><span>This is to get the compiler to choose your interface and ignore the VS2005 one.&nbsp; Your interface declaration usually comes from your .h file generated by midl after compiling your .IDL file.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>Solution #2:<\/span><\/p>\n<p class=\"MsoNormal\"><span><\/span><span>If you are getting an earlier error running midl similar to:<\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p><\/span>&nbsp;<\/p>\n<p class=\"MsoNormal\"><span>error MIDL2003 : redefinition : IContext<\/span><\/p>\n<p class=\"MsoNormal\"><span><\/span>&nbsp;<\/p>\n<p class=\"MsoNormal\"><span><\/span><span>This means that your IDL file that contains the definitions of your COM object has a conflicting interface name.&nbsp; The above solution alone will not work for you.&nbsp; You will need to rename the conflicting type in your IDL file.&nbsp; For example let&rsquo;s say the name is IContext, search for all instances of IContext and replace with IContext2 in your IDL file.&nbsp; Then in your server implementation code anywhere you include the generated .h file add this code:<\/span><\/p>\n<p class=\"MsoNormal\"><span><\/span>&nbsp;<\/p>\n<p class=\"MsoNormal\"><span><\/span><span>typedef<\/span><span> IContext2 IContext;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>#define<\/span><span> IID_IContext IID_IContext2<\/span><\/p>\n<p class=\"MsoNormal\"><span><\/span><span><\/span>&nbsp;<\/p>\n<p class=\"MsoNormal\"><span>This way you won&rsquo;t need to change any of your server implementation code and old clients will still be able to connect to you using the IContext name.&nbsp; Before you can build successfully add the workaround described in solution #1 above so that you don&rsquo;t get a redefinition error on IContext with the one in ObjIdl.h.&nbsp; Now you should be able to build successfully and old clients should be able to connect to you (provided you didn&rsquo;t change anything else).<\/p>\n<p><\/span><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello my name is Steven Toscano and I work on the Visual C++ IDE team.&nbsp; Recently we had a customer visit that exposed a common problem people are seeing when migrating to VS2005 from older versions of VS. &nbsp; You will encounter this problem if you have defined an interface with the same name as [&hellip;]<\/p>\n","protected":false},"author":289,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus"],"acf":[],"blog_post_summary":"<p>Hello my name is Steven Toscano and I work on the Visual C++ IDE team.&nbsp; Recently we had a customer visit that exposed a common problem people are seeing when migrating to VS2005 from older versions of VS. &nbsp; You will encounter this problem if you have defined an interface with the same name as [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/5653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/289"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=5653"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/5653\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=5653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=5653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=5653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}