×

Loading...
Ad by
Ad by

microsoft driver级别很多union。主要是为了省内村。

typedef struct _IO_STACK_LOCATION {
  UCHAR  MajorFunction;
  UCHAR  MinorFunction;
  UCHAR  Flags;
  UCHAR  Control;
  union {
        //
        // Parameters for IRP_MJ_CREATE 
        //
        struct {
            PIO_SECURITY_CONTEXT SecurityContext;
            ULONG Options;
            USHORT POINTER_ALIGNMENT FileAttributes;
            USHORT ShareAccess;
            ULONG POINTER_ALIGNMENT EaLength;
        } Create;
        //
        // Parameters for IRP_MJ_READ 
        //
        struct {
            ULONG Length;
            ULONG POINTER_ALIGNMENT Key;
            LARGE_INTEGER ByteOffset;
        } Read;
        //
        // Parameters for IRP_MJ_WRITE 
        //
        struct {
            ULONG Length;
            ULONG POINTER_ALIGNMENT Key;
            LARGE_INTEGER ByteOffset;
        } Write;
        //
        // Parameters for IRP_MJ_QUERY_INFORMATION 
        //
        struct {
            ULONG Length;
            FILE_INFORMATION_CLASS POINTER_ALIGNMENT FileInformationClass;
        } QueryFile;
        //
        // Parameters for IRP_MJ_SET_INFORMATION 
        //
        struct {
            ULONG Length;
            FILE_INFORMATION_CLASS POINTER_ALIGNMENT FileInformationClass;
            PFILE_OBJECT FileObject;
            union {
                struct {
                    BOOLEAN ReplaceIfExists;
                    BOOLEAN AdvanceOnly;
                };
                ULONG ClusterCount;
                HANDLE DeleteHandle;
            };
        } SetFile;
        //
        // Parameters for IRP_MJ_QUERY_VOLUME_INFORMATION 
        //
        struct {
            ULONG Length;
            FS_INFORMATION_CLASS POINTER_ALIGNMENT FsInformationClass;
        } QueryVolume;
        //
        // Parameters for IRP_MJ_DEVICE_CONTROL and IRP_MJ_INTERNAL_DEVICE_CONTROL 
        //
        struct {
            ULONG OutputBufferLength;
            ULONG POINTER_ALIGNMENT InputBufferLength;
            ULONG POINTER_ALIGNMENT IoControlCode;
            PVOID Type3InputBuffer;
        } DeviceIoControl;
        //
        // Nonsystem service parameters.
        //
        // Parameters for IRP_MN_MOUNT_VOLUME 
        //
        struct {
            PVOID DoNotUse1;
            PDEVICE_OBJECT DeviceObject;
        } MountVolume;
        //
        // Parameters for IRP_MN_VERIFY_VOLUME 
        //
        struct {
            PVOID DoNotUse1;
            PDEVICE_OBJECT DeviceObject;
        } VerifyVolume;
        //
        // Parameters for Scsi using IRP_MJ_INTERNAL_DEVICE_CONTROL 
        //
        struct {
            struct _SCSI_REQUEST_BLOCK *Srb;
        } Scsi;
        //
        // Parameters for IRP_MN_QUERY_DEVICE_RELATIONS 
        //
        struct {
            DEVICE_RELATION_TYPE Type;
        } QueryDeviceRelations;
        //
        // Parameters for IRP_MN_QUERY_INTERFACE 
        //
        struct {
            CONST GUID *InterfaceType;
            USHORT Size;
            USHORT Version;
            PINTERFACE Interface;
            PVOID InterfaceSpecificData;
        } QueryInterface;
        //
        // Parameters for IRP_MN_QUERY_CAPABILITIES 
        //
        struct {
            PDEVICE_CAPABILITIES Capabilities;
        } DeviceCapabilities;
        //
        // Parameters for IRP_MN_FILTER_RESOURCE_REQUIREMENTS 
        //
        struct {
            PIO_RESOURCE_REQUIREMENTS_LIST IoResourceRequirementList;
        } FilterResourceRequirements;
        //
        // Parameters for IRP_MN_READ_CONFIG and IRP_MN_WRITE_CONFIG 
        //
        struct {
            ULONG WhichSpace;
            PVOID Buffer;
            ULONG Offset;
            ULONG POINTER_ALIGNMENT Length;
        } ReadWriteConfig;
        //
        // Parameters for IRP_MN_SET_LOCK 
        //
        struct {
            BOOLEAN Lock;
        } SetLock;
        //
        // Parameters for IRP_MN_QUERY_ID 
        //
        struct {
            BUS_QUERY_ID_TYPE IdType;
        } QueryId;
        //
        // Parameters for IRP_MN_QUERY_DEVICE_TEXT 
        //
        struct {
            DEVICE_TEXT_TYPE DeviceTextType;
            LCID POINTER_ALIGNMENT LocaleId;
        } QueryDeviceText;
        //
        // Parameters for IRP_MN_DEVICE_USAGE_NOTIFICATION 
        //
        struct {
            BOOLEAN InPath;
            BOOLEAN Reserved[3];
            DEVICE_USAGE_NOTIFICATION_TYPE POINTER_ALIGNMENT Type;
        } UsageNotification;
        //
        // Parameters for IRP_MN_WAIT_WAKE 
        //
        struct {
            SYSTEM_POWER_STATE PowerState;
        } WaitWake;
        //
        // Parameter for IRP_MN_POWER_SEQUENCE 
        //
        struct {
            PPOWER_SEQUENCE PowerSequence;
        } PowerSequence;
        //
        // Parameters for IRP_MN_SET_POWER and IRP_MN_QUERY_POWER 
        //
        struct {
            ULONG SystemContext;
            POWER_STATE_TYPE POINTER_ALIGNMENT Type;
            POWER_STATE POINTER_ALIGNMENT State;
            POWER_ACTION POINTER_ALIGNMENT ShutdownType;
        } Power;
        //
        // Parameters for IRP_MN_START_DEVICE 
        //
        struct {
            PCM_RESOURCE_LIST AllocatedResources;
            PCM_RESOURCE_LIST AllocatedResourcesTranslated;
        } StartDevice;
        //
        // Parameters for WMI Minor IRPs 
        //
        struct {
            ULONG_PTR ProviderId;
            PVOID DataPath;
            ULONG BufferSize;
            PVOID Buffer;
        } WMI;
        //
        // Others - driver-specific
        //
        struct {
            PVOID Argument1;
            PVOID Argument2;
            PVOID Argument3;
            PVOID Argument4;
        } Others;
    } Parameters;
  PDEVICE_OBJECT  DeviceObject;
  PFILE_OBJECT  FileObject;
  .
  .
} IO_STACK_LOCATION, *PIO_STACK_LOCATION;
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / 标准C的问题, 解释一下结果
    #include "stdafx.h"

    int i=20;

    int cube(int i) {
    return i*i*i;
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    union
    {
    int a;
    int b;

    }dir;
    dir.a = 12;
    dir.b = 13;
    int i=10;
    printf("%d\n", cube(i));
    printf("%d\n", dir.a+dir.b);
    system("pause");
    return 0;
    }

    运行结果是1000
    26
    • 小儿科,你知道什么是union { int a; int b; }dir;吗??
      • 模模糊糊,为什么是26?
        • 你这学习态度也太糟了吧. 这是教科书里的最基本概念, 随便翻本书就知道了.
        • this union here only has 4 bytes space ether interpret as a OR b; and you have globe i and local i, the compiler take the local i in the case.
          • 以前只知道共享内存, 开来还是没真正理解。 多谢
            • union就是多位一体。一个家伙有多个名字。a和b只是一个存储地址的不同名字而已。或者说&(dir.a) == &(dir.b)
            • 你现在还没有理解共享内存
              共享内存和union是完全不同的两码事。
              联合是联合,任何一本C入门读物上都有介绍
              共享内存是IPC的一个机制,你可以google下IPC 共享内存来看看。
              • 同意, 还真没理解好
                我把union该了, 现在结果为什么不是26.0呢?

                union
                {
                int a;
                float b;

                }dir;
                dir.a = 12;
                dir.b = 13.0;

                printf("%f ", dir.a+dir.b);
                • dir.a+dir.b == *(int*)&(dir.b) + dir.b 如果真有人写出这种code,而且还能很好的运行。真是Write Unmaintainable Code 的高手。找到rolia一个漏洞,回复过的帖子也可以修改。
                  • 晕,让他看看type cast的章节,要不说了用处也不大
                • wk! i fu le you!
                • 我真服了你了!!
                  你先了解下int和float在系统中的存储方式吧。
                  建议你从C++入门看起,否则你写出这种程序去面试立即被毙了!

                  上面的问题,建议你改写成以下方式:

                  union {
                  float f;
                  int i;
                  char ch[4]; // 32位OS,如果64位OS写成 char ch[8]
                  } dir;

                  然后你给f赋不同的数,观察i和ch[]的变化
                  • C++入门?? too much for him. he needs to read "The C Programming Language"
          • 你这个SENIOR,居然教起小学生啦.让我教教你.以后你看见谁敢在系统级别的程序中用UNIION,直接毙掉,基本没有错.
            • 为什么
              • 你SEARCH一下MS的代码库.我相信没有.当然,MS经过多年的发展,估计收入了不少垃圾.
                • Win32 API中就有很多Union呀。
            • 补充一下:如果看到有人在系统中已经用了union,马上毙掉自己!
            • 放在这里,我等知音.
            • There are unions in definitions of IPv4 and IPv6 headers.
            • microsoft driver级别很多union。主要是为了省内村。
              typedef struct _IO_STACK_LOCATION {
                UCHAR  MajorFunction;
                UCHAR  MinorFunction;
                UCHAR  Flags;
                UCHAR  Control;
                union {
                      //
                      // Parameters for IRP_MJ_CREATE 
                      //
                      struct {
                          PIO_SECURITY_CONTEXT SecurityContext;
                          ULONG Options;
                          USHORT POINTER_ALIGNMENT FileAttributes;
                          USHORT ShareAccess;
                          ULONG POINTER_ALIGNMENT EaLength;
                      } Create;
                      //
                      // Parameters for IRP_MJ_READ 
                      //
                      struct {
                          ULONG Length;
                          ULONG POINTER_ALIGNMENT Key;
                          LARGE_INTEGER ByteOffset;
                      } Read;
                      //
                      // Parameters for IRP_MJ_WRITE 
                      //
                      struct {
                          ULONG Length;
                          ULONG POINTER_ALIGNMENT Key;
                          LARGE_INTEGER ByteOffset;
                      } Write;
                      //
                      // Parameters for IRP_MJ_QUERY_INFORMATION 
                      //
                      struct {
                          ULONG Length;
                          FILE_INFORMATION_CLASS POINTER_ALIGNMENT FileInformationClass;
                      } QueryFile;
                      //
                      // Parameters for IRP_MJ_SET_INFORMATION 
                      //
                      struct {
                          ULONG Length;
                          FILE_INFORMATION_CLASS POINTER_ALIGNMENT FileInformationClass;
                          PFILE_OBJECT FileObject;
                          union {
                              struct {
                                  BOOLEAN ReplaceIfExists;
                                  BOOLEAN AdvanceOnly;
                              };
                              ULONG ClusterCount;
                              HANDLE DeleteHandle;
                          };
                      } SetFile;
                      //
                      // Parameters for IRP_MJ_QUERY_VOLUME_INFORMATION 
                      //
                      struct {
                          ULONG Length;
                          FS_INFORMATION_CLASS POINTER_ALIGNMENT FsInformationClass;
                      } QueryVolume;
                      //
                      // Parameters for IRP_MJ_DEVICE_CONTROL and IRP_MJ_INTERNAL_DEVICE_CONTROL 
                      //
                      struct {
                          ULONG OutputBufferLength;
                          ULONG POINTER_ALIGNMENT InputBufferLength;
                          ULONG POINTER_ALIGNMENT IoControlCode;
                          PVOID Type3InputBuffer;
                      } DeviceIoControl;
                      //
                      // Nonsystem service parameters.
                      //
                      // Parameters for IRP_MN_MOUNT_VOLUME 
                      //
                      struct {
                          PVOID DoNotUse1;
                          PDEVICE_OBJECT DeviceObject;
                      } MountVolume;
                      //
                      // Parameters for IRP_MN_VERIFY_VOLUME 
                      //
                      struct {
                          PVOID DoNotUse1;
                          PDEVICE_OBJECT DeviceObject;
                      } VerifyVolume;
                      //
                      // Parameters for Scsi using IRP_MJ_INTERNAL_DEVICE_CONTROL 
                      //
                      struct {
                          struct _SCSI_REQUEST_BLOCK *Srb;
                      } Scsi;
                      //
                      // Parameters for IRP_MN_QUERY_DEVICE_RELATIONS 
                      //
                      struct {
                          DEVICE_RELATION_TYPE Type;
                      } QueryDeviceRelations;
                      //
                      // Parameters for IRP_MN_QUERY_INTERFACE 
                      //
                      struct {
                          CONST GUID *InterfaceType;
                          USHORT Size;
                          USHORT Version;
                          PINTERFACE Interface;
                          PVOID InterfaceSpecificData;
                      } QueryInterface;
                      //
                      // Parameters for IRP_MN_QUERY_CAPABILITIES 
                      //
                      struct {
                          PDEVICE_CAPABILITIES Capabilities;
                      } DeviceCapabilities;
                      //
                      // Parameters for IRP_MN_FILTER_RESOURCE_REQUIREMENTS 
                      //
                      struct {
                          PIO_RESOURCE_REQUIREMENTS_LIST IoResourceRequirementList;
                      } FilterResourceRequirements;
                      //
                      // Parameters for IRP_MN_READ_CONFIG and IRP_MN_WRITE_CONFIG 
                      //
                      struct {
                          ULONG WhichSpace;
                          PVOID Buffer;
                          ULONG Offset;
                          ULONG POINTER_ALIGNMENT Length;
                      } ReadWriteConfig;
                      //
                      // Parameters for IRP_MN_SET_LOCK 
                      //
                      struct {
                          BOOLEAN Lock;
                      } SetLock;
                      //
                      // Parameters for IRP_MN_QUERY_ID 
                      //
                      struct {
                          BUS_QUERY_ID_TYPE IdType;
                      } QueryId;
                      //
                      // Parameters for IRP_MN_QUERY_DEVICE_TEXT 
                      //
                      struct {
                          DEVICE_TEXT_TYPE DeviceTextType;
                          LCID POINTER_ALIGNMENT LocaleId;
                      } QueryDeviceText;
                      //
                      // Parameters for IRP_MN_DEVICE_USAGE_NOTIFICATION 
                      //
                      struct {
                          BOOLEAN InPath;
                          BOOLEAN Reserved[3];
                          DEVICE_USAGE_NOTIFICATION_TYPE POINTER_ALIGNMENT Type;
                      } UsageNotification;
                      //
                      // Parameters for IRP_MN_WAIT_WAKE 
                      //
                      struct {
                          SYSTEM_POWER_STATE PowerState;
                      } WaitWake;
                      //
                      // Parameter for IRP_MN_POWER_SEQUENCE 
                      //
                      struct {
                          PPOWER_SEQUENCE PowerSequence;
                      } PowerSequence;
                      //
                      // Parameters for IRP_MN_SET_POWER and IRP_MN_QUERY_POWER 
                      //
                      struct {
                          ULONG SystemContext;
                          POWER_STATE_TYPE POINTER_ALIGNMENT Type;
                          POWER_STATE POINTER_ALIGNMENT State;
                          POWER_ACTION POINTER_ALIGNMENT ShutdownType;
                      } Power;
                      //
                      // Parameters for IRP_MN_START_DEVICE 
                      //
                      struct {
                          PCM_RESOURCE_LIST AllocatedResources;
                          PCM_RESOURCE_LIST AllocatedResourcesTranslated;
                      } StartDevice;
                      //
                      // Parameters for WMI Minor IRPs 
                      //
                      struct {
                          ULONG_PTR ProviderId;
                          PVOID DataPath;
                          ULONG BufferSize;
                          PVOID Buffer;
                      } WMI;
                      //
                      // Others - driver-specific
                      //
                      struct {
                          PVOID Argument1;
                          PVOID Argument2;
                          PVOID Argument3;
                          PVOID Argument4;
                      } Others;
                  } Parameters;
                PDEVICE_OBJECT  DeviceObject;
                PFILE_OBJECT  FileObject;
                .
                .
              } IO_STACK_LOCATION, *PIO_STACK_LOCATION;
              
            • 楼上的.这个UNION很多陷阱的.估计大伙还没有遇到.特别是在不同PROCESSOR之间用的时候. 我昨晚说话没有详细考虑,在接口的地方,用一下也是可以的.虽然看着很让我讨厌,很难跟踪维护.