Announcement

Collapse
No announcement yet.

Passing a pointer between functions. Cleanest way to get the data.

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

  • Passing a pointer between functions. Cleanest way to get the data.

    This Windows API:

    Code:
    GetQueuedCompletionStatus(CompletionPortID,  pNumberOfBytes,  pCompletionKey, VARPTR(lpOVERLAPPED), %INFINITE)
    pCompletionKey gives me a pointer to a TYPE:
    Code:
    TYPE WSABUF DWORD
    dLen AS DWORD ' the length of the buffer
    buf AS BYTE PTR ' the pointer to the buffer
    END TYPE
    dLen is an dword and buf is a pointerr to an ASCIIZ

    What are the least number of steps to get dlen and the MY_ASCIIZ back so I can do something like this:

    Code:
    MSGBOX FORMAT$(dLen) + " - " + THE_STRING_THAT_IS_IN_MY_ASCIIZ

  • #2
    encapsulate the API call in a function. The return value should be a a DWORD the same as the DLEN paramater. Return the length as the function value.

    Then pass the function any data needed for the API call, but add one more parameter a string BYREF (to return a value) or an AsciiZ, which ever you prefer. Store the string pointed to by BUF in that string parameter. This way the function also returns a string as well.

    Now if the API generates an error value (its return value) return a zero for the function or if the length the string is zero (null).

    Then in your code do this:

    Code:
    DLen = MyAPIFunctionCall( .... parameters ...,   MyStringVar$)
    IF DLen <> 0 THEN
             MsgBox FORMAT$(DLen) + "  " + MyStringVar$
    ELSE
             MsgBox "Error"
    END IF
    Chris Boss
    Computer Workshop
    Developer of "EZGUI"
    http://cwsof.com
    http://twitter.com/EZGUIProGuy

    Comment


    • #3
      Hi Chris!
      Normally I would do something like that. However, this function is in a thread that runs all the time. The pointer is given to me by the Win function
      GetQueuedCompletionStatus - it is like WaitForMultiplObjects with a fixed list of arguments.

      So my question is more like,

      Given

      Code:
      TYPE COMPLETION_PORT_KEY
       ACCEPTING_SOCKET AS DWORD
       WSA_BUFER AS WSABUF
      END TYPE
      
      LOCAL MY_COMPLETION_PORT_KEY AS COMPLETION_PORT_KEY
      LOCAL MY_COMPLETION_PORT_KEY_POINTER AS COMPLETION_PORT_KEY POINTER
      How do I "de-refference" the pointer once inside the called function?

      I have a DWORD in the called function. And I want to get the individual values in the TYPE passed by pointer.

      This feels sort of screwy:

      Code:
      LOCAL MY_COMPLETION_PORT_KEY AS COMPLETION_PORT_KEY
      LOCAL MY_COMPLETION_PORT_KEY_PTR AS COMPLETION_PORT_KEY POINTER
      
      MY_COMPLETION_PORT_KEY_PTR = THE_POINTER_FROM_THE_FUNCTION
      How to I get the values referenced by the pointer into the MY_COMPLETION_PORT_KEY TYPE?

      This didnt work.

      Code:
        LOCAL A_DWORD AS DWORD
      A_DWORD = @MY_COMPLETION_PORT_KEY_PTR.ACCEPTING_SOCKET
      I suspect there is some VARPTR stuff I am not getting...

      OR

      Code:
       LOCAL MY_COMPLETION_PORT_KEY     AS COMPLETION_PORT_KEY AT CompletionPortID

      Comment


      • #4
        would it be @MY_COMPLETION_PORT_KEY_POINTER.ACCEPTING_SOCKET
        and
        @MY_COMPLETION_PORT_KEY_POINTER.WSA_BUFER. (whatever field in WSABUF) ?

        Comment


        • #5
          Not sure what you are saying Frank...

          Comment


          • #6

            Code:
            TYPE COMPLETION_PORT_KEY
             ACCEPTING_SOCKET AS DWORD
             WSA_BUFER AS WSABUF
            END TYPE
            
            TYPE WSABUF DWORD
             dLen AS DWORD ' the length of the buffer
             buf AS BYTE PTR ' the pointer to the buffer
            END TYPE
            
            LOCAL pCompletionKey as COMPLETION_PORT_KEY Ptr
            
            GetQueuedCompletionStatus(CompletionPortID, pNumberOfBytes, pCompletionKey, VARPTR(lpOVERLAPPED), %INFINITE)
            X = @pCompletionKey.Accepting_Socket
            Y = @pCompletionKey.WSA_Bufer.dLen

            Comment


            • #7
              Here are the two TYPES:
              Code:
              TYPE COMPLETION_PORT_KEY
                ACCEPTING_SOCKET AS DWORD
                WSA_BUFER AS WSABUF
              END TYPE
              
              TYPE WSABUF DWORD
               dLen AS DWORD ' the length of the buffer
               buf AS BYTE PTR ' the pointer to the buffer
              END TYPE
              This works but feels longer than is should need to be???

              Code:
              RESULT = GetQueuedCompletionStatus(CompletionPortID, pNumberOfBytes, pCompletionKey, VARPTR(lpOVERLAPPED), %INFINITE)
              last_error = GetLastError
              
              
              LOCAL MY_COMPLETION_PORT_KEY AS COMPLETION_PORT_KEY
              LOCAL MY_COMPLETION_PORT_KEY_PTR AS COMPLETION_PORT_KEY POINTER
              
              MY_COMPLETION_PORT_KEY_PTR = pCompletionKey
              
              MY_COMPLETION_PORT_KEY = @MY_COMPLETION_PORT_KEY_PTR
              
              LOCAL A_DWORD AS DWORD
              
              A_DWORD = MY_COMPLETION_PORT_KEY.ACCEPTING_SOCKET
              
              LOCAL WSA_BUFER AS WSABUF
              
              WSA_BUFER = MY_COMPLETION_PORT_KEY.WSA_BUFER
              
              LOCAL my_var_len AS DWORD
              
              my_var_len = WSA_BUFER.dlen
              
              
              LOCAL temp_string_pointer AS ASCIIZ POINTER
              LOCAL temp_string AS ASCIIZ *4098
              
              temp_string_pointer = VARPTR(temp_string_pointer)
              
              temp_string = SPACE$(4098)
              
              temp_string_pointer = WSA_BUFER.buf
              
              temp_string = @temp_string_pointer 'WSA_BUFER.buf
              
              
              
              
              
              
              TEXT_TO_SCREEN "-- FROM INSIDE THE WORKER THREAD --> " + temp_string
              TEXT_TO_SCREEN ""

              Comment


              • #8
                Code:
                DIM pcpk AS COMPLETION_PORT_KEY PTR
                pcpk = pCompletionKey
                DIM s AS STRING
                s = PEEK$(@pcpk.WSA_BUFER.buf, @pcpk.WSA_BUFER.dLen)
                print s
                Forum: http://www.jose.it-berater.org/smfforum/index.php

                Comment


                • #9
                  Thanks to everyone!!

                  Comment

                  Working...
                  X