简体   繁体   中英

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| data from the end of the file. The |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| 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. (If the data can be more than 64KB, then use 4 bytes to store the length.)

Thus, the builder should do the following:

  1. Compose |CName|Link| string data and store it in the info variable.
  2. Convert the string in info into a byte array with a chosen text encoding. (I would suggest using the UTF-8 text encoding.)
  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.


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| can be read.
  4. Read the 2-byte number and store it into an ushort (or int ) variable dataLength .
  5. Set the current file position to fileSize-2-dataLength.
  6. Read the bytes of the |CName|Link| data. The number of bytes to read is specified by dataLength .
  7. Process the |CName|Link| data bytes. For example, convert them into a string, using the same( ! ) text encoding that has been used by the builder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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