Announcement

Collapse
No announcement yet.

C Declaration to PB

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

  • C Declaration to PB

    My main question is with how to declare the GMW_GetLicenseInfo function in PB, and how to call it since the parameter is a pointer to a User Defined TYPE. I think I'm missing something here. Also, am I declaring the asciiz's with the correct length?

    Thanks a lot guys!
    Scott

    Code:
    C Code:
    -------
    typedef struct
       {
       char    Licensee[60];        
       char    LicNo[20];           
       char    SiteName[20];        
       long    LicUsers;             
       long    SQLUsers;             
       long    GSSites;              
       long    isDemo;                
       long    isServerLic;           
       long    isRemoteLic;           
       long    isUSALicense;           
       long    DLLVersion;		
       long    Reserved1;
       long    Reserved2;
       long    Reserved[100];
    
    } GMW_LicInfo;
    
    
    int _stdcall GMW_GetLicenseInfo( GMW_LicInfo *pLic );
    
    
    PB Code
    -------
    TYPE GMLicInfo
        Licensee       AS ASCIIZ * 60 ' Should be 61?
        LicNo          AS ASCIIZ * 20 ' Should be 21?  
        SiteName       AS ASCIIZ * 20 ' Should be 21? 
        LicUsers       AS LONG          
        SQLUsers       AS LONG          
        GSSites        AS LONG          
        IsDemo         AS LONG          
        IsServerLic    AS LONG          
        IsRemoteLic    AS LONG          
        IsUSALicense   AS LONG          
        DLLVersion     AS LONG          
        Reserved1      AS LONG
        Reserved2      AS LONG
        sReserved      AS ASCIIZ * 100 ' Should be 101?
    END TYPE
    
    
    DECLARE FUNCTION GMW_GetLicenseInfo LIB "GM6S32.dll" _
                     ALIAS "GMW_GetLicenseInfo" _
                     (BYREF udtLicense AS GMLicInfo) AS LONG
    
    
    FUNCTION PBMAIN()
    
    
        LOCAL lResult as long
        LOCAL udtGMLicInfo as GMLicInfo
    
    
        lResult = GMW_GetLicenseInfo (udtGMLicInfo)
        MSGBOX STR$(lResult) & ":" & STR$(udtGMLicInfo.DLLVersion)
    
    
    END FUNCTION

    ------------------
    Scott Wolfington
    http://www.boogietools.com




    [This message has been edited by Scott Wolfington (edited March 05, 2003).]
    Scott Wolfington
    [url="http://www.boogietools.com"]http://www.boogietools.com[/url]

  • #2
    Originally posted by Scott Wolfington:
    Licensee AS ASCIIZ * 60 ' Should be 61?
    From the PB Help File:

    DIM MyStr AS ASCIIZ * 40
    DIM MyStr AS ASCIZ * 40
    This creates a 40 byte ASCIIZ string named MyStr. The number of characters
    you can store in an ASCIIZ string is always one less than the defined length
    of the string. The last character is used to hold the nul-terminator.
    Therefore, a 40 byte ASCIIZ string can hold up to 39 characters. If you need
    40 usable bytes, then DIM the string to have a 41 byte length. It is on this
    basis that ASCIIZ strings should be defined with a length of at least two
    bytes.
    Same as in C. You always tell the byte lenght, not the usable lenght.

    Your code snippet looks quite correct.

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

    EDITED found the problem

    The last line of your UDT should be:
    Reserved(100) AS LONG

    since a LONG is 4 bytes!



    [This message has been edited by Lothar Pink (edited February 28, 2003).]

    Comment


    • #3
      With Reserved(100) AS LONG you are declaring an array with 101 elements (0 TO 100), not 100. You must use Reserved(99) or Reserved(1 TO 100).


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

      Comment


      • #4
        Thanks guys. This helps a ton. I actually made a mistake in the C declaration for Reserved. That should be a char, not a long.

        I'll play around with this some more.

        Cheers,
        Scott

        ------------------
        Scott Wolfington
        http://www.boogietools.com


        [This message has been edited by Scott Wolfington (edited March 05, 2003).]
        Scott Wolfington
        [url="http://www.boogietools.com"]http://www.boogietools.com[/url]

        Comment


        • #5
          actually, i'd do this ...

          TYPE GMLicInfo
          Licensee AS string * 60
          LicNo AS string * 20
          ... etc
          END TYPE

          unless you're 100% positive that the other software is zterminating their strings.
          if they are not, then you'll lose the last character if their string takes up the whole block.

          --Don

          ------------------
          Don Dickinson
          Author of ddoc Print and Preview
          A dll-based print-preview engine for windows

          www.greatwebdivide.com
          Don Dickinson
          www.greatwebdivide.com

          Comment


          • #6
            Thanks Don. I was under the impression that a C char[xx] variable had to convert to PB as an ASCIIZ. Is this not the case for all chars, or is this just for char*? I know a single byte C char becomes a BYTE in PB.

            Thanks,
            Scott


            ------------------
            Scott Wolfington
            http://www.boogietools.com


            [This message has been edited by Scott Wolfington (edited March 05, 2003).]
            Scott Wolfington
            [url="http://www.boogietools.com"]http://www.boogietools.com[/url]

            Comment


            • #7
              I think I just answered my own question... From PB help file:

              "char foo[32]" translates to DIM foo(0 TO 31) AS BYTE, or DIM foo AS STRING * 32, depending on the context of the code.

              So it seems the ASCIIZ would only be used when there is a pointer involved. As in:

              long fnc_fooBar (char* szMoreFooBar);



              ------------------
              Scott Wolfington
              http://www.boogietools.com
              Scott Wolfington
              [url="http://www.boogietools.com"]http://www.boogietools.com[/url]

              Comment


              • #8
                Originally posted by JOSE ROCA:
                With Reserved(100) AS LONG you are declaring an array with 101 elements (0 TO 100), not 100. You must use Reserved(99) or Reserved(1 TO 100).
                Jose, are you saying that a C array is 1's based instead of 0 (zero) based?




                ------------------
                Scott Wolfington
                http://www.boogietools.com
                Scott Wolfington
                [url="http://www.boogietools.com"]http://www.boogietools.com[/url]

                Comment


                • #9
                  No. I'm saying --if I'm not wrong-- that long Reserved[100]; indicates an array of 100 longs, and the index goes from 0 to 99. But in PB, DIM Reserved(100) AS LONG, dims an array with 101 elements, from 0 TO 100! In those cases, I prefer to use the syntax DIM Reserved(1 TO 100) because is more clear.


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

                  Comment


                  • #10
                    The best translation for "char Licensee[60]" depends very much on
                    how it's used. The struct itself is not enough to tell you whether
                    a fixed-length STRING or ASCIIZ value would be better here. These
                    look like text values, though, so they are quite likely to be the
                    typical $NUL-terminated C strings: ASCIIZ in PowerBASIC.

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

                    Comment

                    Working...
                    X