Recent

Author Topic: [solved] load hunspell.dll  (Read 18108 times)

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: load hunspell.dll
« Reply #15 on: February 09, 2018, 07:11:03 am »
For benefit of anyone following ...
A 64bit version of Hunspell (specifically the DLL) can be built on Windows, you have to install MSYS2, thats easy (but a disk eater) and then build with gcc.

The resulting DLL seems to work OK when tested using hunspell's own command line tool and it loads fine when called with our FPC LoadLibrary() but crashes somewhere (on a malloc) internally when I call the hunspell_create() function.

Further research is indicated .....
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: load hunspell.dll
« Reply #16 on: February 09, 2018, 11:40:08 am »
Running the code below didn't crash for me. I didn't even need the .aff and .dic files yet.

I did need the libgcc_s_seh-1.dll, libstdc++-6.dll and libwinpthread-1.dll to do LoadLibrary but I didn't compile my libhunspell-1.3-0.dll with -static-libgcc and -static-libstdc++ yet.

Code: Pascal  [Select][+][-]
  1. uses Windows, dynlibs;
  2.  
  3. type
  4.   THunspell_create = function(const affpath, dpath: PAnsiChar): Pointer; cdecl;
  5.  
  6. var
  7.   Hunspell_create: THunspell_create;
  8.  
  9. procedure TForm1.Button1Click(Sender: TObject);
  10. const
  11.   LibName = 'libhunspell-1.3-0.dll';
  12. var
  13.   HunLibHandle: TLibHandle;
  14.   fHunHandle: Pointer;
  15.   Aff, Dic: TFileName;
  16. begin
  17.   HunLibHandle := LoadLibrary(LibName);
  18.   if HunLibHandle = dynlibs.NilHandle then
  19.   begin
  20.     ShowMessage('Error ' + GetLastError.ToString + ' ' + ExtractFilePath(Application.ExeName) + LibName);
  21.     exit;
  22.   end;
  23.  
  24.   Hunspell_create := THunspell_create(GetProcAddress(HunLibHandle, 'Hunspell_create'));
  25.   if not Assigned(Hunspell_create) then exit;
  26.  
  27.   Aff := ExtractFilePath(Application.ExeName) + 'en_US.aff';
  28.   Dic := ExtractFilePath(Application.ExeName) + 'en_US.dic';
  29.   fHunHandle := Hunspell_create(pointer(Aff), pointer(Dic));
  30.  
  31.   ShowMessage('success');
  32.  
  33.   FreeLibrary(HunLibHandle);
  34.  
  35. end;

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: load hunspell.dll
« Reply #17 on: February 10, 2018, 09:21:12 am »
Thanks RVK, I just tried your specific code in case I had something in mine (which is basically the same) out of wack.

But it still segV when I try and execute the line -

fHunHandle := Hunspell_create(pointer(Aff), pointer(Dic));

Now, I note you are using v1.3  of Hunspell, I have v1.6. Are you using one built some time ago ?  Should I go back and get an earlier release and try that ?

I also notice you don't set a path to the library, is that because its properly installed on your system ?  Mine is not, I've just compiled it and copied it out of the MSYS2 tree into my lazaras project working directory (along with the other dependancy DLLs). Perhaps thats not a viable way to work ?

