java调用C#写的com组件

1.C#写com组件

开发环境 vs2005

1.新建工程:ClassLibrary1

2.右键点击工程->应用程序->程序集信息->使程序集com可见,打上勾

   右键点击工程->生成->为com Interop注册 打上勾

3.GuidAttribute中的Guid

  通过点击工具->创建GUID->选择4->New Guid->copy->粘贴到此外就行 

4.IPushEvent.cs

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ClassLibrary1
{
    [GuidAttribute(
"D9490AF8-2372-4191-8E63-D0DBC9E6C81D"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    
public interface IPushEvent
    {
        
void SyncRequestEvent(string msg);    
    }
}

5.IPushRender.cs

ContractedBlock.gif ExpandedBlockStart.gif Code

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ClassLibrary1
{
    [GuidAttribute(
"71F09A88-2308-4fcd-9957-0488DD1584FC"),InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    
public interface IPushRender
    {
        
string ResponseRenderAsyc(string content, object userstate);
    }
}

6.

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ClassLibrary1
{
    [GuidAttribute(
"0E99CFC3-741C-406e-9E78-FB82E56C3F1C"),ClassInterface(ClassInterfaceType.AutoDispatch)]
    [ProgId(
"ClassLibrary1.PushServerProvider")]
    [ComSourceInterfaces(
typeof(IPushEvent))]
    
public class PushServerProvider : IPushRender
    {
        
public delegate void RequestDelegate(string msg);
        
public event RequestDelegate SyncRequestEvent; 

        
public string ResponseRenderAsyc(string content, object userstate)
        {
            
string retMessage = "这是从服务器端返回的:" + content;
            OnRequestEvent(retMessage);
            
return retMessage;
        }

        
public void OnRequestEvent(string msg)
        {            
            
if (SyncRequestEvent != null)
            {
                
try
                {
                    SyncRequestEvent(msg);
                }
                
catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                
            }
        }
    }
}

 

6.编译以后,就要以在开发环境中注册了新生成的com组件,在客户环境中,通过regasm.exe注册com组件

regasm.exe的默认安装路径为:C:\Windows\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe

7.可以通过ole/com object viewer可以查看com组件

8.java sdk1.4 通过jacob调用com组件http://danadler.com/jacob/

java测试程序

 

ContractedBlock.gif ExpandedBlockStart.gif Code
import com.jacob.activeX.*;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.jacob.com.*;

public class Test {

    
/**
     * 
@param args
     
*/
    
public static void main(String[] args) {
        
// TODO Auto-generated method stub
        ActiveXComponent mf = new ActiveXComponent("ClassLibrary1.PushServerProvider");
        Object o 
= mf.getObject();
        
        SensorEvents event 
= new SensorEvents();
        
// hook it up to the sControl source
        DispatchEvents de = new DispatchEvents((Dispatch)o, event);
        
        System.out.println(Dispatch.call(mf,
"ResponseRenderAsyc",new Variant( "123"),null));
        mf.safeRelease();
    }

}

 

9.java 事件注册类

 

import com.jacob.com.Variant;  

public   class  SensorEvents {

      
public   void  SyncRequestEvent(Variant[] msg)
      {
         System.
out .println( " java callback for SyncRequestEvent! " );
      }
}

 

在部署时需要将ClassLibrary1.dll和jacob-1.14.3-x86.dll

使用RegAsm.exe 路径\ClassLibrary1.dll /codebase注册dll

Run->Arguments->VM arguments->  -Djava.library.path=jacob-1.14.3-x86.dll 所在的文件夹路径

转载于:https://www.cnblogs.com/liuke209/archive/2009/05/22/1487326.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值