Пустой строковый массив - PullRequest
0 голосов
/ 04 августа 2020

Я хочу получить массив arr [] из кода C# и передать его в Mql5 Script. Для этого я попытался получить строковый массив из загрузки формы (см. Строковый массив «Результат») и передать его в основную функцию. Но я получаю пустой результат массива (переменная "Serial"), когда вызываю основную программу из скрипта MQL5.

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;
           
        }
        
    }
}

Вот код для 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();
}
}
}

Вот мой mql5-код, который я импортировал 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]);
  }
//+------------------------------------------------------------------+

1 Ответ

0 голосов
/ 04 августа 2020

Поскольку Serial не может быть передано с помощью ref, SoftStatus является val ссылкой на массив строк. Установка значения SoftStatus не повлияет на Serial. Вместо этого вы можете скопировать элементы массива.

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
...