如何实现DCOM或者COM+的远程调用

      当DCOM/COM+在本机调用时,往往问题比较简单。如果需要远程调用,往往会发生很多问题,比如80070005(Acess is denied),80040155( Interface not registered)等等问题,因为涉及到跨机器,所以会涉及到权限,注册表配置等等的问题。同时,不同类型的客户端(指C++类型等编译型客户,或者是vbscript等解释型客户)远程调用DCOM 时,客户端所要求的配置不尽相同。本文介绍的正对编译型客户远程调用DCOM的示例。

      首先,在客户端调用远程调用的时候,需要在客户端进行的配置有如下几种选择:

      选择1. 通过nmake -f <dcomservice>.mk生成相应的proxy dll,然后将该proxy dll(一般为<dcomservice>ps.dll)部署在客户段,并通过regsvr32 <dcomservice>ps.dll进行注册,即可实现在客户端进行远程调用
      选择2. 将<dcomservice>.tlb文件发布到客户端,然后运行regtlib (regtlibv12) <dcomservice>.tlb进行注册,也可实现在客户端的远程调用。

下面给出客户端调用代码(VC6.0):
注意:如果调用CoCreateInstanceEx API,必须开启一个_WIN32_DCOM Switch.我们可以在stdafx.h里面加入

#define _WIN32_DCOM  

或者在preprocess里面加入该值,如下所示:

//  DComServiceClient.cpp : Defines the entry point for the console application.
//

#include 
" stdafx.h "
#include 
" ..\DComDemoV1.h "
#include 
" ..\DComDemoV1_i.c "
#include 
< atlbase.h >
#include 
< comdef.h >
#include 
< comutil.h >

int  main( int  argc,  char *  argv[])
{
    
/*  CreateInstance Locally via SmartPointer;
    CComPtr<ISimpleClass> spSimpleClass;
    CoInitialize(NULL);
    HRESULT hr;
    hr=spSimpleClass.CoCreateInstance(__uuidof(SimpleClass));
    if(FAILED(hr))  {printf("Error code:%x",hr);return hr;}

    BSTR result;
    hr=spSimpleClass->HelloWorld(&result);
    if(FAILED(hr))  {printf("Error code:%x",hr);return hr;}

    char* p=::_com_util::ConvertBSTRToString(result);
    printf("%s\n",p);

    ::SysFreeString(result);
    CoUninitialize();
    return 0;
    
*/
// ====================================================
     /* CreateInstance locally via primitive pointer;
    ISimpleClass* pSimpleClass;
    CoInitialize(NULL);
    HRESULT hr;
    hr=CoCreateInstance(CLSID_SimpleClass,0,CLSCTX_LOCAL_SERVER,IID_ISimpleClass,(void**)&pSimpleClass);
    if(FAILED(hr)) return hr;

    BSTR result;
    hr=pSimpleClass->HelloWorld(&result);
    char* p=::_com_util::ConvertBSTRToString(result);
    printf("%s\n",p);

    pSimpleClass->Release();
    CoUninitialize();
    return 0;
    
*/

    
// call remotely
    
    HRESULT hr;
    ISimpleClass 
* pI = NULL;

    
    COSERVERINFO sin,
* sinptr;

    MULTI_QI mqi;
    mqi.pIID
=& IID_ISimpleClass;
    mqi.hr
= 0 ;
    mqi.pItf
= 0 ;

    COAUTHINFO    authInfo;
    authInfo.dwAuthnSvc 
=  RPC_C_AUTHN_WINNT; // RPC_C_AUTHN_NONE;
    
    authInfo.dwAuthzSvc 
=  RPC_C_AUTHZ_NONE;
    authInfo.pwszServerPrincName 
=  NULL;
    authInfo.dwAuthnLevel 
=  RPC_C_AUTHN_LEVEL_PKT ; // RPC_C_AUTHN_LEVEL_NONE;
    
    authInfo.dwImpersonationLevel 
= RPC_C_IMP_LEVEL_IMPERSONATE; //  RPC_C_IMP_LEVEL_ANONYMOUS;
    
    authInfo.pAuthIdentityData 
=  NULL;
    authInfo.dwCapabilities 
=  NULL;
    

    sin.dwReserved1
= 0 ;
    sin.dwReserved2
= 0 ;
    sin.pwszName
= L " azalea-desk " ; // define the remote server name here
    sin.pAuthInfo =& authInfo;
    sinptr
=& sin;
    
    hr
= CoInitialize( 0 );
    
if (SUCCEEDED(hr))
    {
        
        hr
= CoCreateInstanceEx(CLSID_SimpleClass,
                            NULL,
                            CLSCTX_REMOTE_SERVER,
                            sinptr,
                            
1 ,
                            
& mqi
                            );                            
    

        
if (SUCCEEDED(hr))
        {
        pI
= (ISimpleClass * )mqi.pItf;
        printf(
" Dcom server connect\n " );
        BSTR bsReturnValue;
    
        pI
-> HelloWorld( & bsReturnValue);
        pI
-> Release();
        
char *  pValue = _com_util::ConvertBSTRToString(bsReturnValue);
        printf(
" %s\n " ,pValue);    
        delete pValue;
    
        }
        
else
        {
            printf(
" CreateInstance Error!Error Code:%x\n " ,hr);
        }
    }
    
else
    {
        printf(
" CoInitialize Error!Error Code:%x\n " ,hr);
    }

    CoUninitialize();
    
return   0 ;
}

 

