Using C++ extansion from Lua
Posted: 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...
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;
}