Tesseract: Marshaling to and from COM interface pointers isn't supported (.net core 2.1)

Created on 31 Jul 2018  Â·  9Comments  Â·  Source: charlesw/tesseract

centos7 64bit .net core2.1
exception was thrown when running

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter #2': Invalid managed/unmanaged type combination (Marshaling to and from COM interface pointers isn't supported).
at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointerInternal(IntPtr ptr, Type t)
at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer(IntPtr ptr, Type t)
at InteropRuntimeImplementer.TessApiSignaturesInstance.TessApiSignaturesImplementation..ctor(LibraryLoader loader)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at InteropDotNet.InteropRuntimeImplementer.CreateInstanceT
at Tesseract.Interop.TessApi.Initialize()
at Tesseract.Interop.TessApi.get_Native()
at Tesseract.TesseractEngine..ctor(String datapath, String language, EngineMode engineMode, IEnumerable1 configFiles, IDictionary2 initialOptions, Boolean setOnlyNonDebugVariables)
at TesseractCore.Program.Main(String[] args)

All 9 comments

First, this is great work and a super quality library!

I'm also very interested in getting this to run on Linux using .NET Core. Apparently I'm getting the same error message, but to get that far I had to do some fiddling.

1) I am trying this from feature branch 321-Tesseract-4. My test environment is Ubuntu 18.04 x64 with .NET Core 2.1.
2) Next question was how to provide the native tesseract libraries for the Tesseract.dll wrapper. So to prevent it from asking for the specifically named library files, I changed

        public const string LeptonicaDllName = "liblept1760";
        public const string TesseractDllName = "libtesseract400";

to

        public const string LeptonicaDllName = "liblept";
        public const string TesseractDllName = "libtesseract";

so as a result it would accept libtesseract.so as provided by Ubuntu.
3) I also tried this on a Mac running High Sierra. Tesseract built from source (4.0.0 beta 3). After changing the .so extensions to .dylib in UnixLibraryLoaderLogic.cs (line 58 onwards), I finally got the same error message.

Unfortunately I don't know enough to fix this or work on this issue. If there's anything I could possibly test or do to support this effort, please let me know.

I tried to dig further into the issue. Sorry if any of this sounds dumb, but I don't fully understand what's going on and can only describe with my solid half-knowledge.

So here it goes. When the required native libraries are loaded, handles for the library and all required method calls are obtained. This seems to succeed for Leptonica, however for Tesseract it fails.

Here's the trace output before it crashes:

INFO: Trying to load native function "TessBaseAPIGetThresholdedImage" from the library with handle 140712712753840...
INFO: Successfully loaded native function "TessBaseAPIGetThresholdedImage", function handle = 4488118128.
INFO: Trying to load native function "TessBaseAPIProcessPages" from the library with handle 140712712753840...
INFO: Successfully loaded native function "TessBaseAPIProcessPages", function handle = 4488118416.
INFO: Trying to load native function "TessBaseAPIProcessPage" from the library with handle 140712712753840...
INFO: Successfully loaded native function "TessBaseAPIProcessPage", function handle = 4488118432.

Looking at TessBaseAPIProcessPage, it maps to BaseAPIProcessPage in BaseApi.cs like so:

[RuntimeDllImport(Constants.TesseractDllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "TessBaseAPIProcessPage")]
int BaseAPIProcessPage(HandleRef handle, Pix pix, int page_index, string filename, string retry_config, int timeout_millisec, HandleRef renderer);

Now I'd suspect that pix reference to be the culprit. It is the second parameter (Cannot marshal 'parameter #2': Invalid managed/unmanaged type combination (Marshaling to and from COM interface pointers isn't supported).) Further it seems those functions are loaded in order of appearance in BaseApi.cs. That pix reference is the first occurrence of a non-trivial class (e.g. not a base type, not an enum) of those calls. Does that make sense?

Could it be a bug in .NET Core on Unix?

I have the same problem on Fedora28.

Dear HelGeL, could you please also provide how did you obtain Trace output? I've failed to figure out what should i do in order to see the trace.

Well, my code started to work after commenting BaseAPIProcessPage declaration.

Thanks for info stdcall! I tried that too and now the code works on Unix.

Btw I got to read the Trace output by just putting Console.WriteLines in Logger.cs.

Yes thanks for the info I'll have a look at why that call fails and see if
we can get a linux build together.

On Tue., 4 Sep. 2018, 03:16 HelgeL, notifications@github.com wrote:

Thanks for info stdcall! I tried that too and now the code works on Unix.

Btw I got to read the Trace output by just putting Console.WriteLines in
Logger.cs.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/charlesw/tesseract/issues/433#issuecomment-418164476,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAPzyLRHCIWPj2YBMNtB06uLVJspp_ipks5uXWQFgaJpZM4VoNQB
.

Probably same problem. I'm getting this error on Ubuntu 16.04., (ASP).NET Core, Tesseract 4
System.DllNotFoundException: Failed to find library "liblept1760.so" for platform x64.

I made nuget package for 4.0 tesseract version with fixed API. Tested on CentOS 7.
Genesis.Tesseract4

Was this page helpful?
0 / 5 - 0 ratings