在MFC程序中调用COM
来源:互联网 发布:启辰网络 编辑:程序博客网 时间:2024/06/13 22:31
Intializing
In order to to use COM in your MFC app, you will need to call AfxOleInit() in the InitInstance() of your application class.
Creating the Wrapper class
Open the Classwizard from the View menu. Click [Add Class]. Select 'From a type library...'.
Select the Type Library. This is usually a file with a '.tlb' extension supplied with the dll/exe/ocx, or it may be contained within the dll/exe/ocx file.
In the 'Confirm Classes' dialog box, select an interface, then edit the names of the files which will be auto-generated. I suggest you put '_Wrapper' at the end of both the class name and the file names.For instance: 'IExample_Wrapper', 'Example_Wrapper.h', 'Example_Wrapper.c'.
The new class will be added to the project and should show up in the ClassView tab of the Workspace. This class will be derived from COleDispatchDriver.
Instantiating the COM object
You must create an instance of the COM object by using the CreateDispatch() member of the wrapper class, passing it either a GUID or the text identifier. Note that you must refer to the 'package' name, the Interface, and the version number of the Interface.
e.g.
CExample_Wrapper exampleWrapper;exampleWrapper.CreateDispatch(_T("Example.Example.1"));
Using the COM object's methods
The auto-generated wrapper will have methods for each of the COM methods.These methods do not return the HRESULT. Instead they will return the [out, return] parameter if any.
If a COM methods returns anything other than S_OK, a COleDispatchException will be thrown. You may detect this with a try/catch block.
e.g.
try{ int iTemp = exampleWrapper.GetValue();ࠠ}catch(COleDispatchException* pEx){ //do something.}
AfxOleInit():
AfxOleInit()调用的是OleInitialize(),而OleInitialize()除了调用CoInitializeEx()来初始化COM库外,还进行一些其它的操作,这些操作对OLE应用来说是必须的,这些OLE应用包括:
(1)Clipboard;
(2)Drag&drop;
与AfxOleInit()对应的是,AfxOleTerm()。但是,在你的程序中,AfxOleTerm()可以不出现,这是因为,MFC已经帮你做好了(有兴趣的话,你可以仔细研究一下CWinThread::m_lpfnOleTermOrFreeLib,而CWinApp是从 CWinThread继承的)。
CoInitialize():
CoInitialize和CoUninitialize必须成对使用, CoInitialize()放在C**App::InitInstance(..), CoUninitialize房子C**App::ExitInstance()内。
- 在MFC程序中调用COM
- 避免在MFC程序中调用进程外Com组件出现“由于另一个程序正在运行...”的解决方案
- 【MFC】如何在mfc窗口程序中调用控制台
- 在Delphi.net中调用COM/COM+
- 在C#中调用COM/COM+
- 在MFC中调用DLL
- 在C++程序中调用C#编写的COM程序集
- 如何在PHP中通过ADO调用Asscess数据库和COM程序
- 怎样在PHP中通过ADO调用Asscess数据库和COM程序
- 在Windows Server 2008 X64中通过.NET程序调用32位com组件
- MFC 程序中调用 vbs 脚本文件
- MFC 程序中调用 vbs 脚本文件
- 在SQL中调用COM对象
- 在COM组件中调用JavaScript函数
- 在SQLServer中调用Com组件
- 在SQL中调用COM对象
- 在COM组件中调用JavaScript函数
- 在COM组件中调用JavaScript函数
- Android(Java):按钮复选框点中效果
- 小心C语言的定义与声明
- Android编程之ActivityManager: Segmentation fault
- 使用游标实现Oracle中的行列转换
- 下一座营销金矿:移动营销
- 在MFC程序中调用COM
- Code
- 任务一、数据库迁移的总结
- 活用UNION ALL
- DOS下文件共享
- Oracle字符集之乱码问题
- 网站被K后快捷收复小技巧
- Eclipse CDT Ubuntu下重新导入工程后make: *** No rule to make target `all'. Stop.
- 【Linux下配置NAT服务器共享上网】