Java call. net DLL instance code Introduction

Source: Internet
Author: User

The project must use its provided encryption mechanism and throw it to two. net-written DLL files. I searched the internet and couldn't find anything. It was impossible to even hear people say this. A thought was taken into consideration. Use C # As a Com, call the DLL provided by the customer to implement encryption and decryption, and then provide it to java for use. After some time, it was finally confirmed feasible.

Environment and tools:
1.. net framework 3.5 C #
2. java jdk1.5, Tomcat 5.5
3. 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
1. Introduce jacob. jar in the Development Environment
2. Copy the jacob-1.15-M3-x86.dll file to the C: WindowsSystem32 directory. If it is a Web application, copy it to the jdk1.5.0 _ 16in directory (bin directory under the jdk installation 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 ();
}
}

}

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.