C # create COM component: Java call. Net DLL Method

Source: Internet
Author: User

Http://web.chinaitlab.com/web/812577.html

This article will introduce a Java call in detail. net DLL method to meet special requirements of customers: "in Java projects, you must use the provided. net write DLL encryption mechanism!"

Environment and tools:

◆. NET Framework 3.5 C #

◆ Java jdk1.5, Tomcat 5.5

◆ Jacob-1.15-M3

Implementation example:

I. C # create 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, open assembly information in the application tag, and select make assembly com-visible. Switch to the build tab and select register for com InterOP.

Guid generation: Open Visual Studio command prompt and enter the guidgen command to call up the tool. Select registry format as the type, click New guid, and copy it out.

[Dispid (1)] is the identifier of the function. If there are multiple functions, add [dispid (2)], [dispid (3)]…

Testcom. dll and testcom. TLB are generated in the DEBUG directory of the Compilation Program.

Manually register COM:

Open Visual Studio command prompt to enter the DEBUG directory, run the command registration: regasm testcom. dll/TLB: testcom. TLB

Ii. Java calls com

Deploy Jacob

◆ Introduce Jacob. jar in the Development Environment

◆ Copy the jacob-1.15-M3-x86.dll file to the c: \ windows \ system32 directory, if it is a Web application, you also need to copy to the jdk1.5.0 _ 16 \ bin directory (JDK installation directory under the bin directory)

Java call code

Code

Import com. Jacob. ActiveX. activexcomponent;

Import com.jacb.com. comthread;

Import com.jacb.com. Dispatch;

Import com.jacb.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", "Brother is the first parameter", "Brother is the second parameter ");

String STR = var. tostring (); // Return Value

} Catch (exception ex)

{

Ex. printstacktrace ();

}

}

}

This completes the Java method to call. Net DLL!

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.