Announcement

Collapse
No announcement yet.

ASCIIZ PTR accepts STRPTR

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

  • ASCIIZ PTR accepts STRPTR

    Hi fellows,

    Consider this as an odd question, if you want, but I've encountered some 'strange' behaviour.
    In TYPE MSGBOXPARAMS (file: Win32api.inc) some items have been declared AS ASCIIZ PTR.
    For instance: lpszText (the message) and lpszCaption (the caption).
    However, when I declare (in my program) message and caption AS ASCIIZ strings and pass them
    as STRPTR(szMessage) and STRPTR(szCaption) respectively, the following compiler error occurs:
    "Dynamic string variable expected".
    Okay, I now use dynamic strings instead and, indeed, the program works. But is'nt this controversial?
    It puzzles me since a day or two. Does someone (Lance or Tom, perhaps?) have an explanation for this?
    And, finally, what should I do to enable the use of genuine ASCIIZ strings?

    Thanks very much!

    ------------------
    mailto:[email protected][email protected]</A>
    www.basicguru.com/zijlema/

    Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
    http://zijlema.basicguru.eu
    *** Opinions expressed here are not necessarily untrue ***

  • #2
    STRPTR() returns a pointer to the dynamic string data, but since the argument you tried to use was an ASCIIZ, you received a compile-timer error.

    So, if your want to use an ASCIIZ string, you just need to use VARPTR() to return a pointer instead of STRPTR().

    IOW, you could do either of the following depending on whether you are using a dynamic string or an ASCIIZ/Fixed-length:
    Code:
    DIM A$, B AS ASCIIZ * %BufLen
    udt.param = STRPTR(A$)
    udt.param = VARPTR(b)

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

    Comment


    • #3
      One more note:

      Internally, dynamic strings have a trailing CHR$(0) appended so that they can be passed to API functions that expect nul-terminated strings. This should explain why you can pass a STRPTR() where an ASCIIZ (pointer) was expected.

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

      Comment


      • #4
        Thank you Lance!
        As far as I understand your message, it does not really matter whether I use STRPTR(DyNamicString) or VARPTR(szAsciizString)?

        BTW: Since today the 'front page' of this site pops up with yellow lightbulbs only, as if every forum contained new postings.
        In the Forum pages themselves I don't see red folder symbols, despite the fact there are new postings. Any idea?

        ------------------
        mailto:[email protected][email protected]</A>
        www.basicguru.com/zijlema/

        Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
        http://zijlema.basicguru.eu
        *** Opinions expressed here are not necessarily untrue ***

        Comment


        • #5
          The forums are working normally for me. Using Netscape 4.77.

          -- Eric
          "Not my circus, not my monkeys."

          Comment


          • #6
            You might have a cookie problem (filter)?


            ------------------
            Bernard Ertl
            Bernard Ertl
            InterPlan Systems

            Comment


            • #7
              Originally posted by Egbert Zijlema:
              BTW: Since today the 'front page' of this site pops up with yellow lightbulbs only, as if every forum contained new postings.
              In the Forum pages themselves I don't see red folder symbols, despite the fact there are new postings. Any idea?

              Forget it, folks. It indeed was a cookie problem. For one or another unknown reason "BypassCookie=" was set to "true".
              I did'nt do it myself, so one final question: how can those accidents occur?

              ------------------
              mailto:[email protected][email protected]</A>
              www.basicguru.com/zijlema/

              [This message has been edited by Egbert Zijlema (edited August 23, 2001).]

              Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
              http://zijlema.basicguru.eu
              *** Opinions expressed here are not necessarily untrue ***

              Comment


              • #8
                As far as I understand your message, it does not really matter whether I use STRPTR(DyNamicString) or VARPTR(szAsciizString)?
                Yes, but the dynamic string must have content (not empty/null)...


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

                Comment


                • #9
                  originally posted by lance edmonds:
                  yes, but the dynamic string must have content (not empty/null)...


                  it is'nt. see: http://www.powerbasic.com/support/pb...ad.php?t=23111

                  ------------------
                  mailto:[email protected]egbert.zijl[email protected]</a>
                  www.basicguru.com/zijlema/

                  [this message has been edited by egbert zijlema (edited august 23, 2001).]

                  Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
                  http://zijlema.basicguru.eu
                  *** Opinions expressed here are not necessarily untrue ***

                  Comment


                  • #10
                    In your case, you are setting a member of a UDT destined for an API call, so the API itself will very likely test for a null-pointer and react accordingly.

                    However, when dealing with exported PowerBASIC Subs/Functions that receive pointers (for example, in custom control libraries, etc), it may be wise to test that the pointer is non-zero before using it, but it will depend on how the pointer is going to be used. For example, if it is getting passed to an API that accepts null-pointers then there is no need to test it your code, but if the target memory is to be directly accessed... testing will help avoid a GPF.


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

                    Comment


                    • #11

                      You should never pass an empty dynamic string to an API function that expects an ASCIIZ string. I figured this out after my program was displaying strange "random" text in various windows, and it was all due to using empty dynamic strings (and it didn't cause any GPF either).

                      Regards,

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

                      Comment


                      • #12
                        More by good luck than good design I would suggest...

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

                        Comment


                        • #13


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

                          Comment


                          • #14
                            Depends on the API. In general, you should only pass a null ASCIIZ pointer
                            (or empty dynamic string, which is the same thing) if the function is
                            documented to accept a null pointer. Otherwise, it may just go boom.
                            The Windows APIs do a bare minimum of error checking (frequently less).

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

                            Comment

                            Working...
                            X