C # create Com component: java call. net DLL Method

Source: Internet
Author: User

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: WindowsSystem32 directory, if it is a Web application, you also need to copy to the jdk1.5.0 _ 16in 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!

 

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.