Files

267 lines
9.2 KiB
C++
Raw Permalink Normal View History

2014-10-31 09:05:52 +02:00
// This is the main DLL file.
#include "stdafx.h"
#include "MT4Handler.h"
#include "Windows.h"
2016-12-12 17:29:52 +02:00
#include <vcclr.h>
#include <functional>
2014-10-31 09:05:52 +02:00
using namespace System;
using namespace MTApiService;
using namespace System::Runtime::InteropServices;
using namespace System::Reflection;
using namespace System::Security::Cryptography;
using namespace System::Security;
using namespace System::Text;
using namespace System::IO;
using namespace System::Collections::Generic;
using namespace System::Diagnostics;
2016-12-12 17:29:52 +02:00
#define _DLLAPI extern "C" __declspec(dllexport)
void convertSystemString(wchar_t* dest, String^ src)
2014-10-31 09:05:52 +02:00
{
pin_ptr<const wchar_t> wch = PtrToStringChars(src);
memcpy(dest, wch, wcslen(wch) * sizeof(wchar_t));
dest[wcslen(wch)] = '\0';
2014-10-31 09:05:52 +02:00
}
2016-12-12 17:29:52 +02:00
template <typename T> T Execute(std::function<T()> func, wchar_t* err, T default_value)
2014-10-31 09:05:52 +02:00
{
2016-12-12 17:29:52 +02:00
T result = default_value;
try
{
2016-12-12 17:29:52 +02:00
result = func();
}
catch (Exception^ e)
{
convertSystemString(err, e->Message);
}
2016-12-12 17:29:52 +02:00
return result;
2014-10-31 09:05:52 +02:00
}
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall initExpert(int expertHandle, int port, wchar_t* symbol, double bid, double ask, wchar_t* err)
2014-10-31 09:05:52 +02:00
{
2016-12-12 17:29:52 +02:00
return Execute<bool>([&expertHandle, &port, symbol, &bid, &ask]() {
MT4Handler^ mtHandler = gcnew MT4Handler();
MtAdapter::GetInstance()->InitExpert(expertHandle, port, gcnew String(symbol), bid, ask, mtHandler);
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
}
_DLLAPI bool _stdcall deinitExpert(int expertHandle, wchar_t* err)
{
return Execute<bool>([&expertHandle]() {
MtAdapter::GetInstance()->DeinitExpert(expertHandle);
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
2014-10-31 09:05:52 +02:00
}
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall updateQuote(int expertHandle, wchar_t* symbol, double bid, double ask, wchar_t* err)
2014-10-31 09:05:52 +02:00
{
2016-12-12 17:29:52 +02:00
return Execute<bool>([&expertHandle, symbol, &bid, &ask]() {
MtAdapter::GetInstance()->SendQuote(expertHandle, gcnew String(symbol), bid, ask);
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
2014-10-31 09:05:52 +02:00
}
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall sendEvent(int expertHandle, int eventType, wchar_t* payload, wchar_t* err)
{
2016-12-12 17:29:52 +02:00
return Execute<bool>([&expertHandle, &eventType, payload]() {
MtAdapter::GetInstance()->SendEvent(expertHandle, eventType, gcnew String(payload));
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
}
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall sendIntResponse(int expertHandle, int response, wchar_t* err)
2014-10-31 09:05:52 +02:00
{
2016-12-12 17:29:52 +02:00
return Execute<bool>([&expertHandle, &response]() {
MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseInt(response));
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
2014-10-31 09:05:52 +02:00
}
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall sendBooleanResponse(int expertHandle, bool response, wchar_t* err)
2014-10-31 09:05:52 +02:00
{
2016-12-12 17:29:52 +02:00
return Execute<bool>([&expertHandle, &response]() {
MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseBool(response));
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
2014-10-31 09:05:52 +02:00
}
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall sendDoubleResponse(int expertHandle, double response, wchar_t* err)
2014-10-31 09:05:52 +02:00
{
2016-12-12 17:29:52 +02:00
return Execute<bool>([&expertHandle, &response]() {
MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseDouble(response));
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
2014-10-31 09:05:52 +02:00
}
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall sendStringResponse(int expertHandle, wchar_t* response, wchar_t* err)
2014-10-31 09:05:52 +02:00
{
2016-12-12 17:29:52 +02:00
return Execute<bool>([&expertHandle, response]() {
MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseString(gcnew String(response)));
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
2014-10-31 09:05:52 +02:00
}
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall sendErrorResponse(int expertHandle, int code, wchar_t* message, wchar_t* err)
2014-10-31 09:05:52 +02:00
{
2016-12-12 17:29:52 +02:00
return Execute<bool>([&expertHandle, &code, message]() {
MtResponseString^ res = gcnew MtResponseString(gcnew String(message));
res->ErrorCode = code;
MtAdapter::GetInstance()->SendResponse(expertHandle, res);
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
2014-10-31 09:05:52 +02:00
}
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall sendVoidResponse(int expertHandle, wchar_t* err)
2014-10-31 09:05:52 +02:00
{
2016-12-12 17:29:52 +02:00
return Execute<bool>([&expertHandle]() {
MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseObject(nullptr));
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
}
2014-10-31 09:05:52 +02:00
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall sendDoubleArrayResponse(int expertHandle, double* values, int size, wchar_t* err)
{
return Execute<bool>([&expertHandle, values, &size]() {
array<double>^ list = gcnew array<double>(size);
for (int i = 0; i < size; i++)
{
list[i] = values[i];
}
MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseDoubleArray(list));
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
2014-10-31 09:05:52 +02:00
}
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall sendIntArrayResponse(int expertHandle, int* values, int size, wchar_t* err)
2014-10-31 09:05:52 +02:00
{
2016-12-12 17:29:52 +02:00
return Execute<bool>([&expertHandle, values, &size]() {
array<int>^ list = gcnew array<int>(size);
2016-12-12 17:29:52 +02:00
for (int i = 0; i < size; i++)
{
list[i] = values[i];
}
MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseIntArray(list));
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
}
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall sendLongResponse(int expertHandle, __int64 response, wchar_t* err)
{
2016-12-12 17:29:52 +02:00
return Execute<bool>([&expertHandle, &response]() {
MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseLong(response));
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
2014-10-31 09:05:52 +02:00
}
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall getCommandType(int expertHandle, int* res, wchar_t* err)
2014-10-31 09:05:52 +02:00
{
2016-12-12 17:29:52 +02:00
return Execute<bool>([&expertHandle, res]() {
*res = MtAdapter::GetInstance()->GetCommandType(expertHandle);
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
2014-10-31 09:05:52 +02:00
}
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall getIntValue(int expertHandle, int paramIndex, int* res, wchar_t* err)
2014-10-31 09:05:52 +02:00
{
2016-12-12 17:29:52 +02:00
return Execute<bool>([&expertHandle, &paramIndex, res]() {
*res = (int)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex);
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
2014-10-31 09:05:52 +02:00
}
// --- index parameters
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall getDoubleValue(int expertHandle, int paramIndex, double* res, wchar_t* err)
2014-10-31 09:05:52 +02:00
{
2016-12-12 17:29:52 +02:00
return Execute<bool>([&expertHandle, &paramIndex, res]() {
*res = (double)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex);
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
2014-10-31 09:05:52 +02:00
}
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall getStringValue(int expertHandle, int paramIndex, wchar_t* res, wchar_t* err)
2014-10-31 09:05:52 +02:00
{
2016-12-12 17:29:52 +02:00
return Execute<bool>([&expertHandle, &paramIndex, res]() {
convertSystemString(res, (String^)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex));
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
2016-09-29 16:14:53 +03:00
}
2016-12-12 17:29:52 +02:00
_DLLAPI bool _stdcall getBooleanValue(int expertHandle, int paramIndex, int* res, wchar_t* err)
2016-09-29 16:14:53 +03:00
{
2016-12-12 17:29:52 +02:00
return Execute<bool>([&expertHandle, &paramIndex, res]() {
*res = (bool)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex);
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
}
_DLLAPI bool _stdcall getLongValue(int expertHandle, int paramIndex, __int64* res, wchar_t* err)
{
return Execute<bool>([&expertHandle, &paramIndex, res]() {
*res = (__int64)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex);
return true;
}, err, false);
}
// --- named parameters
_DLLAPI bool _stdcall containsNamedValue(int expertHandle, wchar_t* paramName)
{
wchar_t err[1000];
return Execute<bool>([&expertHandle, paramName]() {
return MtAdapter::GetInstance()->ContainsNamedParameter(expertHandle, gcnew String(paramName));
}, err, false);
}
_DLLAPI bool _stdcall getNamedIntValue(int expertHandle, wchar_t* paramName, int* res, wchar_t* err)
{
return Execute<bool>([&expertHandle, paramName, res]() {
System::Object^ obj = MtAdapter::GetInstance()->GetNamedParameter(expertHandle, gcnew String(paramName));
*res = (int)obj;
return true;
}, err, false);
}
_DLLAPI bool _stdcall getNamedDoubleValue(int expertHandle, wchar_t* paramName, double* res, wchar_t* err)
{
return Execute<bool>([&expertHandle, paramName, res]() {
System::Object^ obj = MtAdapter::GetInstance()->GetNamedParameter(expertHandle, gcnew String(paramName));
*res = (double)obj;
return true;
}, err, false);
}
_DLLAPI bool _stdcall getNamedStringValue(int expertHandle, wchar_t* paramName, wchar_t* res, wchar_t* err)
{
return Execute<bool>([&expertHandle, paramName, res]() {
System::Object^ obj = MtAdapter::GetInstance()->GetNamedParameter(expertHandle, gcnew String(paramName));
convertSystemString(res, (String^)obj);
return true;
}, err, false);
}
_DLLAPI bool _stdcall getNamedBooleanValue(int expertHandle, wchar_t* paramName, int* res, wchar_t* err)
{
return Execute<bool>([&expertHandle, paramName, res]() {
System::Object^ obj = MtAdapter::GetInstance()->GetNamedParameter(expertHandle, gcnew String(paramName));
*res = (bool)obj;
return true;
}, err, false);
}
_DLLAPI bool _stdcall getNamedLongValue(int expertHandle, wchar_t* paramName, __int64* res, wchar_t* err)
{
return Execute<bool>([&expertHandle, paramName, res]() {
System::Object^ obj = MtAdapter::GetInstance()->GetNamedParameter(expertHandle, gcnew String(paramName));
if (obj != nullptr)
*res = (__int64)obj;
2016-12-12 17:29:52 +02:00
return true;
}, err, false);
2014-10-31 09:05:52 +02:00
}