简体   繁体   English

如何在WPF C#中使用生成器和存根

[英]How to use builder and stub in WPF C#

I try to build an application using a builder and a stub but i fail 我尝试使用生成器和存根构建应用程序,但失败了

My builder code : 我的生成器代码:

File.Copy(AppDomain.CurrentDomain.BaseDirectory + @"\Camstub.exe", filepath);
            string split = "|";
            string info = split + CName.Text + split + Link.Text + split;

            FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
            BinaryWriter bw = new BinaryWriter(fs);
            fs.Position = fs.Length + 1;
            bw.Write(info);
            bw.Close();
            MessageBox.Show(info);

My stub code : 我的存根代码:

public MainWindow()
    {
        InitializeComponent();

        StreamReader sr = new StreamReader(System.Windows.Forms.Application.ExecutablePath);
        BinaryReader br = new BinaryReader(sr.BaseStream);
        byte[] fileData = br.ReadBytes(Convert.ToInt32(sr.BaseStream.Length));
        br.Close();
        sr.Close();

        ASCIIEncoding Enc = new ASCIIEncoding();
        string split = "|";
        string Message = Enc.GetString(fileData);
        MessageBox.Show(Message);

The messagebox in the builder show me: 生成器中的消息框显示给我:

http://embed.gyazo.com/d8f55ce5ea608b3861e64862242b0012.png

The application is sucessfully build but the messagebox when i execute it show me: 该应用程序已成功构建,但执行我时显示的消息框显示:

在此处输入图片说明

So, I expect the same messagebox in both. 因此,我希望两者都具有相同的消息框。

Any idea ? 任何想法 ?

Thanks in advance ;) 提前致谢 ;)

Based on the information given in the comments, you want to read the |CName|Link| 根据注释中给出的信息,您想阅读| CName | Link |。 data from the end of the file. 文件末尾的数据。 The |CName|Link| | CName | Link | data can be of variable length. 数据可以具有可变长度。

To deal with variable length data you somehow need to indicate the byte length of the data. 为了处理可变长度的数据,您需要以某种方式指示数据的字节长度。 In the given scenario here, a simple and suitable solution would be to store the byte length as a 2-byte or 4-byte number at the end of the file after the |CName|Link| 在给定的情况下,一个简单而合适的解决方案是将字节长度存储为| CName | Link |之后的文件末尾的2字节或4字节数字。 data. 数据。 A 2-byte number (ie, ushort or UInt16 ) allows to specify a length of up to 65536 and should likely be sufficient for your purpose. 一个2字节的数字(即ushortUInt16 )允许指定一个最大长度为65536的长度,并且应该足以满足您的目的。 (If the data can be more than 64KB, then use 4 bytes to store the length.) (如果数据可以大于64KB,则使用4个字节存储长度。)

Thus, the builder should do the following: 因此,构建器应执行以下操作:

  1. Compose |CName|Link| 撰写| CName | Link | string data and store it in the info variable. 字符串数据并将其存储在info变量中。
  2. Convert the string in info into a byte array with a chosen text encoding. 转换在信息串入一个字节数组具有选定文本编码。 (I would suggest using the UTF-8 text encoding.) (我建议使用UTF-8文本编码。)
  3. Append this byte array to the executable file. 将此字节数组追加到可执行文件。
  4. Append the length of the byte array as a 2-byte number to the end of the executable file. 将字节数组的长度作为2字节数字附加到可执行文件的末尾。


The stub should do something like this: 存根应执行以下操作:

  1. Determine the file size of the executable file. 确定可执行文件的文件大小。
  2. Open the executable file for reading. 打开可执行文件以进行读取。
  3. Set the current file position to fileSize-2, so the length of the |CName|Link| 将当前文件位置设置为fileSize-2,因此| CName | Link |的长度。 can be read. 可以阅读。
  4. Read the 2-byte number and store it into an ushort (or int ) variable dataLength . 读取2个字节的数字,并将其存储到ushort (或int )变量dataLength中
  5. Set the current file position to fileSize-2-dataLength. 将当前文件位置设置为fileSize-2-dataLength。
  6. Read the bytes of the |CName|Link| 读取| CName | Link |的字节。 data. 数据。 The number of bytes to read is specified by dataLength . 读取的字节数由dataLength指定。
  7. Process the |CName|Link| 处理| CName | Link | data bytes. 数据字节。 For example, convert them into a string, using the same( ! ) text encoding that has been used by the builder. 例如,使用构建器已使用的相同( )文本编码将它们转换为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM