唐山网站建设

设为主页 加入收藏 繁體中文

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

核心提示:本文将具体为大家先容1个java调用.net DLL的方法,以实现特殊的客户的特殊要求:“在Java项目中必须使用其提供的用.net写的DLL加密机制!”

本文将具体为大家先容1个java调用.net DLL的方法,以实现特殊的客户的特殊要求:“在Java项目中必须使用其提供的用.net写的DLL加密机制!”

环境与工具:

◆.net framework 3.5 C#

◆java jdk1.5, Tomcat 5.5

◆jacob⑴.15-M3

实现例子:

1、C# 制作Com组件

新建1个Class 项目,取名TestCom

代码

using System;  using System.Collections.Generic; 
using System.Linq;  using System.Text; 
using System.Runtime.InteropServices;  
namespace TestCom 
{      [Guid("E9BCF867-CD81⑷0d0⑼591-ED28D1ED2B53")]     
public inte***ce IEncrypt     
{          [DispId(1)]          
string GetEncrypt(string str,string str2);     
}     
[Guid("33A6E58D-E1F5⑷b53-B2E2-03B9F8BA2FAD"),
ClassInte***ce(ClassInte***ceType.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-Visible1项选中。再切换到Build标签将 Register for COM interop1项选中。

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

2、Java调用Com

部署jacob

◆在开发环境中引进jacob.jar

◆拷贝jacob⑴.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","哥是第1个参数","哥是第2个参数");             
String str  = var.toString(); //返回值             
} catch (Exception ex)
{                 
ex.printStackTrace();             
}     
}  
}

这便完成了Java调用.net DLL的方法!

http://www.fw8.net/


TAG:方法,代码,目录,函数,项目
评论加载中...
内容:
评论者: 验证码: