Jump to content

DllCall to my dll function returning structure is not working.


Recommended Posts

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.

 

 

Link to comment
Share on other sites

  • Moderators

Moved to the correct forum, as the Technical Discussion form very clearly states "DO not post general support questions here"

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...