ENDLICH: DLL's in BASIC (FreeBasic Ver. 0.90.1)

Antworten
IKT
Beiträge: 554
Registriert: Mittwoch 4. November 2009, 18:32

ENDLICH: DLL's in BASIC (FreeBasic Ver. 0.90.1)

Beitrag von IKT » Sonntag 9. Juni 2013, 14:26

@alle

nach ca. einer sehr intensiven Woche, ist es mir gelungen eine DLL in FreeBASIC mit CCalculateEx herzustellen. Das Hauptproblem waren natürlich (mal wieder) die Strings.

Code: Alles auswählen

' :::::::::: DLL-START ::::::::::::: '
Extern "windows-ms"

' ===== DEFINE NAMES TO INDEXES ===== ' easier to remember than numbers

' PInputs (none in this DLL)

' POutputs
const as byte path = 0
const as byte prog = 1
const as byte errx = 2

' PUser (none in this DLL)

' PStrings preparation (different from numeric variables)
const as short SI = 1001                ' SI = String Index multiplier factor

' PStrings
const as byte OStrg1 = 0 
const as byte OStrg2 = 1

' other var's
STATIC SHARED AS STRING EPath, EProg    ' temporary static string var's, used in CSimStart and CCalculateEx


' ===== DEFINITION OF EXTERNAL FUNCTION's/SUB's ===== '

'Function NumInputsEx () as Byte Export ' used only if configuable by CConfig
Function NumInputs () as Byte Export    ' static definition

    Function = 0
End Function


'Function NumOutputsEx () as Byte Export' used only if configuable by CConfig
Function NumOutputs () as Byte Export   ' static definition

    Function = 3
End Function


Sub GetInputName (byval Channel as Byte, byref InputName as ZString) Export
    
' not used
End Sub


Sub GetOutputName (byval Channel as Byte, byref OutputName as ZString) Export 

	Select CASE Channel
		Case 0
			OutputName = "$Path"
        Case 1
			OutputName = "$Prog"
        Case 2
            OutputName = "ERRx"
	End Select
End Sub


Sub CSimStart (byval PInput as Double ptr, byval POutput as Double ptr, byval PUser as Double ptr) Export

    EPath = ExePath + "\"
    EProg = "ExePath.dll (c) ML-Enterprises, 2013"   ' needs to be done once only, fill TempStrings
End Sub


SUB CCalculateEx (byval PInput as DOUBLE PTR, byval POutput as DOUBLE PTR, _
    byval PUser as DOUBLE PTR, byref PStrings as ZString PTR) EXPORT

    ' NO TYPEDEFS needed at all ... but, see next line
    ' IMPORTANT! String Indexes must be multiplied with SI to work correctly
    ' Conclusion: the strings are really 1001 bytes (1000 bytes string + terminator)
    PStrings[path * SI] = EPath         ' needs to be here because of NO strings in CSimStart
    PStrings[prog * SI] = EProg
    POutput[path] = PStrings[path * SI]
    POutput[prog] = PStrings[prog * SI]
    POutput[errx] = 98765.01234         ' test for numeric out (comment out when writing *.log) see below
    
    ' writing to a file to sort out possible errors (comment it out when OK [creates a lot of 'traffic' on disk])
'    dim as integer x = -1               ' set fault ... quick and dirty err checking
'    VAR fnr = FREEFILE, fname = "X:\YOUR\PATH\TO\dll.log"   ' adapt: 'Drive:\...\...\...\file.ext' to your settings
'                                        ' Remark: make shure that your USER has the necessary access rights (in OS)
'    x = open (fname, FOR OUTPUT, AS #fnr)
'    POutput[errx] = CDbl(x)             ' to 'see' if 'file open' throws an error, numeric out
'    if x = 0 then                       ' if 'file open' is OK, 'dll.log' is written to
'        print #fnr, "Path : " + PStrings[path * SI]
'        print #fnr, "Prog : " + PStrings[prog * SI]
'        print #fnr, "File#: " & fnr     ' # stands for number, as supplied by FreeFile
'        close #fnr
'    end if
END SUB


Sub CSimStop (byval PInput as Double ptr, byval POutput as Double ptr, byval PUser as Double ptr) Export

' not used
End Sub


Sub CConfigure (byval PUser As Double ptr) Export
	
	Beep ' only for test	
End Sub


End Extern

' :::::::::: END OF DLL :::::::::: '
Zuletzt geändert von IKT am Freitag 2. Mai 2014, 11:13, insgesamt 1-mal geändert.
++++++
Gruss/regards IKT

