Preferably C++, it should work for all programs, no exception. I should make automation and decrease the size of program so please not prefer Resource Hacker or dlls. However, if you can’t think of another method, you can write it, I’d be willing to increase the size a little rather than find no way. It should change every info that FileVersionInfo contains.
I tried Windows API, but it just deleted version info, not changed it. I tried a dll called “DSOFile”, it even not deleted version info. Here is a code example.
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr BeginUpdateResource(string pFileName, [MarshalAs(UnmanagedType.Bool)] bool bDeleteExistingResources);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool UpdateResource(IntPtr hUpdate, IntPtr lpType, IntPtr lpName, ushort wLanguage, byte[] lpData, uint cbData);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EndUpdateResource(IntPtr hUpdate, [MarshalAs(UnmanagedType.Bool)] bool fDiscard);
private static IntPtr StringToIntPtr(string s)
{
return Marshal.StringToHGlobalUni(s);
}
public static bool UpdateVersionResource(string filePath, string resourceName, string newValue)
{
IntPtr handle = BeginUpdateResource(filePath, false);
if (handle == IntPtr.Zero)
{
Console.WriteLine($"Error opening file for update: {resourceName}");
return false;
}
// Create the structure for the version info resource
var versionInfo = new VS_VERSIONINFO
{
Length = (ushort)(Marshal.SizeOf(typeof(VS_VERSIONINFO)) + (newValue.Length + 1) * 2),
ValueLength = (ushort)(newValue.Length + 1),
Type = 1,
Key = "VS_VERSION_INFO",
Value = newValue
};
int size = Marshal.SizeOf(versionInfo);
IntPtr buffer = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(versionInfo, buffer, false);
byte[] data = new byte[size];
Marshal.Copy(buffer, data, 0, size);
Marshal.FreeHGlobal(buffer);
IntPtr type = new IntPtr(16); // RT_VERSION
IntPtr name = new IntPtr(1); // VS_VERSION_INFO
ushort language = 1033; // English (US)
bool result = UpdateResource(handle, type, name, language, data, (uint)data.Length);
if (!result)
{
Console.WriteLine($"Error updating resource: {resourceName}");
EndUpdateResource(handle, true);
return false;
}
if (!EndUpdateResource(handle, false))
{
Console.WriteLine($"Error finishing update: {resourceName}");
return false;
}
return true;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct VS_VERSIONINFO
{
public ushort Length;
public ushort ValueLength;
public ushort Type;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string Key;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string Value;
}
Yıldız Özkaynak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.