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!