none
Initialising a struct RRS feed

  • Question

  • In non-driver code I can initialise a struct using the following assignments. But this is not allowed when used in a driver. Is there anything I can do to make this allowable, or is it a non-circumventible limitation? Thanks.

    #pragma pack(1)
    typedef struct
    {
      UCHAR  Jmp [2]         = { 0xFF, 0x25 };
      UINT32 OperandLocation = 0;
      UINT64 Address;
    } JMP;
    

    Tuesday, November 6, 2018 1:44 PM

Answers

  • I used it for its elegance. Was caught out by MS being incompatible with itself. 

    Hmm hmm. This kind of declaration of a struct is C++.

    struct
    {
      UCHAR  Jmp [2]         = { 0xFF, 0x25 };

    C compiler (and older C++) do not understand this.

    -- pa 

    • Marked as answer by Dev10110110 Thursday, November 8, 2018 1:21 PM
    Wednesday, November 7, 2018 7:34 PM
  • You should not be trying to initialize in a typedef in general, this is highly non-standard.   Instead try:

    #pragma pack(1)
    typedef struct
    {
      UCHAR  Jmp [2];
      UINT32 OperandLocation;
      UINT64 Address;
    } JMP;
    
    JMP j = {{ 0xFF, 0x25 }, 0, 0 };


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    • Marked as answer by Dev10110110 Tuesday, November 6, 2018 10:57 PM
    Tuesday, November 6, 2018 7:18 PM
  • I used it for its elegance. Was caught out by MS being incompatible with itself. 

    Hmm hmm. This kind of declaration of a struct is C++.

    struct
    {
      UCHAR  Jmp [2]         = { 0xFF, 0x25 };

    C compiler (and older C++) do not understand this.

    -- pa 

    Yes, I was surprised by MS not being compatible with itself. It did not occur to me MS C++ was not compatible with Driver MS C/C++. The driver project allowed me to add existing .cpp file to it, and so I did.

    I am curious, if you care to answer, is driver project purely in C and not C++? In any case I don't really do C++. I intermingled my C code with C++ because it worked and at places it was elegant. I found the typedef assignment to be very elegant indeed.


    I just compiled this code in a driver project by VS2015+WDK 10.0.10586. Compiled successfully in .cpp file but failed in .c file.
    • Marked as answer by Dev10110110 Thursday, November 8, 2018 12:53 PM
    Thursday, November 8, 2018 8:50 AM

All replies

  • You should not be trying to initialize in a typedef in general, this is highly non-standard.   Instead try:

    #pragma pack(1)
    typedef struct
    {
      UCHAR  Jmp [2];
      UINT32 OperandLocation;
      UINT64 Address;
    } JMP;
    
    JMP j = {{ 0xFF, 0x25 }, 0, 0 };


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    • Marked as answer by Dev10110110 Tuesday, November 6, 2018 10:57 PM
    Tuesday, November 6, 2018 7:18 PM
  • I used it for its elegance. Was caught out by MS being incompatible with itself. I had some existing algorithmic code that I wanted to re-use. Not changing the typedef means it will save me code changes and re-validation all over the place. But, it's not possible. Thanks for your confirmation.

    • Edited by Dev10110110 Tuesday, November 6, 2018 10:59 PM
    Tuesday, November 6, 2018 10:57 PM
  • I used it for its elegance. Was caught out by MS being incompatible with itself. 

    Hmm hmm. This kind of declaration of a struct is C++.

    struct
    {
      UCHAR  Jmp [2]         = { 0xFF, 0x25 };

    C compiler (and older C++) do not understand this.

    -- pa 

    • Marked as answer by Dev10110110 Thursday, November 8, 2018 1:21 PM
    Wednesday, November 7, 2018 7:34 PM
  • I used it for its elegance. Was caught out by MS being incompatible with itself. 

    Hmm hmm. This kind of declaration of a struct is C++.

    struct
    {
      UCHAR  Jmp [2]         = { 0xFF, 0x25 };

    C compiler (and older C++) do not understand this.

    -- pa 

    Yes, I was surprised by MS not being compatible with itself. It did not occur to me MS C++ was not compatible with Driver MS C/C++. The driver project allowed me to add existing .cpp file to it, and so I did.

    I am curious, if you care to answer, is driver project purely in C and not C++? In any case I don't really do C++. I intermingled my C code with C++ because it worked and at places it was elegant. I found the typedef assignment to be very elegant indeed.


    • Edited by Dev10110110 Wednesday, November 7, 2018 10:25 PM
    Wednesday, November 7, 2018 10:22 PM
  • Whether or not to use C++ in kernel drivers is a dangerous flame-producing question.

    It looks like Microsoft has mostly enabled use of C++ in drivers, the compiler and WDK tools support it. But one should really understand what he is doing.

    Either this specific form of initializer is not supported in kernel mode, of your VS & WDK version have older C++ compiler which does not support it at all.

    Good luck.

    -- pa

    Thursday, November 8, 2018 12:30 AM
  • The kernel crowd around here is rather small. I doubt it is capable of catching fire.

    My VS2017 is completely up-to-date, unless the community version is in some way hobbled. The SDK is no older than 9 months. So, it looks to me MS is definitely not compatible with itself. When I asked the original question, I was hoping for some kind of switch that magically makes MS compatible with itself. I didn't think I was asking for a miracle.

    I really know what I am doing even though I ask simple questions. When I approach something new, I generally just grab a working sample and push that to the limit. This way, it allows me to get into anything quickly and it immediately works. The drawback is that I don't have, and often don't need, the context information the sample is based on. Hence, from time to time, I have to ask simple questions to fill in the gaps.






    • Edited by Dev10110110 Thursday, November 8, 2018 3:02 AM
    Thursday, November 8, 2018 2:57 AM
  • I used it for its elegance. Was caught out by MS being incompatible with itself. 

    Hmm hmm. This kind of declaration of a struct is C++.

    struct
    {
      UCHAR  Jmp [2]         = { 0xFF, 0x25 };

    C compiler (and older C++) do not understand this.

    -- pa 

    Yes, I was surprised by MS not being compatible with itself. It did not occur to me MS C++ was not compatible with Driver MS C/C++. The driver project allowed me to add existing .cpp file to it, and so I did.

    I am curious, if you care to answer, is driver project purely in C and not C++? In any case I don't really do C++. I intermingled my C code with C++ because it worked and at places it was elegant. I found the typedef assignment to be very elegant indeed.


    I just compiled this code in a driver project by VS2015+WDK 10.0.10586. Compiled successfully in .cpp file but failed in .c file.
    • Marked as answer by Dev10110110 Thursday, November 8, 2018 12:53 PM
    Thursday, November 8, 2018 8:50 AM

  • I just compiled this code in a driver project by VS2015+WDK 10.0.10586. Compiled successfully in .cpp file but failed in .c file.

    I had main in .c, this definition in .h, and its associated source code in .cpp. This must be the reason for the failure. Thanks for your testing. It may well have solved the mystery.

    Even if this fixes the problem, I will not use this approach any longer. Firstly, C++ is easier to debug compared to C because function parameter types are easier to discern. Secondly, C++ probably has overheads. So sticking to pure C will give me greater code security and efficiency.

    Still, it's good to know the reason for the failure. Many thanks.







    • Edited by Dev10110110 Thursday, November 8, 2018 1:19 PM
    Thursday, November 8, 2018 12:36 PM