该DCOM Service只定义了一个HelloWorld()返回字符串的method.客户端调用成功后的screenshot如下:

点击此处下载源代码

转载于:https://www.cnblogs.com/Winston/archive/2009/06/04/1496411.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue中使用vtk.js和itk.js进行dcom影像预览,可以按照以下步骤进行操作: 1. 安装必要的依赖项 需要安装Vue、vtk.js、itk.js和其他必要的依赖项。可以使用npm或yarn命令进行安装。 ``` npm install vue vtk-js itk-vtk-viewer axios ``` 2. 创建Vue组件 在Vue组件中创建一个div元素来显示影像。使用itk.js创建一个ImageViewer和一个ImageIO,并将其添加到vtk.js的RenderWindow中。然后使用axios从DCOM服务器获取DICOM数据,并将其加载到ImageIO中。 ```html <template> <div ref="container"></div> </template> <script> import vtk from 'vtk.js'; import ITK from 'itk'; import vtkITKHelper from 'vtk.js/Sources/Common/DataModel/ITKHelper'; import vtkITKImageReader from 'vtk.js/Sources/IO/Misc/ITKImageReader'; import vtkITKImageViewer from 'vtk.js/Sources/Interaction/Widgets/ITK/ITKImageViewer'; export default { name: 'DCOMViewer', data() { return { imageData: null, }; }, mounted() { const container = this.$refs.container; // Create a vtkRenderWindow const renderWindow = vtk.Rendering.Core.vtkRenderWindow.newInstance(); renderWindow.setSize(container.offsetWidth, container.offsetHeight); // Create an ITK ImageIO const imageIO = ITK.vtkITKImageIO.newInstance(); // Load DICOM data from DCOM server using axios axios.get('http://dcom-server.com/dicom-data').then((response) => { // Convert DICOM data to Uint8Array const data = new Uint8Array(response.data); // Read the image data using ITK ImageIO ITK.IoPipeline.ImageIO.readBufferAsImage(imageIO, data).then((image) => { // Convert the ITK image to a vtkImageData const imageData = vtkITKHelper.convertItkToVtkImage(image); // Create a vtkITKImageReader and set its input to the vtkImageData const imageReader = vtkITKImageReader.newInstance(); imageReader.setFileName('dummy'); imageReader.setBlob(imageData); imageReader.setUseCompression(false); // Create a vtkITKImageViewer and set its input to the vtkITKImageReader const imageViewer = vtkITKImageViewer.newInstance(); imageViewer.setRenderWindow(renderWindow); imageViewer.setInput(imageReader); // Add the image viewer to the container imageViewer.setContainer(container); imageViewer.renderLater(); }); }); }, }; </script> ``` 3. 使用Vue组件 将Vue组件添加到Vue应用程序中并使用它来显示DCOM影像。 ```html <template> <div> <DCOMViewer /> </div> </template> <script> import DCOMViewer from '@/components/DCOMViewer'; export default { name: 'App', components: { DCOMViewer, }, }; </script> ``` 这样就可以使用Vue、vtk.js和itk.js进行DCOM影像预览了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值