Preface
With the help of CLE, it is easy to use C # dynamic libraries in Java, which is illustrated by a simple example. Using CLE, you need to install the http://www.srplab.com/Data/starcore_win32.2.0.3.exe beforehand. Here is a section of C # code, very simple:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using Star_csharp;
Namespace Testcs
{
public class Class1
{
Public Class1 (Stringmsg,floatnum)
{
Console.WriteLine ("Demo ...");
Console.WriteLine (num);
}
Public Stringgetmessage (STRINGMSG)
{
Return "Hello" +msg;
}
}
}
The code contains a constructor, and a getmessage function, which is entered as a string and returns 1 strings.
Compile the above code into a dynamic library, using the following command.
Csc/reference:c:\srplab\libs\star_csharp.dll/target:library/platform:x86 Testcs.cs
Java calls a C # generated dynamic library, which first requires initialization of the CLE
Use the following code: import com.srplab.www.starcore.*;
public class test{
public static void Main (string[] args) {
Starcorefactory starcore=starcorefactory.getfactory ();
Starserviceclass service=starcore._initsimple ("Test", "123", 0,0);
Starsrvgroupclass Srvgroup = (starsrvgroupclass) service._get ("_servicegroup");
Srvgroup._clearservice ();
Starcore._moduleexit ();
}
Call C #
1. To initialize the C # interface with _initraw
Srvgroup._initraw ("CSharp", Service);
Input parameters: The first string is the name of the interface, which can be "CSharp", "Csharp4", "Csharp45", respectively. Net3.5,. Net4.0 and. Net4.5. The second parameter is the service object that was created when the initialization was initiated.
2. Loading C # Dynamic libraries with _loadrawmodule
Srvgroup._loadrawmodule ("CSharp", "Testcs", "Testcs.dll", false);
Input parameters: The first is the name of the interface, the second is the name of the Assembly, the third is the file name of the dynamic library, the fourth parameter is bool type, and false can be entered.
3. Using _importrawcontext to import classes in a C # dynamic library
Starobjectclass Class1 = Service._importrawcontext ("CSharp", "Testcs.") Class1 ", True," ");
Input parameters, the first is the interface name, the second is the name of the complete class, and the third parameter is set to True to indicate that 1 classes were imported. The fourth argument is reserved, with the fixed setting to "".
4. To assign an instance of 1 classes using the _new function
Starobjectclass Inst = class1._new ("", "", "CLE value", 44);
Input parameters: The first two names of the new objects, and the LUA string initialization, set to empty. The arguments for the constructor are followed by.
5. To invoke a member function of a class using the _call function
String result = (string) inst._call ("GetMessage", "Java");
Input parameters: The first is the name of the function to invoke, followed by the parameter of the function. The return value needs to be converted to the corresponding numeric value depending on the return type of the function.
The complete code is as follows:
Import com.srplab.www.starcore.*; public class test{
public static void Main (string[] args) {
Starcorefactory starcore=starcorefactory.getfactory ();
Starserviceclass service=starcore._initsimple ("Test", "123", 0,0);
Starsrvgroupclass Srvgroup = (starsrvgroupclass) service._get ("_servicegroup");
--init CSharp RAW Interface---* *
Srvgroup._initraw ("CSharp", Service);
--load CSharp Module---* *
Srvgroup._loadrawmodule ("CSharp", "Testcs", "Testcs.dll", false);
--attach object to Testcs. CLASS1---* *
Starobjectclass Class1 = Service._importrawcontext ("CSharp", "Testcs.") Class1 ", True," ");
Starobjectclass Inst = class1._new ("", "", "CLE value", 44);
String result = (string) inst._call ("GetMessage", "Java");
SYSTEM.OUT.PRINTLN (result);
Srvgroup._clearservice ();
Starcore._moduleexit ();
}
}
After the Java code is compiled with Javac, it runs
Java test
You can see the correct output.
If you need to run on a machine that does not have CLE installed, you will need to copy "Libstarcore.dll" "Star_java.dll" Star_csharp.dll "Starcore.jar" to the current directory. and specify Starcore.jar at run time
Java-classpath./; Starcore. Jar Test