I am encoding a simple C # application that allows me to add a resource to the .EXE file that I selected. The problem is that the call to the UpdateResource function fails with error 6, which, according to MSDN, is InvalidHandle (despite the fact that the BeginUpdateResource call seems to be successful) (the code is copied and pasted from a larger file, so if some, anyway , the code compiles, but does not work properly)
public partial class Form1 : Form
{
[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 IntPtr BeginUpdateResource(string pFileName, [MarshalAs(UnmanagedType.Bool)]bool bDeleteExistingResources);
[DllImport("kernel32.dll",SetLastError=true)]
static extern bool EndUpdateResource(IntPtr hUpdate, bool fDiscard);
static unsafe void SetRes(string path)
{
IntPtr beginPointer = BeginUpdateResource(path, false);
if (beginPointer != null)
{
MessageBox.Show("Begin works");
ushort id = (ushort)Language.MakeLanguageID();
string newMessage = "hello world!";
Byte[] bytes = new ASCIIEncoding().GetBytes(newMessage);
GCHandle bHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
IntPtr ptr = bHandle.AddrOfPinnedObject();
bool update = UpdateResource(beginPointer,"FILE", "Test", id,ptr, (uint)bytes.Length);
if (update == true)
{
MessageBox.Show("Update");
EndUpdateResource(beginPointer, false);
}
else
{
MessageBox.Show(Marshal.GetLastWin32Error().ToString());
}
}
}
source
share