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

Method calling fails after loading and also multiple instances issue #12

Open
gl2007 opened this issue Jan 30, 2022 · 7 comments
Open
Labels
bug Something isn't working

Comments

@gl2007
Copy link

gl2007 commented Jan 30, 2022

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?

@trungnt2910
Copy link
Owner

trungnt2910 commented Jan 31, 2022

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).

@gl2007
Copy link
Author

gl2007 commented Jan 31, 2022

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

@trungnt2910
Copy link
Owner

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.

@trungnt2910
Copy link
Owner

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.

@trungnt2910 trungnt2910 added the bug Something isn't working label Jan 31, 2022
@gl2007
Copy link
Author

gl2007 commented Jan 31, 2022

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

@gl2007
Copy link
Author

gl2007 commented Jan 31, 2022

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!

@trungnt2910
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants