2

我正在尝试在 as400 仿真器(IBM 的 Personal Communications iSeries)中自动化一个过程,为此,我使用了一些代码(我完全没有从CodeProject窃取),它使用 EHLLAPI 连接到仿真器。

我创建了一个 Winforms 来实现带有一些按钮和一些文本框的代码来测试它。这是从 EHLLAPI 公开方法的类:

public class EhllapiFunc //Class used to import the DLL.
{
    [DllImport("PCSHLL32.dll")]
    public static extern UInt32 hllapi(out UInt32 Func, StringBuilder Data, out UInt32 Length, out UInt32 RetC);
}

class EhllapiWrapper
{
    /*These are the constants used to as function codes that are sent as parameters to the function in the DLL. 
    There are a lot more constants but these are the only ones used for the moment*/
    const UInt32 HA_CONNECT_PS = 1; /* 000 Connect PS*/
    const UInt32 HA_DISCONNECT_PS = 2; /* 000 Disconnect PS*/
    const UInt32 HA_COPY_PS_TO_STR = 8; /* 000 Copy PS to String*/
    
    /*EHLLAPI return codes. There are a lot more. I'll leave just some of them as I'm not getting any return codes in my issue*/
    const UInt32
        HARC_SUCCESS = 0; /* 000 Good return code.*/
    const UInt32
        HARC99_INVALID_INP = 0; /* 000 Incorrect input*/
    const UInt32
        HARC_INVALID_PS = 1; /* 000 Invalid PS, Not*/
    const UInt32
        HARC_BAD_PARM = 2; /* 000 Bad parameter, or*/
    
    //Method to connect to the emulator.
    public static UInt32 Connect(string sessionID)
    {
        StringBuilder Data = new StringBuilder(4);
        Data.Append(sessionID);
        UInt32 rc = 0;
        UInt32 f = HA_CONNECT_PS;
        UInt32 l = 4;
        return EhllapiFunc.hllapi(out f, Data, out l, out rc);
    }

    //Method to disconnect.
    public static UInt32 Disconnect(string sessionID)
    {
        StringBuilder Data = new StringBuilder(4);
        Data.Append(sessionID);
        UInt32 rc = 0;
        UInt32 f = HA_DISCONNECT_PS;
        UInt32 l = 4;
        return EhllapiFunc.hllapi(out f, Data, out l, out rc);
    }

    /*This is the method where I'm having the problem.*/
    public static UInt32 ReadScreen(int position, int len, out string txt)
    {
        StringBuilder Data = new StringBuilder(3000);
        UInt32 rc = (UInt32)position;
        UInt32 f = HA_COPY_PS_TO_STR;
        UInt32 l = (UInt32)len;
        UInt32 r = EhllapiFunc.hllapi(out f, Data, out l, out rc);
        txt = Data.ToString();
        return r;
    }
}

这是我用来调用表单上的方法的 OnClick 事件:

private void bReadString_Click(object sender, EventArgs e)
{
    //I tried cleaning the TextBox where the outputed string is sent before calling the method.
    tbReadOut.Text = "";
    string s;
    //I send the position and string length entered on the form to the method and then "s" is modified inside the method "ReadScreen".
    EhllapiWrapper.ReadScreen(Convert.ToInt32(tbReadPos.Text), Convert.ToInt32(tbReadLen.Text), out s);
    tbReadOut.Text = s;
}

这是该问题的一个示例。我应该从光标位置“30”得到一个大小为 2 个字符的字符串:

我得到的垃圾在“字符串结果”上 我上的垃圾

这是我应该得到的模拟器的区域,只是“Sy”: 在此处输入图像描述

这有时有效,有时无效。我尝试了模拟器的不同部分,问题是一样的。获取光标位置并发送字符串工作正常,只是当我试图从模拟器中获取字符串时。

我什至不知道在哪里寻找答案,因为这段代码最初是在 2005 年发布的。

4

1 回答 1

2

我发现了问题。在方法中,ReadScreen我制作了一个StringBuilder需要 3000 个字符的方法。当我在out方法的参数中使用时,我正在处理一个内存地址而不是两个变量。当我为至少 3000 个字符分配空间时,当我读取的字符串很小时,有时它会带有来自“未使用”空间的垃圾。我所做的是将预期的大小更改为StringBuilder我试图读取的字符串的长度减 1(如果它只是长度,我仍然会得到一个垃圾字符)。这是代码:

public static UInt32 ReadScreen(int position, int len, out string txt)
{
    StringBuilder Data = new StringBuilder(len - 1); //This line was the problem.
    UInt32 rc = (UInt32)position;
    UInt32 f = HA_COPY_PS_TO_STR;
    UInt32 l = (UInt32)len;
    UInt32 r = EhllapiFunc.hllapi(out f, Data, out l, out rc);
    txt = Data.ToString();
    return r;
}

我的解释可能是错误的,因为我还在学习 C#,而且代码最初是 2005 年的,但是这个修改对我有用。如果我误解了什么或者我有什么错误,请随时纠正我。

于 2021-10-15T13:52:43.030 回答