Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取精确的一个结构字段_C#_.net_Winapi - Fatal编程技术网

C# 读取精确的一个结构字段

C# 读取精确的一个结构字段,c#,.net,winapi,C#,.net,Winapi,以下是获取系统信息的代码: using System; using System.Runtime.InteropServices; public class win{ [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO{ public ushort wProcessorArchitecture; public ushort wReserved; publi

以下是获取系统信息的代码:

using System;
using System.Runtime.InteropServices;

public class win{
    [StructLayout(LayoutKind.Sequential)]
    public struct SYSTEM_INFO{
        public ushort wProcessorArchitecture;
        public ushort wReserved;
        public uint dwPageSize;
        public IntPtr lpMinimumApplicationAddress;
        public IntPtr lpMaximumApplicationAddress;
        public UIntPtr dwActiveProcessorMask;
        public uint dwNumberOfProcessors;
        public uint dwProcessorType;
        public uint dwAllocationGranularity;
        public ushort wProcessorLevel;
        public ushort wProcessorRevision;
    };
    [DllImport("kernel32.dll")]
    public static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSystemInfo);

    public static void Main(){
        var sysInfo = new SYSTEM_INFO();

        GetNativeSystemInfo(ref sysInfo);
        var res = sysInfo.wProcessorArchitecture;
        Console.WriteLine(res);
    }
}
输出为
9
。 有没有一种方法可以在不定义所有结构的情况下读取一个结构字段

例如,获取第一个字段的值:

using System;
using System.Runtime.InteropServices;

public class win{
    [DllImport("kernel32.dll")]
    public static extern void GetNativeSystemInfo(IntPtr lpSystemInfo);

    public static void Main(){
        var sysInfo = Marshal.AllocHGlobal(96);
        GetNativeSystemInfo(sysInfo);
        var res = Marshal.ReadByte(sysInfo, 0);
        Console.WriteLine(res);
    }
}
输出为
9
。但如何获取第三个字段的值呢?缓冲区内可能有标记,可以告诉我,新字段开始了吗

例如,如何获取第三个字段的值

只需在前两个字段后设置偏移量:

using System;
using System.Runtime.InteropServices;

public class win {
    [DllImport("kernel32.dll")]
    public static extern void GetNativeSystemInfo(IntPtr lpSystemInfo);

    public static void Main() {
        var offset = 4;
        var sysInfo = Marshal.AllocHGlobal(offset + 4);
        GetNativeSystemInfo(sysInfo);
        var res = Marshal.ReadInt32(sysInfo, offset);
        Marshal.FreeHGlobal(sysInfo);
        Console.WriteLine(res);
    }
}
输出:
4096
dwPageSize

例如,如何获取第三个字段的值

只需在前两个字段后设置偏移量:

using System;
using System.Runtime.InteropServices;

public class win {
    [DllImport("kernel32.dll")]
    public static extern void GetNativeSystemInfo(IntPtr lpSystemInfo);

    public static void Main() {
        var offset = 4;
        var sysInfo = Marshal.AllocHGlobal(offset + 4);
        GetNativeSystemInfo(sysInfo);
        var res = Marshal.ReadInt32(sysInfo, offset);
        Marshal.FreeHGlobal(sysInfo);
        Console.WriteLine(res);
    }
}

输出:
4096
dwPageSize

数据是一个二进制数据块。如果您有两个字节(1,2)和一个int(0x100),那么它看起来像二进制的01 02 00 00。数据中没有其他数据告诉您数据类型所在的偏移量。这就是结构定义的工作。您需要通过定义结构或手动硬编码所有极易出错的偏移量来知道偏移量的位置。为什么要这样做?您正在与一个C API通信。在C语言中,数据结构在类型系统中编码。类型系统仅对编译器可用。一旦完成,目标代码中就没有元数据了。如果您需要使数据有意义,那么您必须用您使用的任何语言复制它的结构。如果您不想自己编写结构定义,请检查。数据是一个二进制数据块。如果您有两个字节(1,2)和一个int(0x100),那么它看起来像二进制的01 02 00 00。数据中没有其他数据告诉您数据类型所在的偏移量。这就是结构定义的工作。您需要通过定义结构或手动硬编码所有极易出错的偏移量来知道偏移量的位置。为什么要这样做?您正在与一个C API通信。在C语言中,数据结构在类型系统中编码。类型系统仅对编译器可用。一旦完成,目标代码中就没有元数据了。如果您需要使数据有意义,那么您必须用您使用的任何语言复制它的结构。如果您不想自己编写结构定义,请签出。