日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Java Tomcat 中調(diào)用.net DLL的方法

系統(tǒng) 2078 0

  近日一個(gè)java的項(xiàng)目,客戶(hù)要求項(xiàng)目中必須使用其提供的加密機(jī)制,扔給了兩個(gè)。net寫(xiě)的DLL.網(wǎng)絡(luò)上搜了一圈也沒(méi)找到啥東西,甚至看到人揚(yáng)言此事絕無(wú)可能。郁悶當(dāng)中考慮了一個(gè)思路。用C#做一個(gè)Com,調(diào)用客戶(hù)提供的DLL實(shí)現(xiàn)加密解密的方法,然后提供給java使用。經(jīng)過(guò)一番搗騰,最后證實(shí)可行。
?
?  環(huán)境與工具:
?
?  1、。net framework 3.5 C#
?
?  2、java jdk1.5, Tomcat 5.5
?
?  3、jacob-1.15-M3
?
?  實(shí)現(xiàn)例子:
?
?  一、C# 制作Com組件
?
?  新建一個(gè)Class 項(xiàng)目,取名TestCom
?
?  Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->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 "測(cè)試 | "+str+"|"+str2;
?
?  }
?
?  }
?
?  }
?
?  打開(kāi) Project--> Properties菜單 在Application標(biāo)簽中打開(kāi) Assembly Information 將Make assembly Com-Visible一項(xiàng)選中。再切換到Build標(biāo)簽將 Register for COM interop一項(xiàng)選中。
?
?  Guid的生成:打開(kāi)Visual Studio Command Prompt 輸入guidgen 命令調(diào)出工具。類(lèi)型選擇Registry Format,點(diǎn)擊New Guid,然后COPY出來(lái)。
?
?  [DispId(1)]為函數(shù)的標(biāo)識(shí)。如果有多個(gè)函數(shù)可相應(yīng)的在函數(shù)前面加[DispId(2)], [DispId(3)]…
?
?  設(shè)置強(qiáng)名稱(chēng):打開(kāi)Visual Studio Command Prompt 輸入:sn -k TestComkey.snk 生成TestComkey.snk 文件
?
?  打開(kāi) Project--> Properties菜單 在Signing標(biāo)簽中選中 Sign the assembly,選擇剛剛生成的強(qiáng)名稱(chēng)文件TestComkey.snk
?
?  編譯程序Debug目錄中會(huì)生成 TestCom.dll 和TestCom.tlb www.yzyedu.com
?
?  手工注冊(cè)Com方法:
?
?  打開(kāi)Visual Studio Command Prompt進(jìn)入Debug目錄,運(yùn)行命令注冊(cè):
?
?  regasm TestCom.DLL /tlb:TestCom.tlb www.jx-jf.com
?
?  gacutil -i TestCom.DLL (執(zhí)行這個(gè)命令需要TestCom.DLL 具有強(qiáng)名稱(chēng))
?
?  二、java 調(diào)用 Com
?
?  部署jacob
?
?  1、在開(kāi)發(fā)環(huán)境中引入jacob.jar
?
?  2、拷貝jacob-1.15-M3-x86.dll 文件到 C:\Windows\System32目錄,如果是Web應(yīng)用的話(huà)還需要拷貝到j(luò)dk1.5.0_16\bin目錄(jdk安裝目錄下的bin目錄)
?
?  java調(diào)用代碼
?
?  Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->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","哥是第一個(gè)參數(shù)","哥是第二個(gè)參數(shù)");
?
?  String str = var.toString(); //返回值
?
?  } catch (Exception ex) {
?
?  ex.printStackTrace();
?
?  }
?
?  }
?
?  }
?
?

Java Tomcat 中調(diào)用.net DLL的方法


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 太湖县| 青冈县| 湟源县| 安福县| 建始县| 东乌珠穆沁旗| 和政县| 华坪县| 潼南县| 南和县| 新邵县| 宝鸡市| 乐安县| 库车县| 安图县| 云林县| 武汉市| 普定县| 连平县| 聂拉木县| 繁峙县| 定结县| 大姚县| 万源市| 潍坊市| 余干县| 荣成市| 北京市| 布尔津县| 壶关县| 寻乌县| 天长市| 马边| 石门县| 北宁市| 尉氏县| 清徐县| 仙游县| 普安县| 沂水县| 济阳县|