用VC++编写DCOM程序以及DCOM的配置方法.doc_第1页
用VC++编写DCOM程序以及DCOM的配置方法.doc_第2页
用VC++编写DCOM程序以及DCOM的配置方法.doc_第3页
用VC++编写DCOM程序以及DCOM的配置方法.doc_第4页
用VC++编写DCOM程序以及DCOM的配置方法.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

怎样用VC+编写DCOM程序 于玲 2001.2.10一、 建立服务器程序:1、 用VC+建立一个名为Test的workspace。2、 在workspace中加入一个新的工程,选ATL COM Appwizard项,命名为AddSub,下一步server类型选service(EXE)。3、 添加组件对象:打开InsertNew ATL Object,选Simple Object,在names下shortname填AddSubCom,如果需要可以将interface改为IAdd(默认为IAddSub),在attributes下interface选custom,则该com组件接口将从IUnknown继承,如果aggregation选yes,则该组件可被聚合。4、 a) 组件接口函数的定义:打开AddSub.idl文件,在接口IAdd下填写接口函数的声明:interface IAdd : IUnknown/接口函数的声明HRESULT AddMatrix(in int length, in,size_is(length) double* pfMatrix, in,size_is(length) double* psMatrix, out,size_is(length) double* prMatrix); ;另一种方法是:在workspace的class标签中,右键点击IAdd,选Add Method,在对话框中添加。b)组件接口函数的声明(如果在上一步中使用第二种方法添加,则无需此步):打开AddSubCom.h文件,在类声明后面加入:STDMETHOD(AddMatrix)(int length,double* pfMatrix,double* psMatrix,double* prMatrix);c) 组件接口函数的实现:打开AddSubCom.cpp文件,加入接口方法的实现函数:STDMETHODIMP CAddSubCom:AddMatrix(int length,double* pfMatrix,double* psMatrix,double*prMatrix) /函数功能return NOERROR;5、 组件注册:一般地,编译完成服务器自动注册;如果使用编译好的服务器,注册方法是用/Register参数执行服务器程序,如C:MyDirAddSub /RegServer,解注册用/UnregServer(参见AddSub.cpp源码)。二、 建立代理/存根DLL1、 建立一个新的工程命名为PSAddSub,工程选Win32 Dynamic-Link Library,其余默认。2、 在工程中新建一个文本文件PSAddSub.def,内容如下: LIBRARY PSAddSubEXPORTS DllGetClassObject PRIVATE DllCanUnloadNow PRIVATE DllRegisterServer PRIVATE DllUnregisterServer PRIVATE GetProxyDllInfo PRIVATE3、 将AddSub.idl生成的AddSub.h、AddSub_i.c、AddSub_p.c、dlldata.c四个文件加入工程。4、 projectsettingc/c+中category Preprocessor definitions加入REGISTER_PROXY_DLL, _WIN32_DCOM, projectsettinglink中Object/library modules加入rpcndr.lib rpcns4.lib rpcrt4.lib ,然后编译,生成PSAddSub.dll。5、 注册dll:c:MyDirregsvr32 PSAddSub.dll;也可以用VC+的toolsRegister Control。n dictionary.h 包含接口说明的头文件,可用于C或者C+语言;n dictionary_p.c 该C文件实现了接口IDictionary的代理和存根;n dictionary_i.c 该C文件定义了IDL文件中用到的所有全局描述符GUID,包括接口描述符;n dlldata.c 该C文件包含代理/存根程序的入口函数以及代理类厂所需要的数据结构等。三、 建立客户程序在客户端主程序中加入如下程序段:#include #include #include #include ./Server/Server.hint main()CString *pszHostName=new CString(125.223.8.77);/char *pszHostName=125.223.8.77;/pszHostName=;HRESULT hResult;/ Prepare COM for usehResult = CoInitialize(NULL);/初始化COM库if (FAILED(hResult) return 0;IClassFactory *pClf;IUnknown *pIUnknown;LPOLESTR lpszHostname= pszHostName-AllocSysString();COSERVERINFO ServerInfo = 0, lpszHostname, NULL, 0 ;/获取服务器信息hResult=CoGetClassObject(CLSID_AddSubCom,CLSCTX_SERVER,&ServerInfo,IID_IClassFactory,(void*)&pClf);/创建类厂if (FAILED(hResult) return 0;hResult = pClf-CreateInstance(NULL,IID_IUnknown,(void*) &pIUnknown);/创建组件对象 if (FAILED(hResult) pClf-Release();return 0;pClf-Release();/释放类厂IAddSubCom *pIAddMatrix;hResult = pIUnknown-QueryInterface(IID_IAddSubCom, (void*) &pIAddMatrix);/查询接口if (FAILED(hResult) pIUnknown-Release();return 0;/call functiondouble pfSubMatrix10, psSubMatrix10, prSubMatrix10 ;for(int i=0; iAddMatrix(SubMatrixLength,pfSubMatrix,psSubMatrix,prSubMatrix);/调用接口函数 pIAddMatrix-Release();/we have done with the IAddMatrix interface/ Were done with the Matrix objectpIUnknown-Release();pIUnknown = NULL;CoUninitialize();/释放COM库for(i=0; iSubMatrixLength ;i+)printf(%ft, prSubMatrixi) ;printf(n) ;return 0 ;客户端工程必须包含AddSub.h和AddSub_i.c文件,对于AddSub_i.c文件projectsettingc/c+中Precompiled Headers应选Automatic use of precompiled headers。注意:在客户端,无论编写debug还是release程序,projectsettingc/c+中的code generation下的Use run-time library应选DEBUG Multithreaded DLL。四、 客户调用服务器客户可以远程调用服务程序,代理/存根DLL必须在两端都有副本,并且注册,这样客户就可以通过代理/存根DLL调用远端的服务器了。五、 易出现的错误: C:AddSubSvr.cpp(8) : fatal error C1852: ReleaseMinDependency/AddSubSvr.pch is not a valid precompiled header fileError executing cl.exe.解决:对于AddSubSvr.cpp文件,projectsettingc/c+中Precompiled Headers应选Automatic use of precompiled headers。此错误易在从新编译时出现。Linking.LIBC.lib(crt0.obj) : error LNK2001: unresolved external symbol _mainReleaseMinDependency/MutSvr.exe : fatal error LNK1120: 1 unresolved externalsError executing link.exe.解决:对于MutSvr工程,projectsettingc/c+中Preprecessor definitions,将_ATL_MIN_CRT删去。#include #include #include #include ./Server/Server.husing namespace std ;typedef struct tarParamchar ip20 ;double *coe ;long power ;double a ;double b ;double eps ;double root ;int result ;param ;DWORD WINAPI run(LPVOID pParams)param* pp = (param *)pParams ;HRESULT hResult;hResult = CoInitialize(NULL);/初始化COM库if (FAILED(hResult) return 0;IClassFactory *pClf;IUnknown *pIUnknown;wchar_t ip42 ;int len = strlen(pp-ip) ;mbstowcs(ip, pp-ip, len); /转换单字符串为宽字符串iplen = 0 ;/设置字符串结尾COSERVERINFO ServerInfo = 0, ip, NULL, 0 ;/获取服务器信息hResult=CoGetClassObject(CLSID_Polynomial,CLSCTX_SERVER,&ServerInfo,IID_IClassFactory,(void*)&pClf);/创建类厂if (FAILED(hResult) return 0;hResult = pClf-CreateInstance(NULL,IID_IUnknown,(void*) &pIUnknown);/创建组件对象 if (FAILED(hResult) pClf-Release();return 0;pClf-Release();/释放类厂IPolynomial *pIPolynomial;hResult = pIUnknown-QueryInterface(IID_IPolynomial, (void*) &pIPolynomial);/查询接口if (FAILED(hResult) pIUnknown-Release();return 0;unsigned long time=GetTickCount();/调用接口函数pp-result = pIPolynomial-polynomial_root(pp-coe, pp-power, pp-a, pp-b, pp-eps, &pp-root) ;/*if(pp-result)coutsetiosflags(ios:fixed)setprecision(10)rootendl ;else coutnot found!Release();pIUnknown-Release();pIUnknown = NULL;CoUninitialize();/释放COM库return 0;int main()ifstream infile(ip.txt, ios:in) ;char ip2020 =0;param machine20 ;int num ;int parallelNum ;int i = 0 ;HANDLE MThread20;/读取服务器IP地址doinfilemachinei.ip ;while( strlen(machinei.ip)5 & i+0 )cout当前的处理机有num台: endl ;i=0 ;while( inum )coutmachinei+.ipendl ;coutparallelNum ;if( parallelNumnum )/输入的并行数大于处理机数return -1 ;i = 0 ;double coe= 10,1,2,3,4,5,6,7;unsigned long parallel_time=GetTickCount(); double a =-1000 ;double b = 1000 ;double step = (b-a)/parallelNum ;while( iparallelNum )

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论