Page 1 of 1

Using C++ extansion from Lua

PostPosted: Tue Apr 26, 2011 3:39 am
by wmaster
Example of using external extension written in C++, for indicators and strategies, embedded in Lua..
This example is showing call and execution of external application mentioned for controlled loop sound alerts. It was user request, with full usage specification, which can be found here: viewtopic.php?f=28&t=3891

Extension is DLL module (source code below) and there is function playSound which calls external application called SoundPlayApp.exe, yet mentioned application needs extra parameters in console mode. First parameter needed is path to sound file, and the second is number to be looped. I.e. : SoundPlayApp.exe "c:\mysound.wav" 5

There are many other possibilities for executing external calculation, this is one simple example how to extend asynchronized sound alerts, because original functions within C library are hard to control using loop mode...

Code: Select all
// ExternSoundPlay.cpp : Defines the exported functions for the DLL application.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <ShellAPI.h>
#include <tchar.h>

extern "C"
   #include "lua.h"
   #include "lualib.h"
   #include "lauxlib.h"

using namespace std;

bool getNumber(lua_State *L, int index, int& value)
    if (lua_isnumber(L, index) == 0)
        return false;
   value = lua_tointeger(L, index);
    return true;

bool getString(lua_State *L, int index, string& value)
   if (lua_isstring(L, index) == 0)
        return false;
   value = lua_tostring(L, index);
    return true;

std::wstring s2ws(const std::string& s)
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;

int playSound(lua_State *L)
   string BuySellFlag;

   if (!getString(L, 1, BuySellFlag))
        return luaL_error(L, "Incorrect BuySell param");

   std::wstring stemp = s2ws(BuySellFlag);

   ShellExecute(NULL, _T("open"), _T("SoundPlayApp.exe"), stemp.c_str(), NULL, SW_SHOWNORMAL );

   lua_pushnumber(L, 0);

   return 0;

static const luaL_reg lib_functions[] =
    {"playSound", playSound},
    {NULL, NULL}

extern "C" __declspec(dllexport)
int luaopen_ExternSoundPlay(lua_State *L)
    luaL_register(L, "ExternSoundPlay", lib_functions);
    return 1;

Re: Using C++ extansion from Lua

PostPosted: Tue Apr 26, 2011 9:53 am
by Nikolay.Gekht
Thank you for the example.

1) If you want to use Marketscope's data such as price sources in your Lua extensions you can use Indicore Integration SDK:
Read more: ... on_Modules

2) Do NOT use SEH (C++ structure exception handling) direction in the function which calls Lua or is called from Lua. The Lua JIT 2.0 we are using in SDK 2.0 is not compatible with Microsoft's SEH.

Using C extansion from Lua

PostPosted: Wed Sep 28, 2011 2:07 am
by soosteVek
Can i use SQL Server-based cache invalidation mechanism without using SqlDataSource Control. I am using DataAdapter and SqlCommandBuilder.

Thank you.

Re: Using C++ extansion from Lua

PostPosted: Wed Sep 28, 2011 9:01 am
by Nikolay.Gekht
The only way I know do it this way is to use SQL server direct connection API.