空字符串数组

Empty string array

我想从 C# 代码获取 arr[] 数组并将其传递给 Mql5 脚本。 为此,我尝试从表单加载中获取一个字符串数组(请参阅“结果”字符串数组)并将其传递给主函数。 但是当我从 MQL5 脚本调用主程序时,我得到空数组结果(“Serial”变量)。

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

这是我导入 C# 的 mql5 代码 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]);
  }
//+------------------------------------------------------------------+

由于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