巧用C#做中间语言 实现Java调用.net DLL的方法

环境与工具: ◆.net framework 3.5 C# ◆java jdk1.5, Tomcat 5.5 ◆jacob-1.15-M3 实现例子: 一、C# 制作Com组件 新建一个Class 项目,取名TestCom 代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace TestCom { [Guid("E9BCF867-CD81-40d0-9591-ED28D1ED2B53")] public interface IEncrypt { [DispId(1)] string GetEncrypt(string str,string str2); } [Guid("33A6E58D-E1F5-4b53-B2E2-03B9F8BA2FAD"), ClassInterface(ClassInterfaceType.None)] public class Encrypt:IEncrypt { public Encrypt(){} public string GetEncrypt(string str,string str2) { return "测试 | "+str+"|"+str2; } } } 打开 Project--> Properties菜单 在Application标签中打开 Assembly Information 将Make assembly Com-Visible一项选中。再切换到Build标签将 Register for COM interop一项选中。 Guid的生成:打开Visual Studio Command Prompt 输入guidgen 命令调出工具。类型选择Registry Format,点击New Guid,然后COPY出来。 [DispId(1)]为函数的标识。如果有多个函数可相应的在函数前面加[DispId(2)], [DispId(3)]… 编译程序Debug目录中会生成 TestCom.dll 和TestCom.tlb 手工注册Com方法: 打开Visual Studio Command Prompt进入Debug目录,运行命令注册:regasm TestCom.DLL /tlb:TestCom.tlb  二、Java调用Com 部署jacob ◆在开发环境中引入jacob.jar ◆拷贝jacob-1.15-M3-x86.dll 文件到 C:/Windows/System32目录,如果是Web应用的话还需要拷贝到jdk1.5.0_16/bin目录(jdk安装目录下的bin目录) java调用代码 代码 import com.jacob.activeX.ActiveXComponent; import com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try{ ActiveXComponent dotnetCom = null; dotnetCom = new ActiveXComponent("TestCom.Encrypt"); Variant var = Dispatch.call(dotnetCom,"GetEncrypt","哥是第一个参数","哥是第二个参数"); String str = var.toString(); //返回值 } catch (Exception ex) { ex.printStackTrace(); } } } 这便完成了Java调用.net DLL的方法! 

转载于:https://www.cnblogs.com/huangjihua/archive/2011/04/15/4125189.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值