C # making COM components: How Java calls. NET DLLs

Source: Internet
Author: User
Tags copy net return string visual studio

This article will give you a detailed introduction to Java calling. NET DLLs to achieve special customer requirements: "You must use the. NET-written DLL encryption mechanism that you have provided in your Java project!" ”

Environment and tools:

.net framework 3.5 C #

Java jdk1.5, Tomcat 5.5

Jacob-1.15-m3

Implementation example:

C # Production of COM components

Create a new class item, name 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 in the Application tab and open Assembly information to select Make Assembly com-visible. Switch to the Build tab to select the Register for COM Interop item.

Build of GUIDs: Opens visual Studio command Prompt Input guidgen command to pull up tools. Type Select registry Format, click the new Guid, and then copy it.

[DispId (1)] is the identity of the function. If you have more than one function, add [DispId (2)] to the front of the function, [DispId (3)] ...

TestCom.dll and testcom.tlb are generated in the debug directory of the compiler

To register the COM method manually:

Open Visual Studio command prompt into the debug directory and run the command registration: RegAsm Testcom.dll/tlb:testcom.tlb

Second, Java invoke COM

Deploy Jacob

Introducing Jacob.jar in the development environment

Copy the Jacob-1.15-m3-x86.dll file to the C:\Windows\System32 directory and, if it is a Web application, copy it to the Jdk1.5.0_16\bin directory (the 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", "elder brother is the first parameter", "elder brother is the second parameter");
String str = var.tostring (); return value
catch (Exception ex)
{
Ex.printstacktrace ();
}
}
}

This completes the Java call to the. NET DLL Method!



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.