使用C#编辑PE头

时间:2017-11-06 10:02:00

标签: c# resources portable-executable

我想要做的事实上是非常基本的,但我找不到合适的切入点。 我有一个可执行文件(不是CLR),我想在文件的资源中添加(以后读取)一些文本。

到目前为止,我发现PeNet用于枚举PE标头,MSDN article用C方法修改资源。但是这需要我将一个字符串压缩成一个资源?...

修改 使用this sample我尝试更改资源,但在运行代码后,我丢失了可执行文件的内容。

这是我到目前为止使用的代码:

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern IntPtr BeginUpdateResource(string pFileName, [MarshalAs(UnmanagedType.Bool)]bool bDeleteExistingResources);

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern bool UpdateResource(IntPtr hUpdate, uint lpType, uint lpName, ushort wLanguage, byte[] lpData, uint cbData);

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern bool EndUpdateResource(IntPtr hUpdate, bool fDiscard);

    var path = @"c:\temp\some.exe";
    var arr = Encoding.UTF8.GetBytes("this is my text");

    IntPtr ipTarget = BeginUpdateResource(outPath, false);
    if (UpdateResource(ipTarget, 6/*RT_STRING*/, 1234/*some name*/, 1033, arr, (uint)arr.Length))
    {
        EndUpdateResource(ipTarget, false);
    }

0 个答案:

没有答案
相关问题