Com组件开发过程中用的不多,资料也不多,故记录开发Com组件中的部分问题。

这一篇文章里,讲解了如何使用VS2010创建Com组件。现在基于该文章创建的Com组件接口,创建VC++项目来调用该接口。

使用流程

新建win32控制台项目。

主文件代码如下:

#include "stdafx.h"
#include "../testCom/testCom_i.h"
#include "../testCom/testCom_i.c" int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr;
IMyClass *pCom = NULL;
CoInitialize(NULL);
hr = CoCreateInstance(CLSID_MyClass,NULL,1,IID_IMyClass,(LPVOID*)&pCom);
if (SUCCEEDED(hr))
{
LONG res;
pCom->Add(2,3,&res);
printf("2+3=%d\n",res);
}
CoUninitialize();
return 0;
}

代码说明:

#include "../testCom/testCom_i.h"

#include "../testCom/testCom_i.c"

是将com组件定义接口的文件包含到本工程中,这样操作后才可以在后面直接调用Com接口函数。

调用Com组件前必须调用CoInitialize(NULL);

调用完成后需要释放CoUninitialize();

CoCreateInstance用于创建一个Com实例,函数说明如下:

HRESULT CoCreateInstance(
__in REFCLSID rclsid,
__in LPUNKNOWN pUnkOuter,
__in DWORD dwClsContext,
__in REFIID riid,
__out LPVOID *ppv
); rclsid [in]
The CLSID associated with the data and code that will be used to create the object. pUnkOuter [in]
If NULL, indicates that the object is not being created as part of an aggregate. If non-NULL, pointer to the aggregate object's IUnknown interface (the controlling IUnknown). dwClsContext [in]
Context in which the code that manages the newly created object will run. The values are taken from the enumeration CLSCTX. riid [in]
A reference to the identifier of the interface to be used to communicate with the object. ppv [out]
Address of pointer variable that receives the interface pointer requested in riid. Upon successful return, * ppv contains the requested interface pointer. Upon failure, * ppv contains NULL.

相关下载

代码下载

VS2010调用Com组件的更多相关文章

  1. Titanium中调用ios组件时语言不是本地化的解决方法

    用Titanium开发的ios应用中,当调用系统组件时,尽管手机已经设置了系统语言为中文,但那些组件的界面却仍为英文.比如调用iphone中的相册组件,其界面为: 那么怎么让它跟系统语言保持一致呢? ...

  2. VC中调用COM组件的方法(转载)

    原文参考:http://hi.baidu.com/mingyueye/item/53ebecd44da76917d80e4449 总结一下在VC中调用COM组件的方法 准备及条件: COM服务器为进程 ...

  3. .NET通过调用Office组件导出Word文档

    .NET通过调用Office组件导出Word文档 最近做项目需要实现一个客户端下载word表格的功能,该功能是用户点击"下载表格",服务端将该用户的数据查询出来并生成数据到Word ...

  4. 服务器端调用Word组件读取Word权限、未将对象引用到对象实例终极解决方案

    最近因为业务需要,需要在服务器上调用Word组件,结果遇到各种问题,比如检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败 ...

  5. vc中调用Com组件的方法详解

    vc中调用Com组件的方法详解 转载自:网络,来源未知,如有知晓者请告知我.需求:1.创建myCom.dll,该COM只有一个组件,两个接口:   IGetRes--方法Hello(),   IGet ...

  6. Asp.Net调用Office组件操作时的DCOM配置 (转)

    Asp.Net调用Office组件操作时的DCOM配置 http://blog.csdn.net/gz775/article/details/6447758 在项目中将数据导出为Excel格式时出现“ ...

  7. vue组件之间的通信以及如何在父组件中调用子组件的方法和属性

    在Vue中组件实例之间的作用域是孤立的,以为不能直接在子组件上引用父组件的数据,同时父组件也不能直接使用子组件的数据 一.父组件利用props往子组件传输数据 父组件: <div> < ...

  8. php实现ppt转图片,php调用com组件问题

    PHP 调用com组件将ppt转为图片. 需要在php.ini中开启 extension=php_com_dotnet.dllcom.allow_dcom = true   测试代码如下:   < ...

  9. 【微服务】之五:轻松搞定SpringCloud微服务-调用远程组件Feign

    上一篇文章讲到了负载均衡在Spring Cloud体系中的体现,其实Spring Cloud是提供了多种客户端调用的组件,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使 ...

随机推荐

  1. [USACO2005][POJ3169]Layout(差分约束)

    题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...

  2. struts2升级报ActionContextCleanUp<<is deprecated。Please use the new filters

    把web.xml中配置struts.xml的文件改成 <?xml version="1.0" encoding="UTF-8"?> <web- ...

  3. FZU2082树链剖分

    简单题. #include<queue> #include<stack> #include<cmath> #include<cstdio> #inclu ...

  4. hdu5007 字符串

    字符串问题.是否出现iPhone Apple等词:我考虑时想到既然是否有这些词,可以写map标记一下:然后又最长的是iPhone,6个单词,所以第一个for遍历所有单词 然后在一个for(1~6),用 ...

  5. Struts2的使用以及Spring整合Struts2

    一.如何单独使用Struts2 (1)引入struts2的jar包 commons-fileupload-1.2.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar ...

  6. PLSQL中配置Oracle方法

    在服务器上,用PL/SQL连接Oracle数据库时,出现了一个问题,提示: Initialization error Could not load "F:\oracle\bin\oci.dl ...

  7. Blog Explanation

    有疑问或blog中说明错误的欢迎评论或联系QQ:30056882,邮箱BLADEVIL@outlook.com.本人水平不高,欢迎讨论问题. blog中所有随笔均为原创,转载请注明来源. (已退役.)

  8. BZOJ1093 最大半连通子图

    Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意 两点u,v,存在一条u到v的有向路径或者从v到 ...

  9. POJ1947 Rebuilding Roads

    Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, n ...

  10. POJ1364 King

    Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen p ...