Announcement

Collapse
No announcement yet.

how to pass a pointer to a null terminated string?

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

  • how to pass a pointer to a null terminated string?

    I need to pass a pointer to a null terminated string to a dll.
    ASCIIZ*256 AS POINTER won't work, also tried AS STRING POINTER
    but it don't work either.
    i am trying to use GMP dll. ( http://www.cs.nyu.edu/exact/core/gmp/ )

    ------------------
    as the salmon fish is compelled to go back to it's birthplace to spawn, so comes a day when man is compelled to go back to it's source.. GOD

  • #2
    Dynamic strings use Byval StrPtr( a$ )
    Fixed strings and asciiz use Byval VarPtr( az )


    ------------------
    http://www.hellobasic.com
    Freeware and commercial tools for PowerBASIC
    PBSoft - Netherlands
    hellobasic

    Comment


    • #3
      Johan --

      Are you asking...

      1) How to create an ASCIIZ variable

      2) How to write a DECLARE statement for a function (in a DLL) which has an ASCIIZ parameter

      3) How to call a function which has already been DECLAREd?

      You will need to do all three things. Which one(s) are you not clear about?

      -- Eric

      ------------------
      Perfect Sync Development Tools
      Perfect Sync Web Site
      Contact Us: mailto:[email protected][email protected]</A>
      "Not my circus, not my monkeys."

      Comment


      • #4
        >I need to pass a pointer to a null terminated string to a dll.

        Code:
         DECLARE FUNCTION FOO LIB "MYDLL.DLL"  (sz AS ASCIIZ) AS LONG
        
        
        FUNCTION Caller() AS LONG
         LOCAL  szSomething AS ASCIIZ * %Required_size
        
          szSomething  ="Something"
          CALL          Foo (szSomething)
        
        END FUNCTION
        FWIW: anytime you have either a DECLARE or a PROCEDURE header with a parameter of " X AS ASCIIZ [* length]" the compiler will generate code at point of call to pass the address of the string data, or error out the compile if you use the wrong data type (Error 480).



        [This message has been edited by Michael Mattias (edited November 01, 2004).]
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          thanks guys, but still have problems, here's what i have.

          Code:
                TYPE MPF              ' multiprecision floating point
                   mp_prec AS LONG
                   mp_size AS LONG
                   mp_expt AS LONG
                   mp_limb AS LONG
                END TYPE
          
          
                DECLARE SUB cMPFinit CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_init" (X AS MPF,prec AS LONG)
                DECLARE SUB cMPFclear CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_clear" (X AS MPF)
                DECLARE SUB cMPFsprintf CDECL LIB "libgmp-3.dll" ALIAS "__gmp_sprintf" (sz AS ASCIIZ) AS LONG,(fmt AS ASCIIZ) AS LONG,X AS MPF)
                DECLARE SUB cMPFset_str CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_set_str" (X AS MPF,(sz AS ASCIIZ) AS LONG, b AS LONG)
                DECLARE SUB cMPFsqrt CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_sqrt" (Y AS MPF,X AS MPF)
          
          
          FUNCTION PBMAIN () AS LONG
              DIM x AS MPF
              DIM y AS MPF
              DIM a AS ASCIIZ*63999
              DIM f AS ASCIIZ*256
              a="2"
              f="%17.10Fe"
              CALL cMPFinit(x,512)
              CALL cMPFinit(y,512)
              CALL cMPFset_str(x,a,10)
              a=SPACE$(100)
              CALL cMPFsqrt(y,x)
              CALL cMPFsprintf(f,a,y)
              CALL cMPFclear(x)
              CALL cMPFclear(y)
              MSGBOX a
          
          END FUNCTION
          ------------------


          [This message has been edited by Johan Klassen (edited November 01, 2004).]
          as the salmon fish is compelled to go back to it's birthplace to spawn, so comes a day when man is compelled to go back to it's source.. GOD

          Comment


          • #6
            Those DECLAREs won't (shouldn't, anyway) compile. You have extra parens around individual parameters.

            And this this one...
            Code:
            DECLARE SUB cMPFset_str CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_set_str" (X AS MPF,(sz AS ASCIIZ) AS LONG, b AS LONG)
            ..has a parameter of

            "(sz AS ASCIIZ) AS LONG"

            .. which is totally from somewhere I've never been. Which is it, ASCIIZ or LONG? Can't be both.

            Since you've obviously put forth some effort, I don't think anyone will mind if you post the "C" declarations and ask for a little help converting.



            [This message has been edited by Michael Mattias (edited November 01, 2004).]
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              this was my first try.

              Code:
              #COMPILE EXE
                    TYPE MPF              ' multiprecision floating point
                       mp_prec AS LONG
                       mp_size AS LONG
                       mp_expt AS LONG
                       mp_limb AS LONG
                    END TYPE
              
                    DECLARE SUB cMPFinit CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_init" (X AS MPF,prec AS LONG)
                    DECLARE SUB cMPFclear CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_clear" (X AS MPF)
                    DECLARE SUB cMPFsprintf CDECL LIB "libgmp-3.dll" ALIAS "__gmp_sprintf" (s AS STRING,fmt AS STRING,X AS MPF)
                    DECLARE SUB cMPFset_str CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_set_str" (X AS MPF,s AS STRING, b AS LONG)
                    DECLARE SUB cMPFsqrt CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_sqrt" (Y AS MPF,X AS MPF)
              
              
              FUNCTION PBMAIN () AS LONG
                  DIM x AS MPF
                  DIM y AS MPF
                  DIM a AS STRING
                  DIM f AS STRING
                  a="2"
                  f="%17.10Fe"
                  CALL cMPFinit(x,512)
                  CALL cMPFinit(y,512)
                  CALL cMPFset_str(x,a,10)
                  a=SPACE$(20)
                  CALL cMPFsqrt(y,x)
                  CALL cMPFsprintf(a,f,y)
                  CALL cMPFclear(x)
                  CALL cMPFclear(y)
                  MSGBOX a
              
              END FUNCTION
              but the message box gives "%17.10Fe" when it should be " 1.4142135624e+00"

              my second try
              Code:
              #COMPILE EXE
                    TYPE MPF              ' multiprecision floating point
                       mp_prec AS LONG
                       mp_size AS LONG
                       mp_expt AS LONG
                       mp_limb AS LONG
                    END TYPE
              
                    DECLARE SUB cMPFinit CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_init" (X AS MPF,prec AS LONG)
                    DECLARE SUB cMPFclear CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_clear" (X AS MPF)
                    DECLARE SUB cMPFsprintf CDECL LIB "libgmp-3.dll" ALIAS "__gmp_sprintf" (s AS STRING POINTER,fmt AS STRING POINTER,X AS MPF)
                    DECLARE SUB cMPFset_str CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_set_str" (X AS MPF,s AS STRING POINTER, b AS LONG)
                    DECLARE SUB cMPFsqrt CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_sqrt" (Y AS MPF,X AS MPF)
              
              
              FUNCTION PBMAIN () AS LONG
                  DIM x AS MPF
                  DIM y AS MPF
                  DIM a AS STRING POINTER
                  DIM f AS STRING POINTER
                  @a="2"
                  @f="%17.10Fe"
                  CALL cMPFinit(x,512)
                  CALL cMPFinit(y,512)
                  CALL cMPFset_str(x,a,10)
                  @a=SPACE$(20)
                  CALL cMPFsqrt(y,x)
                  CALL cMPFsprintf(a,f,y)
                  CALL cMPFclear(x)
                  CALL cMPFclear(y)
                  MSGBOX @a
              
              END FUNCTION
              program crashes
              ------------------

              trhird try

              Code:
              #COMPILE EXE
                    TYPE MPF              ' multiprecision floating point
                       mp_prec AS LONG
                       mp_size AS LONG
                       mp_expt AS LONG
                       mp_limb AS LONG
                    END TYPE
              
              
                    DECLARE SUB cMPFinit CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_init" (X AS MPF,prec AS LONG)
                    DECLARE SUB cMPFclear CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_clear" (X AS MPF)
                    DECLARE SUB cMPFsprintf CDECL LIB "libgmp-3.dll" ALIAS "__gmp_sprintf" (s AS ASCIIZ PTR,fmt AS ASCIIZ PTR,X AS MPF)
                    DECLARE SUB cMPFset_str CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_set_str" ( X AS MPF,s AS ASCIIZ PTR,b AS LONG)
                    DECLARE SUB cMPFsqrt CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_sqrt" (Y AS MPF,X AS MPF)
              
              
              FUNCTION PBMAIN () AS LONG
                  DIM x AS MPF
                  DIM y AS MPF
                  DIM a AS ASCIIZ*2000
                  DIM f AS ASCIIZ*20
                  a="2"
                  f="%17.10Fe"
                  CALL cMPFinit(x,512)
                  CALL cMPFinit(y,512)
                  CALL cMPFset_str(x,VARPTR(a),10)
                  a=SPACE$(20)
                  CALL cMPFsqrt(y,x)
                  CALL cMPFsprintf(VARPTR(a),VARPTR(f),y)
                  CALL cMPFclear(x)
                  CALL cMPFclear(y)
                  MSGBOX a
              
              END FUNCTION
              *crash*
              maybe i should give up.

              if i understand the powerbasic documentation corectly:
              Code:
              DIM s AS ASCIIZ*20
              DIM p AS LONG
              s="hello"
              p=VARPTR(s)
              p points to the memory location that contains the string "hello"
              what i need is a pointer to a pointer to string, how do declare such a beast?



              [This message has been edited by Johan Klassen (edited November 01, 2004).]
              as the salmon fish is compelled to go back to it's birthplace to spawn, so comes a day when man is compelled to go back to it's source.. GOD

              Comment


              • #8
                It looks like you're making this more complicated than it needs to be
                but:

                pp = VARPTR(p)

                You can pass this as "BYVAL pp" in your call, as needed.

                ------------------
                Tom Hanlin, PowerBASIC Staff
                Opinions expressed may not be those of my employer or myself

                Comment


                • #9
                  thanks for all your replies, and i appologize for my lack of
                  communication skills.

                  for what it's worth, here's the working version.

                  Code:
                  'you can download gmp from [url="http://www.cs.nyu.edu/exact/core/gmp/"]http://www.cs.nyu.edu/exact/core/gmp/[/url] 
                  
                  #COMPILE EXE
                  
                        TYPE MPF              ' multiprecision floating point
                           mp_prec AS LONG
                           mp_size AS LONG
                           mp_expt AS LONG
                           mp_limb AS LONG
                        END TYPE
                  
                  
                        DECLARE SUB cMPFinit CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_init2" (BYREF X AS MPF,BYVAL prec AS LONG)
                        DECLARE SUB cMPFclear CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_clear" (BYREF X AS MPF)
                        DECLARE SUB cMPFsprintf CDECL LIB "libgmp-3.dll" ALIAS "__gmp_sprintf" (BYREF s AS ASCIIZ,BYREF fmt AS ASCIIZ,BYREF X AS MPF)
                        DECLARE SUB cMPFset_str CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_set_str" (BYREF X AS MPF,BYREF s AS ASCIIZ,BYVAL b AS LONG)
                        DECLARE SUB cMPFsqrt CDECL LIB "libgmp-3.dll" ALIAS "__gmpf_sqrt" (BYREF Y AS MPF,BYREF X AS MPF)
                  
                  
                  FUNCTION PBMAIN () AS LONG
                      DIM x AS MPF
                      DIM y AS MPF
                      DIM a AS ASCIIZ*2000
                      DIM f AS ASCIIZ*20
                      a="2"
                      f="%77.70Fe"
                      CALL cMPFinit(x,512)'initialize x to 512 bit precision
                      CALL cMPFinit(y,512)
                      CALL cMPFset_str(x,a,10)'set x to value of a base 10
                      a=SPACE$(100)'reserve space for sprintf
                      CALL cMPFsqrt(y,x)'y=sqrt(x)
                      CALL cMPFsprintf(a,f,y)'sprintf the value of y into the string a
                      CALL cMPFclear(x)'deallocate
                      CALL cMPFclear(y)
                      MSGBOX a
                  
                  END FUNCTION
                  ------------------




                  [This message has been edited by Johan Klassen (edited November 02, 2004).]
                  as the salmon fish is compelled to go back to it's birthplace to spawn, so comes a day when man is compelled to go back to it's source.. GOD

                  Comment

                  Working...
                  X