Jump to content

Recommended Posts

Posted

I have create a function in FreeBasic like below:

Extern "Windows-MS"

    Type tA
        f1 As Integer
        f2 As Integer
    End Type

    Public Function _switchOrder(ByVal no1 As Integer, ByVal no2 As Integer) As tA Export
        Dim result As Integer

        Dim taa As  tA  

        taa.f1 = no2

        taa.f2 = no1

        Return taa

    End Function

End Extern

Caller AutoIt code is:

#include <MsgBoxConstants.au3>

Global Const $sTag_ftdi_version_info="struct; int no1a; int no2a; endstruct"

Local $aRet=DllCall("Math1.dll","Ptr","_switchOrder", "Int", 10, "Int", 30)
;MsgBox (0,"",@error & "-" & $aRet[0] & "-" & $aRet[1]& "-" & $aRet[2])

Local $t_ftdi_version_info=DllStructCreate($sTag_ftdi_version_info,$aRet[0])
MsgBox (0,"msg1=",@error & "---" & $aRet[0] & "-" & $aRet[1]& "-" & $aRet[2])

;Local $retData1 = DllStructGetData($t_ftdi_version_info,"",1)
Local $retData1 = DllStructGetData($t_ftdi_version_info,"no1a")
MsgBox (0,"msg2=",@error & "--" & $retData1)

;Local $retData1 = DllStructGetData($t_ftdi_version_info,"",2)
Local $retData1 = DllStructGetData($t_ftdi_version_info,"no2a")
MsgBox (0,"msg2=",@error & "--" & $retData1)

;ConsoleWrite(DllStructGetData($t_ftdi_version_info,"",2) & "--" & @error)
;ConsoleWrite(DllStructGetData($t_ftdi_version_info,"no2a") & @CRLF)
;ConsoleWrite(DllStructGetData($tversion_str,1) & @CRLF)

Getting error 2 for DllStructGetData or it give Close Application AutoIt popup message. 

Certainly DllCall is not returning pointer to the Structure in $aRet[0] hence issue.

Can someone help me fix this please?

Thanks in advance.

 

 

Posted

Mr. JLogan,

Just be clear, was my this question supposed to be in General Support category?

Thanks for bearing with me till I get to understand the categories clearly.

Posted

Hello I'm not a freebasic programmer but I think your structure will be free when the function ends. probably you need to use a global structure. I'm not sure if freebasic provide structure pointer or need to use a function like strptr or varptr like old vb6 or something like @Mystructure.  I think is not a good practices to return an structure from a library.  the correct way should be that you pass your structure's pointer an be filled/modify inside the library.

 

Saludos

Posted (edited)

It seems @Danyfirex is right.

After some tests, it seems this one works :

Extern "Windows-MS"
    Type tA
        f1 As Integer
        f2 As Integer
    End Type
    
    Public Function addition(no1  As Integer, no2  As Integer, Byref v As tA Ptr) As Integer Export
        Dim o As tA
        Dim s As Integer
        o.f1 = no2
        o.f2 = no1
        *v = o
        Return 0
    End Function

End Extern
Local $ptr = DllStructCreate("int f1;int f2")

$aRet = DllCall('test.dll', 'int', 'addition', 'int', 10, 'int', 30, 'ptr*', DllStructGetPtr($ptr) )
ConsoleWrite( "F1=" & DllStructGetData($ptr, 'f1') & @LF & _
              "F2=" & DllStructGetData($ptr, 'f2') & @LF )

 

Edited by jguinch
Posted

By the way I tried with Array too using ClassLibrary using Vb.net and C# both but it does not work. The other functions with VB.Net and C# returning Integers etc. worked fine.

I will try now with ByRef Array and modify them in ClassLibrary function as suggested by DanyFirex above and see if I get the data back. 

If it works fine my main purpose of returning multiple large number of items from classlibrary function is solved then in that case. 

I will revert after trying it out. Thanks in the meanwhile.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...