Skip to content

Instantly share code, notes, and snippets.

@rf0444
Last active October 29, 2016 08:14
Show Gist options
  • Save rf0444/bfc82243665b81263cee18aa9a796966 to your computer and use it in GitHub Desktop.
Save rf0444/bfc82243665b81263cee18aa9a796966 to your computer and use it in GitHub Desktop.
MT4 Child Process
// property省略
#import "process.dll"
void OpenChildProcess(string);
void SendToChildProcess(string);
#import
int OnInit() {
ChartSetInteger(ChartID(), CHART_EVENT_OBJECT_CREATE, true);
ChartSetInteger(ChartID(), CHART_EVENT_OBJECT_DELETE, true);
OpenChildProcess("outer.exe");
SendToChildProcess(StringFormat("{\"type\":\"init\",\"symbol\":\"%s\",\"price\":%s,\"lines\":%s}\n", Symbol(), price(), lines()));
return INIT_SUCCEEDED;
}
void OnDeinit(const int reason) {
SendToChildProcess(StringConcatenate("{\"type\":\"close\"}\n"));
}
void OnTick() {
SendToChildProcess(StringFormat("{\"type\":\"tick\",\"price\":%s}\n", price()));
}
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) {
if (chart_object_changed(id)) {
SendToChildProcess(StringFormat("{\"type\":\"line\",\"lines\":%s}\n", lines()));
}
}
string price() {
return StringFormat("{\"ask\":%s,\"bid\":%s}", DoubleToString(Ask, Digits), DoubleToString(Bid, Digits));
}
bool chart_object_changed(const int id) {
return id == CHARTEVENT_OBJECT_CREATE
|| id == CHARTEVENT_OBJECT_DELETE
|| id == CHARTEVENT_OBJECT_CHANGE
|| id == CHARTEVENT_OBJECT_DRAG;
}
string lines() {
string ls = "";
for (int i = 0; i < ObjectsTotal(ChartID()); i++) {
string name = ObjectName(i);
if (ObjectType(name) != OBJ_HLINE) {
continue;
}
double price = ObjectGetDouble(ChartID(), name, OBJPROP_PRICE1);
long clr = ObjectGetInteger(ChartID(), name, OBJPROP_COLOR);
string line = StringFormat("{\"name\":\"%s\",\"price\":%s,\"color\":%d},", name, DoubleToString(price, Digits), clr);
StringAdd(ls, line);
}
if (ls != "") {
ls = StringSubstr(ls, 0, StringLen(ls) - 1);
}
return StringConcatenate("[", ls, "]");
}
#include <windows.h>
PROCESS_INFORMATION pi = {};
HANDLE inputPipe = NULL;
extern "C" __declspec(dllexport) void OpenChildProcess(LPTSTR path) {
SECURITY_ATTRIBUTES sa = {};
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
HANDLE inputReadPipe;
CreatePipe(&inputReadPipe, &inputPipe, &sa, 0);
STARTUPINFO si = {};
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = inputReadPipe;
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
PROCESS_INFORMATION pi = {};
CreateProcess(
NULL,
path,
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&si,
&pi);
}
extern "C" __declspec(dllexport) void SendToChildProcess(LPTSTR msg) {
size_t len = lstrlen(msg) * sizeof(TCHAR);
WriteFile(inputPipe, msg, len, NULL, NULL);
}
// outer.exe
using System;
namespace ConsoleApplication1
{
public class Program
{
public static void Main(string[] args)
{
Console.InputEncoding = System.Text.Encoding.Unicode;
while (true)
{
var x = Console.ReadLine();
Console.WriteLine(x);
if (x == "{\"type\":\"close\"}")
{
break;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment