![]() |
Common Language Extension(CLE) |
|||
Traditional method for java calling c++ usually uses jni interface. This is little more difficult because programmer have to deal with calling conversion, structed memory conversion, string format conversion, record callback java handle, etc. Using cle developed by srplab, the procedure is simplified. Here is a simple example. A function “Add” is defined in c++ which returns the sum of two input integers, the function also call java method “Show” to print the result. Using cle to developed java extension, c++ externsion should be compiled into a sharelib which exports two functions StarCoreService_Init and StarCoreService_Term. There are four steps: 1. Create service. 2. Create interface object, and defines a new function “Add” of it. 3. Write function code of Add. 4. Set function address of Add to object’ function definition.
#include "vsopenapi.h"
static class ClassOfSRPInterface *SRPInterface;
//--Step 3 : Add function body
static VS_INT32 Add(void *Object,VS_INT32 x,VS_INT32 y)
{
//--Callback script function
SRPInterface -> ScriptCall(Object,NULL,"Show","(i)",x+y);
return
x + y;
}
VS_BOOL StarCoreService_Init(class ClassOfStarCore
*starcore)
{
void
*AtomicClass,*Add_AtomicFunction;
class
ClassOfBasicSRPInterface *BasicSRPInterface;
//--Step
1 : Create service
BasicSRPInterface
= starcore ->GetBasicInterface();
BasicSRPInterface
->CreateService("","AddFunctionService",NULL,"123",0,0,0,0,0,0);
//--Step
2 : Create object and defines a new
function “Add”
SRPInterface
= BasicSRPInterface
->GetSRPInterface("AddFunctionService","root","123");
AtomicClass
= SRPInterface
->CreateAtomicObjectSimple("TestItem","TestClass",NULL,NULL,NULL);
Add_AtomicFunction
= SRPInterface ->CreateAtomicFunctionSimple(AtomicClass,"Add","VS_INT32
Add(VS_INT32 x,VS_INT32 y);",NULL,NULL,VS_FALSE,VS_FALSE);
//--Step
4 : Set function address to Add
function
SRPInterface
-> SetAtomicFunction(Add_AtomicFunction,(void *)Add);
return
VS_TRUE;
}
void StarCoreService_Term(class ClassOfStarCore
*starcore)
{
SRPInterface
-> Release();
return;
}
Compile into sharelibrary, may be named “AddFunctionService.dll”.
For java code calls the above sharelib, there are four steps; 1. Init cle, create service group,import sharelibrary, and create service. 2. Define extends class of StarObjectClass, which implement callback function “Show”. 3. Create Instance of object “TestClass” defined in sharelibrary. 4. call function “Add”.
import com.srplab.www.starcore.*;
//--Step2 : Define extends class of
StarObjectClass and implements callback function “Show”.
class MyObjectClass extends StarObjectClass{
public void Show( StarObjectClass self,int para )
{
System.out.println("print from java call back "+para);
return;
}
public
MyObjectClass(StarObjectClass srcobj){
super(srcobj);
}
}
public class java_call{
public
static void main(String[] args){
//--Step1 : Init cle,
create service group,import sharelibrary, and create service
StarCoreFactory
starcore= StarCoreFactory.GetFactory();
StarServiceClass
Service=starcore._InitSimple("test","123",0,0,"AddFunctionService.dll");
//--Step3
: Create Instance of object “TestClass” defined in sharelibrary.
MyObjectClass
a = new MyObjectClass( Service._GetObject("TestClass")._New() );
//--Step4
: call function “Add”.
System.out.println("Add returns :
"+a._Call("Add",12,34));
starcore._ModuleExit();
}
}
|
| examples code download |
| Copywright (C) 2005 www.SRPLab.com(Star-River Platform Lab.) All Rights Reserved | Email:support@srplab.com |