IKT
Beiträge: 554
Registriert: Mittwoch 4. November 2009, 18:32

Re: ENDLICH: DLL's in BASIC (FreeBasic V. 0.24.0)

Beitrag von IKT » Dienstag 11. Juni 2013, 09:10

@alle,

da die String Indizierung im obigen Code Beispiel sehr rudimentär ist, hier noch eine verbesserte Variante (Neue Deklaration von Strings in CCaculateEx):

Code: Alles auswählen

' :::::::::: DLL-START ::::::::::::: '
Extern "windows-ms"

' ===== DEFINE NAMES TO INDEXES ===== ' easier to remember than numbers

' PInputs (none in this DLL)

' POutputs
const as byte path = 0
const as byte prog = 1
'const as byte errx = 2                  ' (only for testing)

' PUser (none in this DLL)

' PStrings
const as byte OStrg1 = 0 
const as byte OStrg2 = 1

' other var's
STATIC SHARED AS STRING EPath, EProg    ' temporary static string var's, used in CSimStart and CCalculateEx


' ===== DEFINITION OF EXTERNAL FUNCTION's/SUB's ===== '

'Function NumInputsEx () as Byte Export ' used only if configuable by CConfig
Function NumInputs () as Byte Export    ' static definition

    Function = 0
End Function


'Function NumOutputsEx () as Byte Export' used only if configuable by CConfig
Function NumOutputs () as Byte Export   ' static definition

    Function = 2
End Function


Sub GetInputName (byval Channel as Byte, byref InputName as ZString) Export
    
' not used
End Sub


Sub GetOutputName (byval Channel as Byte, byref OutputName as ZString) Export 

	Select CASE Channel
		Case 0
			OutputName = "$Path"
        Case 1
			OutputName = "$Prog"
        'Case 2
            'OutputName = "ERRx"        (only for testing)
	End Select
End Sub


Sub CSimStart (byval PInput as Double ptr, byval POutput as Double ptr, byval PUser as Double ptr) Export

    EPath = ExePath + "\"
    EProg = "ExePath.dll (c) ML-Enterprises, 2013"   ' needs to be done once only, fill TempStrings
End Sub


SUB CCalculateEx (byval PInput as DOUBLE PTR, byval POutput as DOUBLE PTR, _
    byval PUser as DOUBLE PTR, byval PStrings as ZString PTR PTR) EXPORT
    ' NO TYPEDEFS needed at all ...
    ' Conclusion: the strings are really 1001 bytes (1000 bytes string + terminator)
    *PStrings[OStrg1] = EPath             ' needs to be here because of NO strings in CSimStart
    *PStrings[OStrg2] = EProg
    POutput[path]   = *PStrings[OStrg1]
    POutput[prog]   = *PStrings[OStrg2]
    ' POutput[errx]   = 98765.01234       ' test for numeric out (only for testing)
END SUB


Sub CSimStop (byval PInput as Double ptr, byval POutput as Double ptr, byval PUser as Double ptr) Export

' not used
End Sub


Sub CConfigure (byval PUser As Double ptr) Export
	
	Beep ' only for test	
End Sub


End Extern
' :::::::::: END OF DLL :::::::::: '
++++++
Gruss/regards IKT

habe
Beiträge: 36
Registriert: Samstag 18. Oktober 2008, 18:51

Re: ENDLICH: DLL's in BASIC (FreeBasic V. 0.24.0)

Beitrag von habe » Mittwoch 12. Juni 2013, 11:47

Hallo IKT,

das es mit FreeBasic geht wuste ich s.h.
viewtopic.php?f=43&t=2942&start=30
Hab das aber nie mit den Strings hinbekommen.
Besten dank dafür :D

IKT
Beiträge: 554
Registriert: Mittwoch 4. November 2009, 18:32

Re: ENDLICH: DLL's in BASIC (FreeBasic V. 0.24.0)

Beitrag von IKT » Donnerstag 13. Juni 2013, 16:48

Hallo habe,

gern geschehen.

Da ich vielleicht ein bisschen perfektionistisch veranlagt bin, "funktioniert's" bei mir erst, wenn alles funktioniert, da ich mich danach voll und ganz auf den INHALT der DLL konzentrieren kann.
Kann auch folgendermassen zusammengefasst werden:
Punkt 1 => erledigt,
weiter mit => Punkt 2 usw.
++++++
Gruss/regards IKT

Antworten

Zurück zu „DLL-Programmierung“