krosty4782 0 Light Poster

Hi people, i have been trying a lot of codes to change a resource of a compiled exe, finally what im using is this:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr BeginUpdateResource(string pFileName, bool bDeleteExistingResources);
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool UpdateResource(IntPtr hUpdate, string lpType, string lpName, ushort wLanguage, IntPtr lpData, uint cbData);
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool EndUpdateResource(IntPtr hUpdate, bool fDiscard);
        static unsafe void Main()
        {
            string path = "c:\\users\\sean\\desktop\\resourcer.exe";

            IntPtr hResource = BeginUpdateResource(path, false);
            if (hResource.ToInt32() == 0)
                throw new Exception("File Not Found");
  
            string newMessage = File.ReadAllText("c:\\users\\sean\\desktop\\newMessage.txt");
            Byte[] bytes = new ASCIIEncoding().GetBytes(newMessage);
            GCHandle bHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
            IntPtr ptr = bHandle.AddrOfPinnedObject();

            ushort id = (ushort)Language.MakeLanguageID();

            if (UpdateResource(hResource, "FILE", "message.txt", id, ptr, (uint)bytes.Length))
                EndUpdateResource(hResource, false);
            else
                MessageBox.Show("Did not update", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    public static class Language
    {

        public static int MakeLanguageID()
        {
            CultureInfo currentCulture = CultureInfo.CurrentCulture;
            int pid = GetPid(currentCulture.LCID);
            int sid = GetSid(currentCulture.LCID); 
            return (((ushort)pid) << 10) | ((ushort)sid);
        }


        public static int GetPid(int lcid)
        { return ((ushort)lcid) & 0x3ff; }


        public static int GetSid(int lcid)
        { return ((ushort)lcid) >> 10; }

    }
}

What im doing to see if it working is to "descompress" the resource (the txt) and look if the text has change, but really not...

The code that im using to descompress the resource is:

string strNewPathToSave = "new.txt";
      Assembly assembly = Assembly.GetExecutingAssembly();     
      Stream resourceStream = assembly.GetManifestResourceStream("WindowsFormsApplication2.Resources.eva.txt");
      System.IO.FileStream fs = System.IO.File.OpenWrite(strNewPathToSave);
      try
      {
        // Save the File...
        byte[] buffer = new byte[resourceStream.Length];
        resourceStream.Read(buffer, 0, (int)resourceStream.Length);
        fs.Write(buffer, 0, buffer.Length);
      }
      finally
      {
        fs.Close();
      }

So what i do is to open the exe that changes the resource of app2.

Then open app2, and it makes a new .txt called: "new.txt" its suppouse it must have the new text, but it have the old text.

Any idea?

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.