![]() |
Common Language Extension(CLE) |
|||
Here presents a new method for c# calling c++. A function “Add” is defined in c++ which returns the sum of two input integers, the function also call c# method “Show” to print the result. Using cle to developed c# 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 c# 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”.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using star_csharp;
namespace csharp_call
{
class MyObjectClass : StarObjectClass{
//--Step2
: Define extends class of StarObjectClass and implements callback function “Show”.
public void
Show( StarObjectClass self,int para )
{
Console.WriteLine("print from java call back "+para);
return;
}
public MyObjectClass(StarObjectClass
srcobj):base(srcobj){
}
}
class Program
{
static
void
{
//--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”.
Console.WriteLine("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 |