(I did warn you I don't 'do' Windows ...)

David
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: load hunspell.dll
« Reply #18 on: February 10, 2018, 10:25:26 am »
I also notice you don't set a path to the library, is that because its properly installed on your system ? 
On windows the loader will search for the dll specified, on the application's directory, the system folders (system/system32/syswow64) and in the system wide path. In that order you only need to place the dll in one of those locations and it will be loaded.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: load hunspell.dll
« Reply #19 on: February 10, 2018, 11:17:11 am »
Thanks taazz, the DLL is being loaded OK. I have it in the (lazarus's) app worKing directory and set an explicit path to it. There is a three plus set process -
  • Load the library - OK
  • Identify the address of the (eg) hunspell_create function - OK
  • Call the function at that address. - SEGV

But I am worried that Windows does not like me calling a function in a DLL thats not "registered" - is that the word ?

I'm reluctant to go ahead and run the hunspell installer as I'm not sure I can reverse the install and its my only windows test platform. Might have to bite the bullet....
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: load hunspell.dll
« Reply #20 on: February 10, 2018, 11:22:51 am »
But I am worried that Windows does not like me calling a function in a DLL thats not "registered" - is that the word ?
only for activeX dlls/controls for simple dlls it should work.
I'm reluctant to go ahead and run the hunspell installer as I'm not sure I can reverse the install and its my only windows test platform. Might have to bite the bullet....
use a virtual machine with windows installed you can froze it to a specific point in time and undo everything when the vm shuts down or backup the files to an external drive and replace them when you need to undo the changes.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: load hunspell.dll
« Reply #21 on: February 10, 2018, 12:37:29 pm »
I'm reluctant to go ahead and run the hunspell installer as I'm not sure I can reverse the install and its my only windows test platform. Might have to bite the bullet....
What is the hunspell installer?
In the steps for Linux, which is the second part you follow after installing msys2 and it's dependencies, you would run make install. But I don't think that installs anything in Windows. Just in the virtual environment of msys2.

I did build 1.3.0 because that was the one supplied with THunspell and it had a direct download link.
I'm now building the latest "-master" release..... building... building... done.

Just tried the resulting libhunspell-1.6-0.dll with the code snippet I gave earlier and I get "success" too.

What were the exact steps you took?
I did:
-----
Download and run Msys2

$ pacman -S base-devel mingw-w64-x86_64-toolchain mingw-w64-x86_64-libtool mingw-w64-x86_64-boost
$ exit

in Windows extract hunspell-master to your home directory of Msys2. For me that was D:\msys64\home\Rik
Run Msys2 again (you are already in your home directory, see ls -ltr)

$ cd hunspell-master
$ autoreconf -vfi
$ ./configure
$ make
$ exit

Now libhunspell-1.6-0.dll is created in D:\msys64\home\Rik\hunspell-master\src\hunspell\.libs
-----
Now put the libhunspell-1.6-0.dll in your program directory and the call to Hunspell_create() should not crash.
I had to copy libgcc_s_seh-1.dll, libstdc++-6.dll and libwinpthread-1.dll too because I didn't statically link them but that should be possible to do.

(My example with dlls is temporarily on my dropbox for the moment. You can use the download button at the top right if you want it.)


dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: load hunspell.dll
« Reply #22 on: February 10, 2018, 11:43:02 pm »
Thanks rvk, very grateful for you help here. But its getting a bit silly isn't it ?

Only thing I did differently during the MSYS2 stage was install gcc in MSYS2. It was installed during the MSYS2 install and that did surprise me a bit.

I assume your build of MSYS2 did also not get you a working gcc ?  Do you have gcc or some other c++ compiler already on the windows box ?  That could be an explanation for my problem ?
I'll grab your tarball rvk and see that that reveals.

No, I have not run make install at the end of the hunspell build process. My code is almost identical to yours now, except where I set a path to load the dll from.

taazz, I don't use windows in a vm because of licensing issues, I don't understand how I can get it to work without purchasing a license for that VM and then a whole lot of fuss every time I want to bring up a new VM.  I routinely use VMs for testing my app on various Linux distros ...
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: load hunspell.dll
« Reply #23 on: February 11, 2018, 12:24:40 am »
OK, can confirm that rvk's kit works perfectly.

If I plug in the hunspell and friends dlls that I made it all falls apart. Its a MSYS2 / hunspell  issue of some sort.

Could be related to fact that my install of MSYS2 did not arrive with a compiler ? Maybe I need to repeat that process.....

I also note that my dlls have names like -
Code: Pascal  [Select][+][-]
  1.  msys-hunspell-1.6-0.dll
and rvk's are like -
Code: Pascal  [Select][+][-]
  1. libhunspell-1.6-0.dll

So there might be a clue there perhaps ?  Clearly been build differently. Anyway, rvk, if you could, I'd like to know what c++ compiler you use when you run 'make' please ?

David
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: load hunspell.dll
« Reply #24 on: February 11, 2018, 12:28:33 am »
I assume your build of MSYS2 did also not get you a working gcc ?  Do you have gcc or some other c++ compiler already on the windows box ?  That could be an explanation for my problem ?
No, I don't have gcc installed on Windows. As I understand it msys2 is a complete separate virtual Linux/Cygwin environment so nothing is taken from Windows.

Gcc is also not installed as default in msys2 but the first command (that pacman line) installs base-devel and I think that includes the gcc compiler. I did need to exit msys2 and enter it again before I could run gcc.

Setting the path in Windows is not needed if you include the dlls in the .exe directory.

So the steps I gave should also work on a complete clean Windows installation to compile hunspell. And the gcc compiler came from the pacman base-devel line.

From where did you copy your compiler hunspell.dll? (From what directory in your msys2 install?)
« Last Edit: February 11, 2018, 12:31:00 am by rvk »

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: load hunspell.dll
« Reply #25 on: February 12, 2018, 09:29:09 am »
OK, so further testing proves everything works as I expected it would if I use the DLLs made by RVK.  Its quite clear that I did something different when I built mine and thats not a Lazarus issue, its purely a MSYS2 / Hunspell / Me issue and I'll sort it out at some stage.

RVK, to save me some immediate time, do you mind if I continue to use the DLLs you made for my (completely open source) project, tomboy-ng Notes ? https://github.com/tomboy-notes/tomboy-ng ?  Credit to "rvk on Lazarus Forum" ?

In either case, much thanks for your help !

I will mark this issue as solved....
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: load hunspell.dll
« Reply #26 on: February 12, 2018, 09:55:22 am »
No problem. You might want to include a libhunspell.txt file with the license or link to hunspell itself.

Something like:
Quote
libhunspell.dll was compiled with MSYS2 from the original source from
https://github.com/hunspell/hunspell

You must distribute this file with your program.

As it is now the dll also needs libstdc++-6.dll and other files. I've read that if I compile with -static-libgcc and -static-libstdc++ they would be statically linked in (so you wouldn't need them separately). I'll try that later today (or when I get some extra time).

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: load hunspell.dll
« Reply #27 on: February 12, 2018, 12:48:24 pm »
David, I couldn't easily statically link in the g++ libraries in MSYS2/MinGW64.
The whole configure/Makefiles were focused on linking them shared.

I finally took Visual Studio and set it to cross-compile to x64 with release_dll and it gave me a new (64 bit) libhunspell.dll of just 479KB. I tried to use it with LoadLibrary and Hunspell_create() and that worked without error (AND without dependencies).

Can you check if that libhunspell.dll does what it needs to do?
(temp directory on dropbox)

Quote
libhunspell.dll was compiled from the original source from
https://github.com/hunspell/hunspell
with Microsoft Visual Studio Community 2015.

You must distribute this file with your program.

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: load hunspell.dll
« Reply #28 on: February 13, 2018, 01:08:37 pm »
Right rvk, indeed that works !

And I thank you in no uncertain terms !

Now, here is what I will do, please yell and shout if it does not sound right. Two parts.

I'll bundle the DLL and license file into the zip file when I release my next Windows binary,  I will also put the DLL and license file in a special one off zip file  for just that release of tomboy-ng.

I'll write up what I have learnt about hunspell onto a Lazarus Wiki page (done a few of those already). It will cover Linux, Windows and Mac and I'll have a link from there back to the zip file (on github) with just the hunspell stuff.   

I'll also mention "rvk from Lazarus Forum" (?) in the license file. I'll call it license_hunspell.txt

I can see that DLL being used by other projects, I'm happy with that, assume you are too ?

Does that all sound OK ?

David
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: load hunspell.dll
« Reply #29 on: February 13, 2018, 01:22:31 pm »
Does that all sound OK ?
Hi David,

Yes, that all sound fine.

As extra you might want to mention, in the license file, that is is the 64-bit version of libhunspell.dll. People searching for it will see right away it's that version.

 

TinyPortal © 2005-2018