I have wrote a DLL in Free Pascal and want to call it in Excel. I would be grateful if someone can help me out.
My Free Pascal DLL for testing is
procedure chess(var number : single);stdcall;
begin
WriteLn(output,'Wir testen die Zahl: ', number);
end;
exports
chess;
begin
end.
In Excel, I have the following VBA code
Private Declare PtrSafe Sub chess Lib "D:\FS\LTMProjekt\TestWB\CLModul_lib.dll" (ByVal Number As Single)
When I call the DLL in VBA with Call chess(TSim)
Excel crashes and closes itself.
I cannot find out why for the last couple of days and it gets realy frustrating now.
Hopefully someone can help me out.
Thanks in advance.
Edit:
Pascal Code of the DLL:
library CLM_WEAdynModell;
{$mode objfpc}{$H+}
uses
CLM_GlobalVariables, CLM_LoadCalc, CLM_Initialization;
type
IOA = array[1..150] of single;
procedure callTModel(var IOArray : IOA); stdcall;
begin
getInputValues(IOArray);
defineModel;
calcSecForces;
writeOutputValues(IOArray);
writeLogFile(IOArray);
end;
exports
callTModel;
begin
end.
The VBA Code:
Option Explicit
Private Declare PtrSafe Sub callTModel Lib "D:\FS\LTMProjekt\TestWB\CLM_WEAdynModell.dll" (ByRef IOArray() As Single)
Private Sub CB_RUN_Click()
Dim ValID, TStep, INLimit, aIndex, bIndex, ActSensor, VisNmax, OUTLimit, i As Integer
Dim TSim, TMax As Single
Dim IOArray(149) As Single
With ActiveSheet
TStep = 0
ValID = 0
INLimit = 9
VisNmax = 20
ActSensor = .Range("NoSensor").Value + 1
OUTLimit = .Range("OUTLimit")
TSim = 0
TMax = .Range("TMax")
ThisWorkbook.Worksheets("IstRes").Range("A4:IV8000").ClearContents
Application.ScreenUpdating = False
Do While TSim < TMax
TSim = .Range("TSim")
aIndex = (4 + TStep) 'Zeilenindex für Source Array
For i = 1 To INLimit
bIndex = (1 + i) ' Spaltenindex für Source Array
If (i < 10) Then IOArray(24 + i) = ThisWorkbook.Worksheets("SourceData").Cells(aIndex, bIndex).Value
bIndex = bIndex + INLimit
If (i < 10) Then IOArray(39 + i) = ThisWorkbook.Worksheets("SourceData").Cells(aIndex, bIndex).Value
bIndex = bIndex + INLimit
If (i < 4) Then IOArray(7 + i) = ThisWorkbook.Worksheets("SourceData").Cells(aIndex, bIndex).Value
bIndex = bIndex + 3
If (i < 4) Then IOArray(19 + i) = ThisWorkbook.Worksheets("SourceData").Cells(aIndex, bIndex).Value
Next i
IOArray(101) = OUTLimit
Call callTModel(IOArray)
aIndex = (4 + TStep) 'Zeilenindex für Target Array
ThisWorkbook.Worksheets("IstRes").Cells(aIndex, 1).Value = ThisWorkbook.Worksheets("SourceData").Cells(aIndex, 1).Value
For i = 1 To OUTLimit
bIndex = (1 + i) ' Spaltenindex für Source Array
ThisWorkbook.Worksheets("IstRes").Cells(aIndex, bIndex).Value = IOArray(101 + i)
Next i
Application.ScreenUpdating = True
If TStep < VisNmax Then
.Cells(9 + TStep, 3).Value = ThisWorkbook.Worksheets("SollRes").Cells(aIndex, ActSensor).Value
.Cells(9 + TStep, 4).Value = ThisWorkbook.Worksheets("IstRes").Cells(aIndex, ActSensor).Value
Else
For i = 1 To (VisNmax - 1)
.Cells(8 + i, 3).Value = .Cells(9 + i, 3).Value
.Cells(8 + i, 4).Value = .Cells(9 + i, 4).Value
Next i
.Cells(8 + VisNmax, 3).Value = ThisWorkbook.Worksheets("SollRes").Cells(aIndex, ActSensor).Value
.Cells(8 + VisNmax, 4).Value = ThisWorkbook.Worksheets("IstRes").Cells(aIndex, ActSensor).Value
End If
TStep = TStep + 1
.Range("TStep") = TStep
Call ScrUpdateEnableNoFlicker
Application.ScreenUpdating = False
Loop ' Ende DoWhile TSim <= TMax
End With ' ActiveSheet
'Application.ScreenUpdating = True
End Sub