本文主要是介绍COM/DCOM开发之进程内组件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一 目的
使用VC++的ATL编程实现进程内组件。同时实现客户端对组件的调用。
二 要求
1)使用C++语言实现进程内组件,组建提供加、减、乘、除、判断是否素数等计算服务;客户端部分包括录入和查询部分。
2)在VC++环境上利用ATL向导进行开发。
三 过程
服务器端:
创建ATL工程:VC++->file->new->ATL COM AppWizar->服务类型选服务(DLL)
工程名为comin
添加com对象: 插入( Insert)–>ATL 对象(New ATL Object)
选择Simple Object,点击next
添加shortname:calmath,其他则会自动添加:
在工作空间中,选择com接口(即Icalmath),右键->ADD methods,添加提供的服务(方法),在接口对应的类中实现方法。
添加add方法,如图:
添加sub方法,如图:
添加multiply方法,如图:
添加divide方法,如图:
添加sushu方法,如图:
编写calmath.cpp的代码
工作区切换到FileView,打开calmath.cpp文件,写入实现方法的代码,calmath.cpp代码如下:
// calmath.cpp : Implementation of Ccalmath
#include "stdafx.h"
#include "Comin.h" //这里自动生成的,一般为工程名
#include "calmath.h"
#include<math.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(Num); return S_OK;
}
编译连接
在工程debug目录中可找到comin.dll组件,在命令行进入该目录
运行命令regsvr32 comin.dll注册组件
出现错误:
解决办法:
以管理员身份运行命令regsvr32 comin.dll注册组件
注册成功
客户端:
客户端需要包含服务端编译IDL时生成的comin.h文件和comin_i.c文件,在服务端的工程文件夹中可找到。
在相应的工程下,在VC环境中,选择文件->新建->工程->win32 console Application,工程名称为:InClient,然后选择“添加到当前工作空间”,从属于一栏打上勾;然后确定。如图:
在该工程中新建inclient.cpp,编写其代码:
//#define _WIN32_WINNT 0x0500
#define _WIN32_DCOM
#include <windows.h>
#include <iostream>
//#import "ComputeService.exe" no_namespace named_guids
#include "../comin.h" //这里是你的服务端工程的文件名
#include "../comin_i.c" //这里是你的服务端工程的文件名
using namespace std;
template< class T >
void show( T t )
{
cout << t << endl;
}
void test_dcom1()
{ //初始化COM库
HRESULT hr=::CoInitialize(NULL);
if( !SUCCEEDED( hr ) )
{
show( "init right failed!" );
}
Icalmath * plRF = NULL;
hr=::CoCreateInstance(CLSID_calmath,NULL,CLSCTX_INPROC_SERVER,IID_Icalmath,(void**)&plRF);
if(FAILED(hr))
{
cout<<"创建组件实例失败"<<endl;
}
//调用方法
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);plRF->add(a,b,&data);printf("计算结果为:");show( data );break;case 2: printf("请输入要相减的两个数字:") ;scanf("%d%d",&a,&b);plRF->sub(a,b,&data);printf("计算结果为:");show( data );break;case 3: printf("请输入要相乘的两个数字:") ;scanf("%d%d",&a,&b);plRF->multiply(a,b,&result);printf("计算结果为:");show( result );break;
case 4: printf("请输入要相除的两个数字:") ;scanf("%d%d",&a,&b);plRF->divide(a,b,&result);printf("计算结果为:");show( result );break;
case 5: printf("请输入要进行判断的数字:") ;scanf("%d",&a);plRF->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();
}
对程序进行编译,连接,运行,结果如图:
这篇关于COM/DCOM开发之进程内组件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!