Announcement

Collapse
No announcement yet.

C Conversion

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • C Conversion

    Hi Guys,

    Any of you C gurus know how to convert the following code to PB, unfortunately,
    it's not included in the latest PB header files and it's a bit beyond me!

    Here's the C code (from Winsock2.h - latest SDK)...

    Code:
    /*
     * Portable socket structure (RFC 2553).
     */
    
    /*
     * Desired design of maximum size and alignment.
     * These are implementation specific.
     */
    #define _SS_MAXSIZE 128                  // Maximum size.
    #define _SS_ALIGNSIZE (sizeof(__int64))  // Desired alignment.
    
    /*
     * Definitions used for sockaddr_storage structure paddings design.
     */
    #define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof (short))
    #define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof (short) + _SS_PAD1SIZE \
                                                        + _SS_ALIGNSIZE))
    
    struct sockaddr_storage {
        short ss_family;               // Address family.
        char __ss_pad1[_SS_PAD1SIZE];  // 6 byte pad, this is to make
                                       // implementation specific pad up to
                                       // alignment field that follows explicit
                                       // in the data structure.
        __int64 __ss_align;            // Field to force desired structure.
        char __ss_pad2[_SS_PAD2SIZE];  // 112 byte pad to achieve desired size;
                                       // _SS_MAXSIZE value minus size of
                                       // ss_family, __ss_pad1, and
                                       // __ss_align fields is 112.
    };
    Thanks for any help,

    Regards,

    ------------------
    Kev G Peel
    KGP Software, Bridgwater, UK.
    mailto:[email protected][email protected]</A>
    kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

  • #2
    Kev,

    Check out www.vbthunder.com. There's a really good article on how to convert C
    declare and such to basic.

    Russ Srole

    ------------------
    "There are two novels that can change a bookish fourteen-year old's life: The Lord of the Rings and Atlas Shrugged. One is a childish fantasy that often engenders a lifelong obsession with its unbelievable heroes, leading to an emotionally stunted, socially crippled adulthood, unable to deal with the real world. The other, of course, involves orcs." - John Rogers

    Comment


    • #3
      Kev,

      The "__int64" translates to a PB "QUAD", the rest is pretty
      much straight forward. The "\" indicates a line is
      continuing similar to PB's " _".

      Cheers,
      Cecil

      ------------------

      Comment


      • #4
        Thanks for the tips guys!

        ------------------
        Kev G Peel
        KGP Software, Bridgwater, UK.
        mailto:[email protected][email protected]</A>
        kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

        Comment


        • #5
          In case you may have overlooked it, the new 6.1x documentation section "Comparative Data Types" contains this kind of information...

          ------------------
          Lance
          PowerBASIC Support
          mailto:[email protected][email protected]</A>
          Lance
          mailto:[email protected]

          Comment


          • #6
            I'm having a bit of trouble converting this also...

            Code:
             
            #define WSAID_ACCEPTEX   0xb5367df1,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
             
            #define WSAID_GETACCEPTEXSOCKADDRS {0xb5367df2,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
            In the code, these "constants" are assigned to a GUID structure like this...

            Code:
                        GUID        guidAcceptEx = WSAID_ACCEPTEX,
                                    guidGetAcceptExSockaddrs = WSAID_GETACCEPTEXSOCKADDRS;
            The trouble is how to assign in PB?


            And another conversion prob (the last!) is what "PendingAccepts" in the
            following should be converted to, it is used like this "so.PendingAccepts[i]" ?

            Code:
            typedef struct _SOCKET_OBJ
            {
                SOCKET               s;              // Socket handle
            
                int                  af,             // Address family of socket (AF_INET, AF_INET6)
                                     bClosing;       // Is the socket closing?
            
                volatile LONG        OutstandingOps; // Number of outstanding overlapped ops on 
                                                     //    socket
            
                BUFFER_OBJ         **PendingAccepts; // Pending AcceptEx buffers 
                                                     //   (used for listening sockets only)
            
                ULONG                LastSendIssued, // Last sequence number sent
                                     IoCountIssued;  // Next sequence number assigned to receives
                BUFFER_OBJ          *OutOfOrderSends;// List of send buffers that completed out of order
            
                // Pointers to Microsoft specific extensions. These are used by listening
                //   sockets only
                LPFN_ACCEPTEX        lpfnAcceptEx;
                LPFN_GETACCEPTEXSOCKADDRS lpfnGetAcceptExSockaddrs;
            
                CRITICAL_SECTION     SockCritSec;    // Protect access to this structure
            
                struct _SOCKET_OBJ  *next;
            } SOCKET_OBJ;

            TIA,

            ------------------
            Kev G Peel
            KGP Software, Bridgwater, UK.
            mailto:[email protected][email protected]</A>

            [This message has been edited by Kev Peel (edited March 26, 2002).]
            kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

            Comment


            • #7
              A GUID is a 16-byte binary value (128 bits), but can also take the format of a human-readable string. If you've looked through the registry, you may have seen some of these using the format "{00000000-0000-0000-0000-000000000000}"

              In any case, the 16-byte [binary] version would make sense against your code above, since those equates appear to define 16 hex bytes...

              Now, I'm relatively sure the orientation/order of the bytes is correct in the following code, but you'll have to try it. Note the way that the first 10 sets of digit-pairs get reversed in blocks...
              Code:
              $sWSAID_ACCEPTEX = "f17d36b5accbcf1195ca00805f48a192" ' template only
              FUNCTION PBMAIN
                  DIM WSAID_ACCEPTEX AS STRING * 16  ' the actual GUID string to be used in the code
                  FOR x& = 1 TO 16
                      MID$(WSAID_ACCEPTEX, x&, 1) = CHR$(VAL("&H0" + MID$($sWSAID_ACCEPTEX, x& * 2 - 1, 2)))
                  NEXT x&
                  ' WSAID_ACCEPTEX will (hopefully) now contain a 16-byte GUID string.
              END FUNCTION
              All going well, this produces a binary equivalant of the human-readable GUID string "{B5367DF1-CBAC-11CF-95CA-00805F48A192}"

              Now I've explained the hard way, you could also define it this way:
              Code:
              $WSAID_ACCEPTEX = CHR$(&Hf1, &H7d, &H36, &Hb5, &Hac, &Hcb, &Hcf, &H11, _
                                     &H95, &Hca, &H00, &H80, &H5f, &H48, &Ha1, &H92)
              I hope this helps!


              ------------------
              Lance
              PowerBASIC Support
              mailto:[email protected][email protected]</A>
              Lance
              mailto:[email protected]

              Comment


              • #8
                PendingAccepts is a pointer to a pointer to a BUFFER_OBJ value.
                Consider it a DWORD PTR and figure that so.PendingAccepts[ i] is a
                pointer to a BUFFER_OBJ value.


                ------------------
                Tom Hanlin
                PowerBASIC Staff

                Comment


                • #9
                  Kev,

                  Don't forget that last structure member:

                  struct _SOCKET_OBJ *next;

                  This structure is constructed to function in a
                  "Linked List", ie you always have a pointer to the
                  next structure embedded in the current structure.
                  Normally, these structures have a head pointer also.

                  Cheers,
                  Cecil

                  ------------------

                  Comment


                  • #10
                    Thanks Lance, Tom & Cecil, brilliant

                    ------------------
                    Kev G Peel
                    KGP Software, Bridgwater, UK.
                    mailto:[email protected][email protected]</A>

                    [This message has been edited by Kev Peel (edited March 27, 2002).]
                    kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

                    Comment

                    Working...
                    X