Using C++ extansion from Lua

Use all the fxcodebase indicator in your application!

Moderator: admin

Using C++ extansion from Lua

Postby wmaster » Tue Apr 26, 2011 3:39 am

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;
}
wmaster
 
Posts: 7
Joined: Mon Feb 07, 2011 11:58 am

Re: Using C++ extansion from Lua

Postby Nikolay.Gekht » Tue Apr 26, 2011 9:53 am

Thank you for the example.

Notes:
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: http://fxcodebase.com/wiki/index.php/Lu ... 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.
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Using C extansion from Lua

Postby soosteVek » Wed Sep 28, 2011 2:07 am

Can i use SQL Server-based cache invalidation mechanism without using SqlDataSource Control. I am using DataAdapter and SqlCommandBuilder.

Thank you.
soosteVek
 
Posts: 2
Joined: Fri Jun 03, 2011 1:21 pm
Location: Norway

Re: Using C++ extansion from Lua

Postby Nikolay.Gekht » Wed Sep 28, 2011 9:01 am

The only way I know do it this way is to use SQL server direct connection API.
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC


Return to C++/.NET API

Who is online

Users browsing this forum: No registered users and 2 guests