简体   繁体   中英

C# DLLimport with mono on ubuntu: not charging dll

Goodmorning to All I'm developing, with Visual Studio 2017, in C# language, an executable software, myExe.exe, that will be run on a PC with ubuntu 14.04 with mono I need to use a DLL, we can call it dll2import.dll, for work via TCP/IP with a specific device (like a driver....). Avoiding getting boring with several useless details I write only important info (but if you need feel free to ask) and a simple example.

In C++ dll2import.dll I have the first function to create handler for working with object, called d2iCreate, I wrap it in this way:

//private const String dllPath = @"D:\ExtDll2Use\dll2import.dll";
//private const String dllPath = "/home/massi/dll2import.dll";
private const String dllPath = "dll2import.dll";
[DllImport(dllPath,EntryPoint = "d2iCreate",CallingConvention = CallingConvention.Cdecl)]
unsafe public static extern UInt32 CSd2iCreate(); 

there are many other function that I wrap similar to this way, but the problem start from that, so it is not usefull write all...

When I run the .exe with my PC all works good!!! But when I put this application on my PC with ubuntu and try o running it with mono.. appears the problem!!! My code go in exception caling CSd2iCreate() !!! like it not found the library.... I try several path without success... I try to put library (and path) in my home directory, in my root, and where .exe is present.... but it is like mono not use it! It is very strange because if I delete dll2import.dll the error not change.. so it is like mono not found the .dll .... And I can tell you that I use other .dll and they work BUT that .dll are c# project developed in visual studio and linked inside my project.. So I suppose I must configure something inside mono or install that library in other way but I not know how!

The .dll is provided "as is" and I have not the source code... I can't recompile it...

If can be relevant I write here the error, but it is not very detailed, it is a generic one.

The code is that:

private unsafe bool CreateHandler() {
      try {
        pwxHandle = d2iCreate();
        return true;
      } catch(Exception ex) {
        Log.Errore(pwxProcLog,System.Reflection.MethodBase.GetCurrentMethod().Name,"Error creating Handler",ex);
        return false;
      }
    }

the routine for log error code is that (an extract):

public void Errore(string Proc, string Metodo, string info, Exception e)
{

        ScriviErr(Proc, Metodo, info);
        ScriviErr(Proc, "Eccezione " + e.Source, e.Message);
        ScriviErr(Proc, "Stack", Environment.NewLine + e.StackTrace + Environment.NewLine);        
    }
}

so the error logged is that:

14/11/2017-17.38.23 <WV1> - CreateHandler: [Error creating Handler]
14/11/2017-17.38.23 <WV1> - Eccezione MyProject2LinkDLL: [dll2import.dll]
14/11/2017-17.38.23 <WV1> - Stack: [
  at (wrapper managed-to-native) MyProject2LinkDLL.Perif:d2iCreate ()
  at MyProject2LinkDLL.Perif.CreateHandler () [0x00000] in <filename unknown>:0 
]

I can tell you that in this line:

14/11/2017-17.38.23 <WV1> - Eccezione MyProject2LinkDLL: [dll2import.dll]

between [] there is the path that I write for take library... so other time was that the error

14/11/2017-14.11.37 <WV1> - Eccezione MyProject2LinkDLL: [/home/massi/dll2import.dll]

if other details are needed feel free to ask!

Thanks, Massimiliano

Not a complete answer but I remember encountering this with a DLL I was reusing a long time ago. Essentially you have understand the architecture you are using:

Mono native uses.Net libraries, with a façade to call *nix functions for native interactions. Thus a P/Invoke call uses the operating system to try and load things. This means a windows compiled dll simply won't link properly as it would try to be loaded through the *nix load library calls.

There is a project within wine to compile the mono runtime against the wine libraries (essentially as .net is actually intended to run) but with the façade provided by the wine implementation. This means that you can invoke the executable within WINE as it will load (via wine) and link correctly against the wine libraries. I haven't used it so I don't know it's maturity Wine - Mono

This could work provided you don't touch some part of the unimplemented/non-working parts of the system and even then, there are so many moving parts you could run into all sorts of integration problems. Is the $100 license for XP going to break you just to run it on windows?

The article here has some highly detailed info P/Invoke Mono .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM