Call DLL Function (from Event Handler) Crashes?

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
todd_mm
Posts: 4
Joined: Fri Feb 03, 2017 4:47 pm

Call DLL Function (from Event Handler) Crashes?

Post 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.
LibreOffice 5.2.4 on Windows 7
User avatar
RoryOF
Moderator
Posts: 34791
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

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

Post by RoryOF »

Are not LibreOffice/OpenOffice BASIC variables case insensitive?
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
todd_mm
Posts: 4
Joined: Fri Feb 03, 2017 4:47 pm

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

Post by todd_mm »

Probably.
Why would that matter? Am I using two variables with the same (insensitive) name?
LibreOffice 5.2.4 on Windows 7
JeJe
Volunteer
Posts: 2938
Joined: Wed Mar 09, 2016 2:40 pm

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

Post 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...
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Zizi64
Volunteer
Posts: 11421
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

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

Post 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.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
todd_mm
Posts: 4
Joined: Fri Feb 03, 2017 4:47 pm

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

Post 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.
LibreOffice 5.2.4 on Windows 7
User avatar
Villeroy
Volunteer
Posts: 31324
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

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

Post by Villeroy »

OpenOffice on Windows is a 32-bit application.
LibreOffice is availlable in 64 bit.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
todd_mm
Posts: 4
Joined: Fri Feb 03, 2017 4:47 pm

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

Post 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.
LibreOffice 5.2.4 on Windows 7
ms777
Volunteer
Posts: 199
Joined: Mon Oct 08, 2007 1:33 am

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

Post 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
Post Reply