I have several images embedded in my executable in the resource section. I followed these steps to create my executable file:
- The generated .resx file for all images (.jpg) in the directory using some utility. Images are called image1.jpg, image2.jpg, etc.
- created a .resources file from a .resx file using:
resgen myResource.resx - Embeds the generated .resource file using the / res flag as:
csc file.cs /res:myResource.resources
4 I refer to these images as follows:
ResourceManager resources = new ResourceManager("myResource", Assembly.GetExecutingAssembly()); Image foo = (System.Drawing.Image)(resources.GetObject("image1"));
Everything is working fine, as expected. Now I want to change the embedded images to some new images. This is what I am doing now:
class foo { [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, string wLanguage, Byte[] lpData, uint cbData); [DllImport("kernel32.dll", SetLastError = true)] static extern bool EndUpdateResource(IntPtr hUpdate, bool fDiscard); public static void Main(string[] args) { IntPtr handle = BeginUpdateResource(args[0], false); if (handle.ToInt32() == 0) throw new Exception("File Not Found: " + fileName + " last err: " + Marshal.GetLastWin32Error()); byte[] imgData = File.ReadAllBytes("SetupImage1.jpg"); int fileSize = imgData.Length; Console.WriteLine("Updaing resources"); if (UpdateResource(handle, "Image", "image1", "image1", imgData, (uint)fileSize)) { EndUpdateResource(handle, false); Console.WriteLine("Update successfully"); } else { Console.WriteLine("Failed to update resource. err: {0}", Marshal.GetLastWin32Error()); } } }
The above code adds a new resource for the specified image (inside the IMAGE title with some random number, as shown in the Resource hacker ), but I want to change the existing resource data for image1 .
I am sure that I am UpdateResource with some invalid arguments.
Can anyone help point this out?
I am using .NET version 2
Thanks,
Vikram
Vikram.exe
source share