Foros del Web » Programación para mayores de 30 ;) » .NET »

Ayuda win32.beginupdateresource(), win32.updateresource(), no funciona.

Estas en el tema de Ayuda win32.beginupdateresource(), win32.updateresource(), no funciona. en el foro de .NET en Foros del Web. Estube probando varias maneras de cambiar los rescursos de un exe, al final lo que ando usando es: Código: using System; using System.Collections; using System.Collections.Generic; ...
  #1 (permalink)  
Antiguo 01/11/2010, 19:05
 
Fecha de Ingreso: octubre-2008
Mensajes: 47
Antigüedad: 15 años, 6 meses
Puntos: 0
Ayuda win32.beginupdateresource(), win32.updateresource(), no funciona.

Estube probando varias maneras de cambiar los rescursos de un exe, al final lo que ando usando es:



Código:
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; }

    }
}
Para ver si funciona lo que hago es "descomprimir" el recurso del exe modificado.
Supuestamente deberia mostrar el nuevo .txt, pero no... cuando lo abro contiene el texto viejo.

Lo que hago para descomprimir los recursos del exe modificado es:



Código:
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();

      }
Redondeando....
Con exe1 cambio un recurso de exe2 (un .txt). Despues abro el exe2 y descomprimo el recurso .txt . Este en lugar de mostrar el nuevo texto, muestra el texto del recurso original.
Alguna idea?

Saludos

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 13:10.