Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C # as an intermediate language to realize Java calling. Net DLL

Shulou Source: shulou.com Published: 2022-06-03 01:04:16 09月20日 Update

How to use C # as an intermediate language to realize Java calling. Net DLL. In view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

51CTO once talked about "Java and. Net WebServices call each other", but how to implement the method of Java calling. Net DLL? We searched the Internet and didn't find anything. We even saw people threatening that it was impossible. This article will introduce in detail a method for java to call. Net DLL to meet the special requirements of special customers: "the DLL encryption mechanism written in. Net must be used in Java projects!"

Environment and tools:

◆ .net framework 3.5 C#

◆ java jdk1.5, Tomcat 5.5

◆ jacob-1.15-M3

Implementation example:

1. C # to make Com components

Create a new Class project named TestCom

Code

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 "Test |" + str+ "|" + str2;} "

Open the Project-- > Properties menu and open Assembly Information in the Application tab to select Make assembly Com-Visible. Then switch to the Build tab and select Register for COM interop.

Guid generation: open the Visual Studio Command Prompt input guidgen command to call up the tool. Select Registry Format, click New Guid, and then COPY out.

[DispId (1)] is the identification of the function. If there are multiple functions, you can add [DispId (2)], [DispId (3)] in front of the function accordingly.

TestCom.dll and TestCom.tlb are generated in the compiler Debug directory

Manually register the Com method:

Open Visual Studio Command Prompt to enter the Debug directory and run the command to register: regasm TestCom.DLL / tlb:TestCom.tlb

2. Java calls Com

Deploy jacob

◆ introduces jacob.jar into the development environment

◆ copies the jacob-1.15-M3-x86.dll file to the C:\ Windows\ System32 directory and, if it is a Web application, to the jdk1.5.0_16\ bin directory (bin directory under the jdk installation directory)

Java calling code

Code

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", "GE is * parameter", "GE is the second parameter"); String str = var.toString (); / / return value} catch (Exception ex) {ex.printStackTrace ();}

This completes the method that Java calls. Net DLL!

This is the answer to the question about how to use C # as an intermediate language to implement Java calling. Net DLL. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Tags: Directories methods codes functions questions languages special parameters commands tools copies more tags environments projects help generation solutions easy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Xiaomi Shulou Tech Info Docker MySQL