Announcement

Collapse
No announcement yet.

Help in C DECL to PB declartion...

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

  • Help in C DECL to PB declartion...

    LPSTR __cdecl e1Head( LPSTR, LPSTR, LPSTR, int*, int );

    This is the export in C, what would be the equivilant, to access this in pb (this is the export from a c dll, I want to access this dll function in pb) we'll label this, header.dll

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

  • #2
    Originally posted by Matthew _Grace:
    LPSTR __cdecl e1Head( LPSTR, LPSTR, LPSTR, int*, int );
    • The first LPSTR means it returns a pointer to an ASCIIZ buffer, so the PB declaration can specify a DWORD return type, and you can cast that to an ASCIIZ PTR variable as required.
    • __cdecl means the function uses CDECL calling convention.
    • The LPSTR parameters means you can pass BYREF ASCIIZ parameters.
    • The int* syntax is a little unusual... more often you will see *int which is a BYREF LONG in PB (I'll make the assumption that int* and *int are the same).
    • Finally, the last int is a BYVAL LONG pass.

    Putting this all together we get:
    Code:
    DECLARE FUNCTION e1Head CDECL LIB "libname.dll" ALIAS "e1Head" ( _
      param1 AS ASCIIZ, param2 AS ASCIIZ, param3 AS ASCIIZ, num1 AS LONG, _
      BYVAL num2 AS LONG) AS DWORD
    I hope this helps!

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

    Comment


    • #3
      Thank you sooo much, I was so stuck on dword, compiler would error doing function() as asciiz ptr, and understandably so!, thanks so much lance!

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

      Comment


      • #4
        (The int* syntax is a little unusual...
        more often you will see *int which is a BYREF LONG in PB
        (I'll make the assumption that int* and *int are the same)

        The int* is in C/C++ an integer pointer or more commonly
        called a pointer to an integer. Technically, it should be
        a DWORD in PB. Although a LONG would probably suffice, the
        newer OS's will eventually bite you down the road!!

        Hope this clarifys the C declare.

        Cheers,
        Cecil

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

        Comment


        • #5
          Hmmm...

          An "int" is a signed [32-bit] integer (as opposed to a "UINT", "UINT32", "unsigned int", etc), so a BYREF LONG would be the best translation.

          Based on your comments, yes, a BYVAL DWORD would also work, provided the DWORD value being passed was the address of the target LONG integer.

          IMHO, a BYREF LONG is likely to make the DECLARE easier to use, than would an implied pointer via a BYVAL DWORD.

          I don't see any reason why the newer OS's would have any effect here, unless you expect PowerBASIC to closely follow the path of .NET (where a Long Integer is 64-bits wide!). I can see why some VB'ers have coined the phrase VB.FRED.


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

          Comment


          • #6
            (Thanks for the user name help Lance)

            my question is this...

            LPSTR __cdecl GetE1( LPSTR, Boolean, int*, int );

            I understand all to be the same, with the exception of the boolean, I've tried byte and it just doens't work... where could I be going wrong?

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

            Comment


            • #7
              Mathew,

              a BOOLEAN in C/C++ is normally a 4 Byte UNSIGNED INTEGER,
              for PB a DWORD.
              FALSE = 0
              TRUE = NOT FALSE = 1

              Regards,

              Torsten

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

              Comment


              • #8
                "BOOLEAN" and "Boolean" are usually not the same thing...

                That is, it is difficult to be 100% sure of the precise data type without inspecting the original code to find where it is defined (since "Boolean" is not an intrinsic type).

                Reading from the PB/Win and PB/CC help file under "Comparative Data Types" we find:
                bool (C++) unsigned 8-bit BYTE (2)
                ...
                BOOL signed 32-bit LONG
                boolean 8-bit, see BOOLEAN BYTE
                Boolean signed 16-bit INTEGER
                BOOLEAN 8-bit S/U BYTE
                ...
                (2) Denotes the closest matching data type, based on usage in modern compilers. In the case of the bool type, older MS C++ compilers may substitute a 32-bit unsigned integer (LONG).
                So your choices are BYTE, LONG, and INTEGER. My best guess would be INTEGER since the code is clearly using other SDK-defined types (LPSTR, etc).

                BTW, using a DWORD to store a boolean value is rarely (if ever) done in PB since a DWORD cannot store the value -1& (the value returned by ISTRUE/ISFALSE and other boolean expressions).

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

                Comment


                • #9
                  Hi Lance,

                  in most cases (all cases i have seen) a BOOLEAN in C/C++ is
                  represented by a 4 Byte unsigned intgeger UINT/DWORD. A boolean
                  expression is evaluated to 0 = False or 1 = True.

                  Many C/C++ code uses boolean arithmetic. Many Basic programmers,
                  who trying to convert C/C++ to Basic are falling deep if they
                  need to convert syntax like below.

                  Code:
                  p = 4 + (3 > 2)
                  
                  Basic p = 3
                  C/C++ p = 5
                  Regards,

                  Torsten



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

                  Comment


                  • #10
                    Agreed... in these cases you really need to understand the context of the original code, which can be difficult if it is of the obfuscated variety!

                    The important point (for everyone) to remember is that C and C++ are case sensitive, and Matthews code uses "Boolean" which is neither "BOOLEAN" nor "boolean".

                    However, "Boolean" is a defined in the SDK as a signed 16-bit var (ie, a PowerBASIC INTEGER). Since Matthews code appears to use other SDK-defined types, it makes sense (to me!) that "Boolean" is of the SDK-defined variety.

                    Of course, this is as much a guess as any, so it will remain to see which of the 3 possible data types works best.

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

                    Comment


                    • #11
                      a BOOLEAN in C/C++ is represented by a 4 Byte unsigned intgeger
                      UINT/DWORD. A boolean expression is evaluated to 0 = False or 1 = True.
                      The common Windows SDK type, BOOLEAN, is actually a LONG (4-byte signed
                      value). As noted, "1" is the "most true" value for C, whereas "-1" is
                      the "most true" value for BASIC. With C or with BASIC, though, any non-zero
                      value is considered "true".

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

                      Comment


                      • #12
                        Lance,

                        MS SDK differs in many cases from standard C/C++. This reads like
                        <C++ specific>
                        bool can be true or false
                        ...
                        <END C++ specific>

                        <Microsoft Specific>
                        ...
                        VC4.2-> bool is 4 bytes
                        VC5 or above-> bool is 1 byte
                        <END Microsoft Specific>

                        Mathew's can be MS VC++ code, but could also be G++ code.
                        "Boolean" should be defined in a Header file, so a search for
                        "#typedef Boolean" should give clearness.

                        regards,

                        Torsten


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


                        [This message has been edited by Torsten Rienow (edited October 18, 2002).]

                        Comment

                        Working...
                        X