环境与工具: ◆。net framework 3。5 C# ◆java jdk1。5, Tomcat 5。5 ◆jacob-1。15-M3 实现例子: 一、C# 制作Com组件 新建一个Class 项目,取名TestCom 代码 1。
using System; 2。using System。Collections。Generic; 3。using System。Linq; 4。using System。Text; 5。using System。Runtime。InteropServices; 6。
7。namespace TestCom 8。{ 9。 [Guid("E9BCF867-CD81-40d0-9591-ED28D1ED2B53")] 10。 public interface IEncrypt 11。 { 12。
[DispId(1)] 13。 string GetEncrypt(string str,string str2); 14。 } 15。 [Guid("33A6E58D-E1F5-4b53-B2E2-03B9F8BA2FAD"), ClassInterface(ClassInterfaceType。
None)] 16。 public class Encrypt:IEncrypt 17。 { 18。 public Encrypt(){} 19。 20。 public string GetEncrypt(string str,string str2) 21。
{ 22。 23。 return "测试 | "+str+"|"+str2; 24。 } 25。 } 26。} 打开 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调用代码 代码 27。import com。jacob。activeX。ActiveXComponent; 28。import com。jacob。com。ComThread; 29。
import com。jacob。com。Dispatch; 30。import com。jacob。com。Variant; 31。public class test { 32。 33。 /** 34。 * @param args 35。
*/ 36。 public static void main(String[] args) { 37。 // TODO Auto-generated method stub 38。 try{ 39。
ActiveXComponent dotnetCom = null; 40。 dotnetCom = new ActiveXComponent("TestCom。Encrypt"); 41。 Variant var = Dispatch。
call(dotnetCom,"GetEncrypt","哥是第一个参数","哥是第二个参数"); 42。 String str = var。toString(); //返回值 43。 } catch (Exception ex) { 44。
ex。printStackTrace(); 45。 } 46。 } 47。 48。} 这便完成了Java调用。net DLL的方法!
完成 丢弃。
全部