Announcement

Collapse
No announcement yet.

ASCIIZ DLL Parameter with dynamic length ?

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

  • ASCIIZ DLL Parameter with dynamic length ?

    Hi,

    I try to write a DLL-function in PowerBasic, witch should be called from my Alaska Xbase++ program. As long I only use BYVAL cTxt as STRING all works perfect.

    But I need a changed string back to my calling program.
    If I call by reference from Xbase++ the STRING parameter will be passed as a ASCIIZ string with needed length.

    FUNCTION MYINFO(BYREF cTxt AS ASCIIZ * 40) EXPORT AS LONG

    will work, but I want the PowerBasic DLL to accept shorter and longer strings too. Is it possible to do ?

    FUNCTION MYINFO(BYREF cTxt AS STRING) EXPORT AS LONG

    will not work, because the runtime errorhandler from my Xbase++ program reports a data corruption.

    I thought of a pointer in the declaration to the ASCIIZ String, but I dont know how to do ...

    Thanks for your help

    by
    Hubert
    Last edited by Hubert Brandel; 8 Feb 2008, 04:53 AM.
    Regards,
    Hubert

    ------------------------------------
    http://familie-brandel.de/index_e.html

  • #2
    Have you tried?

    FUNCTION MYINFO(BYREF cTxt AS ASCIIZ) EXPORT AS LONG
    Forum: http://www.jose.it-berater.org/smfforum/index.php

    Comment


    • #3
      Pointers are greate ...

      Hi,

      I had a second look at the DIM statement and found the solution:

      FUNCTION MYINFO(BYVAL nDataRef as ASCII PTR) EXPORT AS LONG
      msgbox @nDataRef ' show whats comming in
      @nDataRef = "My New Info"+$NUL
      function = len(@nDataRef)
      END FUNCTION

      This can NOT expand the length of the string parameter, but it can change the contense. With the return value as length of the string the calling programm can truncate what it needs.

      It would be better if the function self could expand the string length.
      But I can use what I have seen on API functions, a zero length string parameter will change the return value to the length to be needed.
      Regards,
      Hubert

      ------------------------------------
      http://familie-brandel.de/index_e.html

      Comment


      • #4
        Originally posted by José Roca View Post
        Have you tried?
        FUNCTION MYINFO(BYREF cTxt AS ASCIIZ) EXPORT AS LONG
        it will compile without error ... I thought I have to define the length ...

        function MYINFO(BYREF cTxt AS ASCIIZ) export as long
        msgbox cText
        cText = "MyInfo aus DLL - direkt"
        function = len(cText)
        END FUNCTION

        will work too, but the return value will be the length of PowerBasic. But the return Value in the calling program is still as short as it was.

        So if I take care of the length problem, your solution ist much simpler to write than mine
        Regards,
        Hubert

        ------------------------------------
        http://familie-brandel.de/index_e.html

        Comment


        • #5
          This can NOT expand the length of the string parameter, but it can change the contense. With the return value as length of the string the calling programm can truncate what it needs.
          Bear in mind that when you use an ambiguous-length parameter (BYREF ASCIIZ, or BYVAL ASCIIZ PTR, without the " * length" clause), you are totally at the mercy of the calling procedure that the parameter passed has sufficient memory behind it.

          "Generally" if a procedure accepts an ambiguous-length parameter, it also accepts a "buffer length" parameter so it can decide if it's safe to fill the passed buffer.

          e.g.
          Code:
          FUNCTION Foo (sz AS ASCIIZ, cbLen AS LONG) AS LONG
          
            LOCAL s AS STRING, l as long
            S = "The quick brown fox was banned in Britain" 
            L =  Len (S)
            IF l <=cbLEn then
               sz = S 
              FUNCTION = 0   ' success 
           ELSE
             FUNCTION = -1& ' "insufficient buffer"
           END IF
          END FUNCTION
          This is not really a consideration if this is an 'internal use' function since you control everything, but if this is a function you are providing for others, that buffer length parameter is virtually mandatory.

          Note that the use of the IsBadWritePtr function to test the length is not sufficient, as the passed address may be from the caller's stack or data segment, in which case it WILL be a valid pointer, but writing to it can then overwrite the stack or other program data.

          MCM
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            Thanks for the hints. I will do it with these parameters.
            In 5 years I can't remember the other restrictions
            Regards,
            Hubert

            ------------------------------------
            http://familie-brandel.de/index_e.html

            Comment

            Working...
            X