Untitled

 avatar
unknown
csharp
2 years ago
2.1 kB
2
Indexable
internal class Test {
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern IntPtr GetModuleHandle(
        [ MarshalAs(UnmanagedType.LPWStr) ] string lpModuleName);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr FindResource(IntPtr hModule, string lpName,
                                      string lpType);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr LoadResource(IntPtr hModule, IntPtr hResInfo);

    [DllImport("kernel32.dll")]
    static extern IntPtr LockResource(IntPtr hResData);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern uint SizeofResource(IntPtr hModule, IntPtr hResInfo);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool UpdateResource(IntPtr hUpdate, string lpType,
                                      string lpName, ushort wLanguage,
                                      byte[] 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);

    private bool AddResource(string fileName, byte[] bytes) {
        IntPtr hUpdate = BeginUpdateResource(fileName, false);
        bool result = UpdateResource(hUpdate, "LMAO", "STUB", 0, bytes,
                                     (uint) bytes.Length);
        EndUpdateResource(hUpdate, !result);

        return result;
    }

    private byte[] ExtractResource() {
        IntPtr hModule = GetModuleHandle(string.Empty);

        IntPtr hResInfo = FindResource(hModule, "STUB", "LMAO");
        IntPtr hResData = LoadResource(hModule, hResInfo);

        uint size = SizeofResource(hModule, hResInfo);

        IntPtr pResource = LockResource(hResData);

        byte[] buffer = new byte[size];
        Marshal.Copy(pResource, buffer, 0, buffer.Length);

        return buffer;
    }
}
Editor is loading...