Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
BackT0TheFuture opened this issue Jul 31, 2018 · 9 comments

Comments

@BackT0TheFuture
Copy link

BackT0TheFuture commented Jul 31, 2018

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)

@HelgeL
Copy link

HelgeL commented Aug 13, 2018

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.

@HelgeL
Copy link

HelgeL commented Aug 29, 2018

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?

@stdcall
Copy link

stdcall commented Aug 31, 2018

I have the same problem on Fedora28.

@stdcall
Copy link

stdcall commented Aug 31, 2018

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.

@stdcall
Copy link

stdcall commented Aug 31, 2018

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

@HelgeL
Copy link

HelgeL commented Sep 3, 2018

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.

@charlesw
Copy link
Owner

charlesw commented Sep 5, 2018 via email

@fant0m
Copy link

fant0m commented Oct 26, 2018

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.

@Doomer3D
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants