封送邮件表资源

Pao*_*sco 4 c# pinvoke resources

我需要阅读C#中的消息表资源。

我基本上是试图将C#中的代码移植到此问题的答案中:使用Win32 API列出存储在仅资源库(DLL)中的消息ID和符号名

我的问题是我无法正确编组MESSAGE_RESOURCE_DATAand MESSAGE_RESOURCE_BLOCK结构,其定义如下<winnt.h>

typedef struct _MESSAGE_RESOURCE_BLOCK {
    DWORD LowId;
    DWORD HighId;
    DWORD OffsetToEntries;
} MESSAGE_RESOURCE_BLOCK, *PMESSAGE_RESOURCE_BLOCK;

typedef struct _MESSAGE_RESOURCE_DATA {
    DWORD NumberOfBlocks;
    MESSAGE_RESOURCE_BLOCK Blocks[ 1 ];
} MESSAGE_RESOURCE_DATA, *PMESSAGE_RESOURCE_DATA;
Run Code Online (Sandbox Code Playgroud)

在中MESSAGE_RESOURCE_DATANumberOfBlocks是数组中的MESSAGE_RESOURCE_BLOCK条目数Blocks(即使它被声明为具有单个元素的数组)。

由于我在编译时不知道数组的大小,因此我尝试将声明Blocks为指针的结构编组为一个指针,然后Marshal.PtrToStructure像这样使用:

using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
struct MESSAGE_RESOURCE_BLOCK {
    public IntPtr LowId;
    public IntPtr HighId;
    public IntPtr OffsetToEntries;
}

[StructLayout(LayoutKind.Sequential)]
struct MESSAGE_RESOURCE_DATA {
    public IntPtr NumberOfBlocks;
    public IntPtr Blocks;
}

class Program {

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr LoadLibrary(string fileName);

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

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

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

    static void Main(string[] args) {
        const int RT_MESSAGETABLE = 11;
        IntPtr hModule = LoadLibrary(@"C:\WINDOWS\system32\msobjs.dll");
        IntPtr msgTableInfo = FindResource(hModule, 1, RT_MESSAGETABLE);
        IntPtr msgTable = LoadResource(hModule, msgTableInfo);
        var data = Marshal.PtrToStructure<MESSAGE_RESOURCE_DATA>(LockResource(msgTable));
        int blockSize = Marshal.SizeOf<MESSAGE_RESOURCE_BLOCK>();
        for (int i = 0; i < data.NumberOfBlocks.ToInt32(); i++) {
            IntPtr blockPtr = IntPtr.Add(data.Blocks, blockSize * i);
            // the following line causes an access violation
            var block = Marshal.PtrToStructure<MESSAGE_RESOURCE_BLOCK>(blockPtr);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用,并且出现访问冲突错误。

我该如何整理这样的结构?

Han*_*ant 5

您尚未关闭,这些结构不包含IntPtr。DWORD是32位整数。资源格式中使用的可变长度结构在C#中非常笨拙,没有合适的方法声明它们。最好的办法是使用Marshal.ReadXxx()读取字段。

唯一仍然有用的结构声明是:

[StructLayout(LayoutKind.Sequential)]
struct MESSAGE_RESOURCE_BLOCK {
    public int LowId;
    public int HighId;
    public int OffsetToEntries;
}
Run Code Online (Sandbox Code Playgroud)

然后您将其插入如下:

static void Main(string[] args) {
    const int RT_MESSAGETABLE = 11;
    IntPtr hModule = LoadLibrary(@"C:\WINDOWS\system32\msobjs.dll");
    IntPtr msgTableInfo = FindResource(hModule, 1, RT_MESSAGETABLE);
    IntPtr msgTable = LoadResource(hModule, msgTableInfo);
    IntPtr memTable = LockResource(msgTable);

    int numberOfBlocks = Marshal.ReadInt32(memTable);
    IntPtr blockPtr = IntPtr.Add(memTable, 4);
    int blockSize = Marshal.SizeOf<MESSAGE_RESOURCE_BLOCK>();

    for (int i = 0; i < numberOfBlocks; i++) {
        var block = Marshal.PtrToStructure<MESSAGE_RESOURCE_BLOCK>(blockPtr);
        IntPtr entryPtr = IntPtr.Add(memTable, block.OffsetToEntries);

        for (int id = block.LowId; id <= block.HighId; id++) {
            var length = Marshal.ReadInt16(entryPtr);
            var flags = Marshal.ReadInt16(entryPtr, 2);
            var textPtr = IntPtr.Add(entryPtr, 4);
            var text = "Bad flags??";
            if (flags == 0) {
                text = Marshal.PtrToStringAnsi(textPtr);
            }
            else if (flags == 1) {
                text = Marshal.PtrToStringUni(textPtr);
            }
            text = text.Replace("\r\n", "");
            Console.WriteLine("{0} : {1}", id, text);
            entryPtr = IntPtr.Add(entryPtr, length);
        }
        blockPtr = IntPtr.Add(blockPtr, blockSize);
    }
}
Run Code Online (Sandbox Code Playgroud)

以32位和64位模式输出:

279 : Undefined Access (no effect) Bit 7
1536 : Unused message ID
1537 : DELETE
1538 : READ_CONTROL
1539 : WRITE_DAC
1540 : WRITE_OWNER
1541 : SYNCHRONIZE
1542 : ACCESS_SYS_SEC
1543 : MAX_ALLOWED
1552 : Unknown specific access (bit 0)
1553 : Unknown specific access (bit 1)
1554 : Unknown specific access (bit 2)
1555 : Unknown specific access (bit 3)
1556 : Unknown specific access (bit 4)
1557 : Unknown specific access (bit 5)
...etc...
Run Code Online (Sandbox Code Playgroud)

请记住,当您使用抖动强制以32位模式运行程序时,您将不会读取自己认为的文件。文件系统重定向器将使您读取C:\ WINDOWS \ SysWow64 \ msobjs.dll。