by Victor.Tereschenko » Tue Jan 04, 2011 6:44 am
2) Use direct context switching (you'll need to develop a small interoperation library in C++).
I have such an example:
Lua indicator code
- Code: Select all
-- Indicator profile initialization routine
-- Defines indicator profile properties and indicator parameters
function Init()
indicator:name("Lua-C# sample");
indicator:description("Illustrates use of C# dll from Lua");
indicator:requiredSource(core.Bar);
indicator:type(core.Indicator);
indicator.parameters:addColor("clr", "Color", "", core.rgb(255, 0, 0));
indicator.parameters:addInteger("width", "Width", "", 1, 1, 5);
indicator.parameters:addInteger("style", "Style", "", core.LINE_SOLID);
indicator.parameters:setFlag("style", core.FLAG_LINE_STYLE);
end
-- Indicator instance initialization routine
-- Processes indicator parameters and creates output streams
local source = nil;
local first = 0;
local Color = nil;
local Width = nil;
local Style = nil;
local host = nil;
-- Routine
function Prepare()
host = core.host;
source = instance.source;
first = source:first();
require("LuaDll");
local name = profile:id();
instance:name(name);
out = instance:addStream("Out", core.Line, name, "Out", instance.parameters.clr, first)
out:setWidth(instance.parameters.width);
out:setStyle(instance.parameters.style);
end
local errorLoad = false;
-- Indicator calculation routine
function Update(period)
if (period >= first) then
core.host:trace(source.open[period]);
out[period] = LuaDll.CalcValue(source.open[period], source.high[period], source.low[period], source.close[period]);
end
end
C++/CLI
- Code: Select all
// LuaDll.h
#pragma once
using namespace Calculation; // in C# dll
extern "C"
{
#include "..\Lua\src\lua.h"
#include "..\Lua\src\lualib.h"
#include "..\Lua\src\lauxlib.h"
}
bool getNumber(lua_State *L, int index, double& value)
{
if (lua_isnumber(L, index) == 0)
return false;
value = lua_tonumber(L, index);
return true;
}
int calcValue(lua_State *L)
{
// get passed parameters
double open, high, low, close;
if (!getNumber(L, 1, open))
return luaL_error(L, "Incorrect open value");
if (!getNumber(L, 2, high))
return luaL_error(L, "Incorrect high value");
if (!getNumber(L, 3, low))
return luaL_error(L, "Incorrect low value");
if (!getNumber(L, 4, close))
return luaL_error(L, "Incorrect close value");
// call C# method
double result = Calculator::Calculate(open, high, low, close);
// push result into the Lua stack
lua_pushnumber(L, result);
int numberOfValuesReturned = 1;
return numberOfValuesReturned;
}
static const luaL_reg lib_functions[] =
{
{"CalcValue", calcValue},
{NULL, NULL}
};
extern "C" __declspec(dllexport)
int luaopen_LuaDll(lua_State *L)
{
luaL_register(L, "LuaDll", lib_functions);
return 1;
}
It's impossible to attach a file in this part of the forum. Let me know if you need all sample project files, I'll upload them somewhere.
“There are only three sports: bullfighting, motor racing, and mountaineering; all the rest are merely games.” (c) Ernest Hemingway