Search code examples
c#mql5

Empty string array


I want to get arr[] Array from C# code and pass it to Mql5 Script. To do this, I tried to get a string array out of a form load (see "Result" string array)and pass it to the main function. But I get empty array result("Serial" variable) when I call the main program from MQL5 script.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Text;
using System.Threading;


namespace Keygen
{
    public static class Program
    {
        [STAThread]        
        public static void Main( string[] SoftStatus)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
 System.Threading.Thread.CurrentThread.SetApartmentState(System.Threading.ApartmentState.STA);

            Form1 form1 = new Form1();
            Application.Run(form1);
            SoftStatus = form1.Result;
           
        }
        
    }
}

Here is the code for Form1_Load:

using System.Data;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Windows.Forms;

namespace Keygen
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string[] Result { get; private set; }
        private void Form1_Load(object sender, EventArgs e)
        {
            CPUIdFinished = false;
            BaseBoardSerialFinished = false;
            PhysicalMediaSerialFinished = false;
            SerialStatus = false;

            if (Properties.Settings.Default.ActiveSerial == "")
            {
                button1.Visible = true;
                pictureBox1.Visible = true;
                pictureBox2.Visible = false;
                new Form2().ShowDialog();
                //SerialStatus = true;

            }
            else
            {
                backgroundWorker1.RunWorkerAsync();

                SerialStatus = status;
            }

        }
string CPUId; bool CPUIdFinished;
private setSerials()
        {
            string[] arr =new string[6];
            arr[0] = "a";
            arr[1] = "B";
            arr[2] = "a";
            arr[3] = "B";
            arr[4] = "a";
            arr[5] = "C";
            if (CPUIdFinished)
            {
                arr[0] = "Aa";
                arr[1] = "Bb";
                arr[2] = "Cc";
                arr[3] = "Dd";
                arr[4] = "Ee";
                arr[5] = "Ff";

               }
            for (int j = 0; j < 6; j++) if (arr[j] == null) arr[j] = "";
            Result = arr;
            
        }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    e.Result = GetCPUId();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    CPUId = e.Result.ToString();
    CPUIdFinished = true;
    setSerials();
}
}
}

Here is my mql5 code which i imported the C# Class:

#import "Keygen.dll"
string Serial[6];
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---

 Program::Main(Serial);
 for(int i=0;i<6;i++)Print("Serial: ",Serial[i]);
  }
//+------------------------------------------------------------------+

Solution

  • Since Serial cannot be passed by ref, SoftStatus is a val referencing a string array. Setting the value of SoftStatus won't affect Serial. You could copy the array elements instead.

    public partial class Form1 : Form
    {
        string[] Result { get; } // read-only!
    
        public Form1(string[] result)
        {
            Result = result; // initialize, not set; by val of object ref
            InitializeComponent();
        }
    
        private setSerials()
        {
            // ...
            Array.Copy(arr, Result, 6);
        }
    
        //...
    }
    
    Form1 form1 = new Form1(SoftStatus);
    Application.Run(form1);
    // now SoftStatus has the 6 values updated