• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

More C type questions...

Hi everyone...

how do I translate the following to Delphi 4?

#define TCOMPfromTypeLevelMemory(t,l,m)           \
            (((m) << tcompSHIFT_QUANTUM_MEM  ) |  \
             ((l) << tcompSHIFT_QUANTUM_LEVEL) |  \
             ( t                             ))

typedef struct {
    FDIDECRYPTTYPE    fdidt;            // Command type (selects union below)
    void FAR         *pvUser;           // Decryption context
    union {
        struct {                        // fdidtNEW_CABINET
            void FAR *pHeaderReserve;   // RESERVE section from CFHEADER
            USHORT    cbHeaderReserve;  // Size of pHeaderReserve
            USHORT    setID;            // Cabinet set ID
            int       iCabinet;         // Cabinet number in set (0 based)
        } cabinet;

        struct {                        // fdidtNEW_FOLDER
            void FAR *pFolderReserve;   // RESERVE section from CFFOLDER
            USHORT    cbFolderReserve;  // Size of pFolderReserve
            USHORT    iFolder;          // Folder number in cabinet (0 based)
        } folder;

        struct {                        // fdidtDECRYPT
            void FAR *pDataReserve;     // RESERVE section from CFDATA
            USHORT    cbDataReserve;    // Size of pDataReserve
            void FAR *pbData;           // Data buffer
            USHORT    cbData;           // Size of data buffer
            BOOL      fSplit;           // TRUE if this is a split data block
            USHORT    cbPartial;        // 0 if this is not a split block, or
                                        //  the first piece of a split block;
                                        // Greater than 0 if this is the
                                        //  second piece of a split block.
        } decrypt;
} FDIDECRYPT; /* fdid */

I don't have a problem here with the data types, but with the keyword "union". Do I see it right, that I have to declare the structs (Records) inside the union in my type declarations as normal records and then do something like:

TUnionRec1 = Record

TUnionRec2 = Record

MyRecord = Record
  UnionRec1 : TUnionRec1;
  UnionRec2 : TUnionRec2;

Would this be correct? If sombody know where I could find a allready finished translation of the FDI.H file (From the MS CABS SDK), it wouldn't mind to getting my hands on it ;-)Thanks, Jonny...
  • 2
1 Solution
A Union means that the 'variants' within it are stored in the same memory. In your case it would look like this:

MyRecord = record
  case integer of
    0 : UnionRec1 : TUnionRec1;
    1 : UnioneRec2 : TUnionRec2;

The help on unions is pretty good in Delphi.

I don't know if there is an converted FDI.H file, perhaps you could look at the Jedi Project home page (whose URL escapes me at the moment...)



jonnyfiveAuthor Commented:
Hi Raymund...

Thanks for your answer... could you tell me how I convert the first Part of what I posted?

#define TCOMPfromTypeLevelMemory(t,l,m)           \
            (((m) << tcompSHIFT_QUANTUM_MEM  ) |  \
             ((l) << tcompSHIFT_QUANTUM_LEVEL) |  \
             ( t                             ))

And do I see it right, that I would have to use it then in this particular case like
Case PVUSER of ... and what does the FAR statment mean? :-)

The closest translation to this is:

Function TCOMPfromTypeLevelMemory(t,l,m : Integer) : Integer;

  Result := (t SHL tcompSHIFT_QUANTUM_MEM) OR
            (l SHL tcompSHIFT_QUANTUM_LEVEL) OR t;

Though, as you can see, you could just inline this (which is the ultimate effect in the C code).

The void FAR *pvUser is defining a pvUser as a pointer to void (C way of saying a generic pointer). It is not related to the union which has fdidt as the tag.

FAR is redundant in Win32, basically it meant a pointer which could reference a location outside of the current 64k memory segment (in 16-bit windows land).




Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now