统计 |
blog名称:人在旅途 日志总数:175 评论数量:505 留言数量:13 访问次数:1674495 建立时间:2005年12月7日 |
生命是过客,人在旅途。奶奶是信基督教的,没啥文化,却养育了四子二女,还带过九个孙辈。老人家对生命的看法就是“人都是客人,迟早要回去的。”就以《人在旅途》来纪念她。

« | October 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | |
|
公告 |
本人上传的源程序中可能引用或使用了第三方的库或程序,也可能是修改了第三方的例程甚至是源程序.所以本人上传的源程序禁止在以单纯学习为目的的任何以外场合使用,不然如果引起任何版权问题,本人不负任何责任. | |

|
本站首页 管理页面 写新日志 退出
调整中...
[微软技术开发]ATL:Implement the same default interface from different object in different DLLs(different project) and using user define type in the interface method. |
人在旅途 发表于 2006/1/22 8:24:21 | First Way:1. As normally, first create the ATL object(Eg: First) and an ATL interface(Eg: IFirst) in a DLL by the ATL Wizard in the first project.2. Add IFirst's method by using ATL Class Wizard.3. Create a IDL file(For example, FirstInterface.idl), then move the definition of IFirst to that IDL file. Don't forget to move the necessary "#import" directive and necessary enum, structure definition to that IDL file.4. Now you can remove the IFirst's definition from it's original place and replace with " import "FirstInterface.idl" ". Then you need to add the FirstInterface.idl to your project file group. Now build it, it is ok.5. Thus, you have a file named FirstInterface.idl containing your interface definition. So you can add this IDL file to any project you want to use this interface with following steps: A. Add it to project file group; B. Add " #include "FirstInterface.idl"; " to the project's IDL file; C. Add " interface IFirst " to you coclass definition of the project's IDL file; D. Public your C++ implement class from IFirst. E. Implement in your C++ implement class of these virtual definition methods of IFirst.(The easiest way I think is to copy the source code from you first project and modify it)6. Now everything is ok. What? you want to modify the IFirst(maybe adding some more methods). Yes, you can do it, but it is not encouraged for you to do this. You are advised to make everything ok for the IFirst in your first project. If you really want to change it later, you can change it in your first project(still with the ATL class wizard) and then change the implemention of that IFirst in every other projects. It is really silly, but isn't Microsoft more silly.
Second Way:1. Right click your implement C++ class, then select "Implement Interface",then click "Add Typelib", then add the DLL contain that interface.Notes: You can't modify the interface by this ways.
Sometimes you may want to use use define type or some not standard OLE type in your interface definition. Yes, you can do it like that, but first be sure of two things:1. Your object is in a DLL in in a EXE.2. Your interface is not a dual interface which support IDispatch, but only a custom interface.
Now here is a sample:1. I want to use ITEMIDLIST in the interface definition, so I add it to my definition. But an error raised when compiling that the ITEMIDLIST has no definition.2. So I add "#include shlobj.h" to that IDL file, but still get error when compiling.3. So I remove the "#include shlobj.h", and copying the definition of ITEMIDLIST from the "shlobj.h". But because I already have include the "shlobj.h" in the "stdafx.h". So there are some definition is re-defined. So I got a problem:If I don't copy the definition of ITEMIDLIST to the IDL file, I can't use ITEMIDLIST in the interface definition.If I copy it, then the ITEMIDLIST definition will be generated in the head file created by IDL compilor according to the IDL file. And that head file will need to be included in your project file. But the definition has already defined in the "shlobj.h" which also need to be included in your porject file. So there is a redefinition problem.4.I solve the problem like this:In the IDL file, I using: cpp_quote("#if 1>5") // SHITEMID -- Item ID typedef struct _SHITEMID // mkid { USHORT cb; // Size of the ID (including cb itself) BYTE abID[1]; // The item ID (variable length) } SHITEMID; // ITEMIDLIST -- List if item IDs (combined with 0-terminator) typedef struct _ITEMIDLIST // idl { SHITEMID mkid; } ITEMIDLIST; cpp_quote("#endif")The cpp_quote("xxx") will merge the string directly to the created head file, and it has no effect in the IDL file.The cpp_quote("#if 1>5") and cpp_quote("#endif") will make an effect in the created IDL file that the definition between them will never be compiled by the condition compiling directive(because "if 1>5" always false).Thus I solve this problem. But there are still two things need to care:1. Be sure, you already include "shlobj.h" before include the created head file by IDL compilor.2. When this interface is implemented in two different object like Ob1 and Ob2. And you need to use both the objects in the some project. Then after you have imported both of them, you will get three definition of ITEMIDLIST: A. the Ob1::ITEMIDLIST; B. the Ob2::ITEMIDLIST; C. the definition in the "shlobj.h".So you need to convert them from time to time, eg: (Ob1::ITEMIDLIST)....
|
阅读全文(2708) | 回复(0) | 编辑 | 精华 |
|