No announcement yet.

Want to make a multipurpose function in PB for VB.

  • Filter
  • Time
  • Show
Clear All
new posts

  • Want to make a multipurpose function in PB for VB.

    I want to return a double or string as dword like this

    Function ReadIt( ByVal what as Long ) Export As Dword
        Dim T       As String
        Dim DBL     As Double
        Dim Temp    As Dword
        Select Case What
        Case 0
         T = "1234"
         ! MOV EAX,  T
         ! MOV TEMP, EAX
         ! MOV DWORD T, 0
         FUNCTION = Temp
        Case 1
    'Example from PB but i need to set a double instead..
            DBL = 1234567
            ! mov AL, Byte Ptr rgb_value[0]
            ! xor AH, AH
            ! mov FUNCTION[0], AX
        End Select
    End Function
    Declare Function ReadItSTRING Lib "MY.DLL" Alias "ReadIt" (ByVal What As Long ) As String
    Declare Function ReadItDouble Lib "MY.DLL" Alias "ReadIt" (ByVal What As Long ) As Double

    The Double part is not working (Not with the example above of course but i tried it with MKD$ etc.. )


    Or can use VB variants somehow?

    [email protected]

  • #2
    It is not quite clear to me what you want to accomplish...

    1) Convert a DOUBLE or string to a DWORD? Consider CLNG and VAL functions.

    2) Put the contents of a DOUBLE or string in a DWORD? Well, you can fit up
    to four characters of a string in a DWORD, but a DOUBLE isn't going to fit
    nohow. Storing only half of a DOUBLE isn't likely to be useful.

    3) Return a pointer to a DOUBLE or a string? In that case, you'll need a
    STATIC buffer rather than LOCAL dim'ed values, or they'll disappear when
    the function exits. You also can't use a regular STRING this way, as
    they're inherently dynamic, but you can use a fixed-length string, e.g.:
    STATIC foo AS STRING * 10
    To return a pointer, use VARPTR on the variable name-- or, if you're
    using asm, load the OFFSET of the value rather than the value itself:
    !mov eax, offset T
    Tom Hanlin
    PowerBASIC Staff


    • #3
      The string part i showed is provided in the PB examples (somewhere)
      It does it ok.

      Simply speaking:
      Why couldn't i use it on a double instead?

      That's what i like to do with it.

      Passing from VB to PB i could simply use "as Any" wich unf. doesn't thell WHAT type it is.

      But returnning different types USING a function return value is a bit unclear to me.
      I'm willing to declare a few VB declares/aliases to return a double or string whatever.
      But in PB there must be a way to use one procedure only.
      Therefore my question, can the string-return-stuff be rebuild to returning a double.
      I have 0 knowledge of ASM so...

      With my tries VB returns an error 16 "string to ..."

      I also try the offset technique tommorow (whatever it might mean.. )

      Maybe you can help me out retuning the double using a dword pointer..

      [email protected]


      • #4
        Problem is, here, Visual Basic has about zero understanding of what a pointer is.
        If you return a pointer to a DOUBLE, I don't think there's any easy way to use
        that in VB. If you defined ReadIt as a SUB instead of a FUNCTION, and returned
        the value through a parameter, it would be more practical but, perhaps, not what
        you had in mind.

        An abbreviated version of your function, to return a DOUBLE PTR, might go:
            STATIC dbl AS DOUBLE
            LOCAL dblptr AS DWORD
            dbl = 1234567
            !mov eax, offset dbl
            !mov dblptr, eax
            FUNCTION = dblptr
        I'm assuming you want to use asm code for some reason. Otherwise, VARPTR
        would be a better approach.

        Tom Hanlin
        PowerBASIC Staff