trungnt2910/MemoryModule.NET

Method calling fails after loading and also multiple instances issue

gl2007 opened this issue · 7 comments

hi, I am trying to use your library to run a legacy dll in multiple threads to avoid global data overwrite.

issue1: method call succeeds if using dllimport, but fails for the dll loaded with this library even though getdelegate works fine.

e.g. new declaration:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void StartSession(out int SessionId);
old declaration:
[DllImport("xyz.dll")]
private static extern void StartSession(out int sessionId);

issue2: if I copy dll into 2 and rename both to dll-1.dll and dll-2.dll, the first one succeeds loading while the second one fails at
"// notify library about attaching to process
bool successfull /sic/ = DllEntry(code, Dll.ProcessAttach, null);" in your NativeAssemblyImpl.cs.

Do you have any advice of how to fix both of these issues?

Note: looks like I need to use WINDOWS symbol but then I get compilation error?

Hmm, can you provide an example project that I can reproduce?

Also, check if your library uses thread-local storage, they are still not properly supported on Windows (even with the original MemoryModule, check this issue).

Hmm, can you provide an example project that I can reproduce?
Find attached file. Rename to .7z as it was not allowing. I don't think the dll I am using uses any TLS. Would your library try to load the dll's at the same address or it depends on the dll name? Was wondering if I create a copy with a different name and load it, will that work?

Forgot to mention, this dll uses some huge data files which are needed for its operation but l don’t know if I can share those out.
ZP4LoaderDemo.gz

image

Without data files I cannot check whether your app worked as expected.

Try using the latest stable version from NuGet, here.

Using the source from master may result in some bugs while loading TLS callbacks.
I'm trying to resolve all of this in the new abstractions branch, wait until it gets released for a fix.

The program '[19264] ZP4LoaderDemo.exe' has exited with code 3221225477 (0xc0000005) 'Access violation'.
The program '[19264] ZP4LoaderDemo.exe: Program Trace' has exited with code 0 (0x0).

I have reproduced the error using the latest prerelease. Clearly the latest packages contain some regressions.

Once again, I advise you to use the latest stable version from NuGet, and avoid using any thread-local storage (the C++11 thread_local thingy). If you do have to do thread-local stuff, use a callback to managed C# code or use MinGW's pthread instead, that is known to be working with MemoryModule.

I switched to using the NuGet package. The issue#1 remains the same. See the attached image. (Issue#2 somehow runs now.)

With DllImport the below function and other declared functions succeed.
[DllImport(ZP4Dll)]
private static extern void ZP4StartSession(out int SessionId); //others are similar

The above example function declared as below doesn't work:
private static int SessionId;
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void ZP4StartSession(out int SessionId);

So, I am having a hard time figuring out what is going wrong. I tried multiple variations with IntPtr, UIntPtr, out IntPtr, adding unsafe to declaration and so on, all resulting in same error.
error

Update: I used the DotNetAccess code here and it works. So, not sure what is different in your library, so for now, I will settle with copy/loading from disk.

Thank you for your responses!

There is a known issue with function pointer resolution that has been fixed in a517af1.

If you are still interested, you might want to try the latest dev package (version 0.2.0-dev.52).

I've tried it with your demo project, things seem to work.
If it works on your side, please let me know.