Page 1 of 1

Call DLL Function (from Event Handler) Crashes?

Posted: Fri Feb 03, 2017 5:15 pm
by todd_mm
I'm attempting to call functions in an external DLL from a button handler in scalc, but LibreOffice crashes at the end of the handler.
I'm not certain what I'm doing incorrectly. The external function calls "succeed" (has the desired effect, returns data, etc.), but at the end of the handler, LO crashes.

Here's the BASIC macro I have.

Code: Select all

Declare Function _myfunc Lib "path\to\my.dll" Alias "myfunc" (ByVal inst as Integer) as Integer
Function myFunc(ByVal x as Integer) as Integer
    Dim rc as Integer
    rc = _myfunc(x)
    myFunc = rc
End Function

Sub ButtonHandler(oEvent)
    Print myFunc(0)
    FreeLibrary "path\to\my.dll"
End Sub
Am I doing this the proper way? Do I need to FreeLibrary at the end? It crashes with or without it. I tried putting it inside the function making the external call, without meaningful effect. Is there some other setup/teardown I need to do, in general? Are there limitations on event handlers?
Thanks.

Re: Call DLL Function (from Event Handler) Crashes?

Posted: Fri Feb 03, 2017 5:43 pm
by RoryOF
Are not LibreOffice/OpenOffice BASIC variables case insensitive?

Re: Call DLL Function (from Event Handler) Crashes?

Posted: Fri Feb 03, 2017 5:45 pm
by todd_mm
Probably.
Why would that matter? Am I using two variables with the same (insensitive) name?

Re: Call DLL Function (from Event Handler) Crashes?

Posted: Sat Feb 04, 2017 12:58 am
by JeJe
Try in an otherwise empty module

Declare Function _myfunc Lib "path\to\my.dll" Alias "myfunc" (ByVal inst as Integer) as Integer

Sub test()
dim a as integer,b as integer
a= _myfunc(b)
End Sub

Make things the simplest possible and try...

Re: Call DLL Function (from Event Handler) Crashes?

Posted: Sat Feb 04, 2017 9:19 am
by Zizi64
Or upload please the project (the ODF file with the macro code, and the DLL file. Then we will able to try it and (maybe) to fix the bug.

Re: Call DLL Function (from Event Handler) Crashes?

Posted: Tue Feb 07, 2017 7:33 pm
by todd_mm
It's possible that there's a stack problem. Is there a way to specify the calling convention to be used? I'm having trouble finding documentation for this kind of thing.

Re: Call DLL Function (from Event Handler) Crashes?

Posted: Tue Feb 07, 2017 9:05 pm
by Villeroy
OpenOffice on Windows is a 32-bit application.
LibreOffice is availlable in 64 bit.

Re: Call DLL Function (from Event Handler) Crashes?

Posted: Thu Feb 09, 2017 8:39 pm
by todd_mm
I'm using all 32-bit stuff. The DLL is a 32-bit binary, and I'm using a 32-bit version of LibreOffice.

Re: Call DLL Function (from Event Handler) Crashes?

Posted: Sat Apr 15, 2017 6:24 pm
by ms777
... this works on my Windows 10 (writes a '9'). You may want to try if this also crashes in your handler. Then you can be pretty sure that it is an AO problem

Code: Select all

Declare function keybd_event Lib "user32.dll" ( ByVal bVk As Long, ByVal bScan As Long, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

sub main
  call keybd_event(57, 0, 0, 0)
end sub