Scripts: Programmatic modification of Tester "From:" and "To:" fields with user32.dll

 

Programmatic modification of Tester "From:" and "To:" fields with user32.dll:

This script uses the Windows API to modify the "From:" and "To:" fields within the strategy tester according to user input. For this script the strategy tester should already be open. Tested on Windows 7 64 bit. If you are using another version of win

Author: Mike

 
i think better way to use WriteProcessMemory via Virtual memory.... checked this out:

//+------------------------------------------------------------------+
//|                                                      SetDate.mq4 |
//|                                                     Version: 2.0 |
//+------------------------------------------------------------------+
#property copyright "Copyright (c) 2011, Symr"
#import "user32.dll"
    int GetAncestor(int hWnd, int gaFlags);
    int GetDlgItem(int hDlg, int nIDDlgItem);
    int GetWindow(int hWnd, int uCmd);
    int GetClassNameA(int hWnd, string lpClassname, int nMaxCount);
    int GetWindowThreadProcessId(int hWnd, int& dwProcessId[]);
    int SendMessageA(int hWnd, int Msg, int wParam, int lParam);    
#import
#import "kernel32.dll"
    int OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
    int VirtualAllocEx(int hProcess, int& addr, int size, int dwType, int dwProtect);
   bool VirtualFreeEx(int hProcess, int& addr, int size, int dwType);
   bool WriteProcessMemory(int hProcess, int addr, int buffer[], int size, int& bytes[]);
   void GetSystemTime(int& TimeArray[]);
#import
//+------------------------------------------------------------------+
#define GW_HWNDNEXT         2
#define GW_CHILD            5
#define ID_TERMINAL         0xE81E
#define ID_TESTER           0x53
#define ID_PANEL            0x81BF
#define PROCESS_ALL_ACCESS  0x1F0FFF
#define MEM_COMMIT          0x1000
#define MEM_RELEASE         0x8000
#define PAGE_READWRITE      4
#define DTM_FIRST           0x1000
#define DTM_SETSYSTEMTIME   0x1002
#define GDT_VALID           0
//+------------------------------------------------------------------+
#property show_inputs
#property show_confirm
extern string from_date = "2003.08.11";
extern string to_date   = "2012.05.05";
void start() {
  ChangeDates(0,from_date);
  ChangeDates(1,to_date);
  return (0);
}
//+------------------------------------------------------------------+
int FindControlByID(int hParentWnd, string className, int id) {
  if (id < 0 || hParentWnd <=0) return;
  int cnt = 0;
  int child = GetWindow(hParentWnd,GW_CHILD);
  while (child != 0) {
    child = GetWindow(child,GW_HWNDNEXT);
    string name = "                                                  ";
    int res = GetClassNameA(child,name,50);
    if (name == className || className == "")
      if (cnt == id) break; else cnt++;
  }
  return (child);
}
//+------------------------------------------------------------------+
void SetControlDateTime(int hWnd, datetime time) {
  int hProcessId[1];
  if (GetWindowThreadProcessId(hWnd, hProcessId) == 0) return;
  int hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, hProcessId[0]);
  int addr = 0;
  int p = VirtualAllocEx(hProcess, addr, 2710, MEM_COMMIT, PAGE_READWRITE); 
  int TimeArray[4];
  GetSystemTime(TimeArray);
  int bytes[1]; 
  TimeArray[0] = TimeYear(time) + (TimeMonth(time) * 0x10000);
  TimeArray[1] = TimeDay(time) * 0x10000;
  WriteProcessMemory(hProcess, p, TimeArray, 16, bytes);
  SendMessageA(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, p);
  VirtualFreeEx(hProcess, p, 0, MEM_RELEASE);
}
//+------------------------------------------------------------------+
void ChangeDates (int fieldID, string date) {
  int hMetaTrader = GetAncestor(WindowHandle(Symbol(),Period()),2);
  int hTerminal = GetDlgItem(hMetaTrader, ID_TERMINAL);
  int hTester = GetDlgItem(hTerminal, ID_TESTER);
  int hInner = GetDlgItem(hTester, ID_PANEL);
  int hDtp = FindControlByID(hInner, "SysDateTimePick32", fieldID);
  if (hDtp > 0) SetControlDateTime(hDtp, StrToTime(date));
  return(0);
}
//+------------------------------------------------------------------+
Reason: