1)使用C++语言实现进程外组件,组建提供加、减、乘、除、判断是否素数等计算服务;客户端部分包括录入和查询部分。
2)在VC++环境上利用ATL向导进行开发。
2.进程外组件
服务器端:
创建ATL工程:VC++->file->new->ATLCOMAppWizar->服务类型选服务(service),如下图所示:
添加com对象,Insert-NewATLObject,添加shortname:calmath,其他则会自动添加;
在工作空间中,选择com接口(即Icalmath),右键->ADDmethods,添加提供的服务(方法)
输入MethodName:add
Parameters:[in]intNum1,[in]intNum2,[out]int*result
如下图所示:
按照同样方法,添加方法:sub,multiply,divide,sushu;
如下图所示:
打开calmath.cpp文件(双击添加的方法即可打开),写入实现方法的代码:
calmath的代码如下:
//calmath.cpp:ImplementationofCcalmath
#include"stdafx.h"
#include"OutCalculator.h"
#include"calmath.h"
/////////////////////////////////////////////////////////////////////////////
//Ccalmath
intissushu(ints)
{
inti;
if(s==2||s==1)return1;
for(i=2;i<sqrt(s);i++)
{
if(s%i==0)return0;
}
return1;
}
STDMETHODIMPCcalmath::add(intNum1,intNum2,int*result)
{
//TODO:Addyourimplementationcodehere
*result=Num1+Num2;
returnS_OK;
}
STDMETHODIMPCcalmath::sub(intNum1,intNum2,int*result)
{
//TODO:Addyourimplementationcodehere
*result=Num1-Num2;
returnS_OK;
}
STDMETHODIMPCcalmath::multiply(longNum1,longNum2,long*result)
{
//TODO:Addyourimplementationcodehere
*result=Num1*Num2;
returnS_OK;
}
STDMETHODIMPCcalmath::divide(longNum1,longNum2,long*result)
{
//TODO:Addyourimplementationcodehere
*result=Num1/Num2;
returnS_OK;
}
STDMETHODIMPCcalmath::sushu(intNum,int*result)
{
//TODO:Addyourimplementationcodehere
*result=issushu(Num1);
returnS_OK;
}
编译连接
在工程debug目录中可找到OutCalculator.exe组件
使用OutCalculator.exe/RegServer/Service注册服务
netstartOutCalculator启动服务
如下图所示:
客户端:
新建一个win32consoleapplication工程,工程名称为:OutClient,工程目录与服务器端工程目录相同;
新建一个cpp文件,名称为:client.cpp,保存到当前工程中;
编写client.cpp的代码:
//#define_WIN32_WINNT0x0500
#define_WIN32_DCOM
#include<windows.h>
#include<iostream>
//#import"ComputeService.exe"no_namespacenamed_guids
#include"../OutCalculator/OutCalculator.h"
#include"../OutCalculator/OutCalculator_i.c"
usingnamespacestd;
template<classT>
voidshow(Tt)
{
cout<<t<<endl;
}
voidtest_dcom1()
{
HRESULThr;
hr=CoInitializeSecurity(NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_NONE,
RPC_C_IMP_LEVEL_IDENTIFY,
NULL,
EOAC_NONE,
NULL);
if(!SUCCEEDED(hr))
{
show("initrightfailed!");
}
COSERVERINFOsi;
MULTI_QIqi;
ZeroMemory(&si,sizeof(si));
ZeroMemory(&qi,sizeof(qi));
si.pwszName=L"192.168.4.143";//自己机器的IP
si.pAuthInfo=NULL;
qi.pIID=&IID_Icalmath;
qi.pItf=NULL;
hr=CoCreateInstanceEx(CLSID_calmath,NULL,CLSCTX_REMOTE_SERVER,&si,1,&qi);
if(FAILED(hr))
{
cout<<"cannotcreatemyobject:"<<GetLastError()<<endl;
return;
}
if(FAILED(qi.hr))
{
cout<<"cannotcreatemyobject:"<<GetLastError()<<endl;
return;
}
////////////////////////////////////
Icalmath*pT=NULL;
qi.pItf->QueryInterface(&pT);
qi.pItf->Release();
intdata=0;
longresult=0.0;
intflag;
inta,b;
printf("请选择:1.加;2.减;3.乘;4.除;5.判断素数(注:1表示素数;0表示非素数);0.退出\n");
scanf("%d",&flag);
while(flag!=0){
switch(flag){
case1:printf("请输入要相加的两个数字:");
scanf("%d%d",&a,&b);
pT->add(a,b,&data);
printf("计算结果为:");
show(data);
break;
case2:printf("请输入要相减的两个数字:");
scanf("%d%d",&a,&b);
pT->sub(a,b,&data);
printf("计算结果为:");
show(data);
break;
case3:printf("请输入要相乘的两个数字:");
scanf("%d%d",&a,&b);
pT->multiply(a,b,&result);
printf("计算结果为:");
show(result);
break;
case4:printf("请输入要相除的两个数字:");
scanf("%d%d",&a,&b);
pT->divide(a,b,&result);
printf("计算结果为:");
show(result);
break;
case5:printf("请输入要进行判断的数字:");
scanf("%d",&a);
pT->sushu(a,&data);
printf("判断结果为:");
show(data);
break;
case0:break;
default:break;
}
printf("请选择:1.加;2.减;3.乘;4.除;5.判断素数(注:1表示素数;0表示非素数);0.退出\n");
scanf("%d",&flag);
}
pT->Release();
}
voidmain()
{
CoInitialize(NULL);
test_dcom1();
CoUninitialize();
}
进行编译连接,执行,如下所示:
分享到:
相关推荐
1)使用C++语言实现进程内组件、进程外组件、远程进程外组件,组建提供加、减、乘、除、判断是否素数等计算服务;客户端部分包括录入和查询部分。 2)在VC++环境上利用ATL向导进行开发。 使用VC++的ATL编程实现...
本书以实例进行教学、循序渐进的讲述了COM/DCOM的概念与编程技巧。 全书共分13章,内容包括:Microsoft 对象技术概述、对象的演变、创建COM对象和接口、实现COM客户和服务器、使用MFC进行以COM编程、使用聚合模拟...
com/dcom技术内幕例程
COM/DCOM Exception ClassCOM/DCOM异常类
SkDCOMbridge : COM/DCOM Handler ObjectCOM/DCOM处理对象
com/dcom论文,com/dcom论文
COM分为进程内和进程外两种,但都是在一台机子上运行.DCOM是将COM变为可通过网络调用.COM+是在DCOM基础上加上了导出MSI和事务的概念 COM中用的字符串是BSTR 分布式多层数据库开发_关于建立COM 时的几个选项现在说明...
• Unleashes the power of COM/DCOM in the context of practical advanced solutions. • Uncovers ways in which COM/DCOM can be used that perhaps the reader never imagined were possible. • Removes the ...
COM/DCOM编程指南一书配套源代码(完整的源代码)
DCOM应用程序实例——ATM和Bank,DCOM应用程序实例——ATM和Bank,DCOM应用程序实例——ATM和Bank
COM&DCOM编程指南,看着不错,介绍给大家吧。
全书以组件与分布式应用开发为主题,贯穿实例深入浅出地介绍了Delphi 7支持的组件技术、VCL库的扩充的一般方法、可视组件的开发与发布、ActiveX控件技术、COM/DCOM/COM+的基本概念与原理、在Delphi 7中使用...
#资源达人分享计划#
DCOM Technical Overview 微软的DCOM英文文档
COM&DCOM Programming(含源码)
北京科海培训中心 COM~DCOM编程指南 (230页以后较模糊).zip
COM的技术的发展历程及相关概念 构件对象模型COM/DCOM 基于COM构件的开发
Visual C++ 6.0 DCOM开发指南.pdf,DCOM技术使用教程
Visual C++ 6 DCOM开发指南.part1 Visual C++ 6 DCOM开发指南.part2 共两部分
DCOM技术开发教程,一个很不错的PPT文档,细致,清晰。