1)使用C++语言实现进程外组件,组建提供加、减、乘、除、判断是否素数等计算服务;客户端部分包括录入和查询部分。
2)在VC++环境上利用ATL向导进行开发。
2.进程外组件
服务器端:
创建ATL工程:VC++->file->new->ATL COM AppWizar->服务类型选服务(service),如下图所示:
添加com对象, Insert-New ATL Object,添加shortname:calmath,其他则会自动添加;
在工作空间中,选择com接口(即Icalmath),右键->ADD methods,添加提供的服务(方法)
输入Method Name:add
Parameters:[in] int Num1, [in] int Num2, [out] int *result
如下图所示:
按照同样方法,添加方法:sub,multiply,divide,sushu;
如下图所示:
打开calmath.cpp文件(双击添加的方法即可打开),写入实现方法的代码:
calmath的代码如下:
// calmath.cpp : Implementation of Ccalmath
#include "stdafx.h"
#include "OutCalculator.h"
#include "calmath.h"
/
// Ccalmath
int issushu(int s)
{
int i;
if(s==2||s==1) return 1;
for(i=2;i<sqrt(s);i++)
{
if(s%i==0) return 0;
}
return 1;
}
STDMETHODIMP Ccalmath::add(int Num1, int Num2, int *result)
{
// TODO: Add your implementation code here
*result=Num1+Num2;
return S_OK;
}
STDMETHODIMP Ccalmath::sub(int Num1, int Num2, int *result)
{
// TODO: Add your implementation code here
*result=Num1-Num2;
return S_OK;
}
STDMETHODIMP Ccalmath::multiply(long Num1, long Num2, long *result)
{
// TODO: Add your implementation code here
*result=Num1*Num2;
return S_OK;
}
STDMETHODIMP Ccalmath::divide(long Num1, long Num2, long *result)
{
// TODO: Add your implementation code here
*result=Num1/Num2;
return S_OK;
}
STDMETHODIMP Ccalmath::sushu(int Num, int *result)
{
// TODO: Add your implementation code here
*result=issushu(Num1);
return S_OK;
}
编译连接
在工程debug目录中可找到OutCalculator.exe组件
使用OutCalculator.exe/RegServer /Service 注册服务
net start OutCalculator 启动服务
如下图所示:
客户端:
新建一个win32 console application工程,工程名称为:OutClient,工程目录与服务器端工程目录相同;
新建一个cpp文件,名称为:client.cpp,保存到当前工程中;
编写client.cpp的代码:
//#define _WIN32_WINNT 0x0500
#define _WIN32_DCOM
#include <windows.h>
#include <iostream>
//#import "ComputeService.exe" no_namespace named_guids
#include "../OutCalculator/OutCalculator.h"
#include "../OutCalculator/OutCalculator_i.c"
using namespace std;
template< class T >
void show( T t )
{
cout << t << endl;
}
void test_dcom1()
{
HRESULT hr;
hr = CoInitializeSecurity( NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_NONE,
RPC_C_IMP_LEVEL_IDENTIFY,
NULL,
EOAC_NONE,
NULL );
if( !SUCCEEDED( hr ) )
{
show( "init right failed!" );
}
COSERVERINFO si;
MULTI_QI qi;
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 << "can not create myobject : " << GetLastError() << endl;
return;
}
if (FAILED(qi.hr))
{
cout << "can not create myobject : " << GetLastError() << endl;
return;
}
Icalmath * pT = NULL;
qi.pItf->QueryInterface( &pT );
qi.pItf->Release();
int data = 0;
long result=0.0;
int flag;
int a,b;
printf("请选择:1.加;2.减;3.乘;4.除;5.判断素数(注:1表示素数;0表示非素数);0.退出\n");
scanf("%d",&flag);
while(flag!=0){
switch(flag){
case 1: printf("请输入要相加的两个数字:") ;
scanf("%d%d",&a,&b);
pT->add(a,b,&data);
printf("计算结果为:");
show( data );
break;
case 2: printf("请输入要相减的两个数字:") ;
scanf("%d%d",&a,&b);
pT->sub(a,b,&data);
printf("计算结果为:");
show( data );
break;
case 3: printf("请输入要相乘的两个数字:") ;
scanf("%d%d",&a,&b);
pT->multiply(a,b,&result);
printf("计算结果为:");
show( result );
break;
case 4: printf("请输入要相除的两个数字:") ;
scanf("%d%d",&a,&b);
pT->divide(a,b,&result);
printf("计算结果为:");
show( result );
break;
case 5: printf("请输入要进行判断的数字:") ;
scanf("%d",&a);
pT->sushu(a,&data);
printf("判断结果为:");
show( data );
break;
case 0:break;
default:break;
}
printf("请选择:1.加;2.减;3.乘;4.除;5.判断素数(注:1表示素数;0表示非素数);0.退出\n");
scanf("%d",&flag);
}
pT->Release();
}
void main()
{
CoInitialize( NULL );
test_dcom1();
CoUninitialize();
}
进行编译连接,执行,如下所示: