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

Dynamic DLL Import - Runtime PInvoke #1349

Closed
ryios opened this issue Mar 14, 2018 · 3 comments
Closed

Dynamic DLL Import - Runtime PInvoke #1349

ryios opened this issue Mar 14, 2018 · 3 comments

Comments

@ryios
Copy link

ryios commented Mar 14, 2018

Dynamic DLL Import - Runtime PInvoke

Is it currently possible to do run time loading of native modules in .Net Core? I know it's possible to use [DllImport], but I can't seem to find anything that would allow one to load a native Dll or Library at runtime.

I've run into a few scenarios where I want to detect at runtime if a native api is present and then be able to load support for that api on the fly as needed, if it's present. I.e. GitLib2 and enabling git support in my application if Git is present.

I think it would incredibly handy for game libraries as well, pinvoking into vulkan at runtime and or being able to switch to a directx wrapper on windows at runtime.

I've deduced how this can be built now, but I wanted to inquire if it exists now or is on the road map?

To build it now, I would imagine using [DllImport] to LoadLibrary on windows and dlOpen on Linux and using conditional compilation to build separate os specific versions of the ""Loader"" and then targetting the nuget package for deployment with specific assemblies per environment. On linux calls to "XYZLoader.Loader" would use DLOpen etc, and on windows it would use LoadLibrary.

@svick
Copy link
Contributor

svick commented Mar 14, 2018

I believe what you're asking does not currently exist, but is planned (though it won't make it into .Net Core 2.1). See the issue https://github.com/dotnet/corefx/issues/17135 and the pull request dotnet/coreclr#16409 (which is currently on hold).

In the meantime, you might be able to use the prototype implementation, which indeed uses LoadLibrary or dlopen, depending on the OS. Though you don't need conditional compilation to make that distinction, you can use RuntimeInformation.IsOSPlatform() at runtime for that

@ryios
Copy link
Author

ryios commented Mar 14, 2018

Thanks, exactly the information I was looking for, huge help. The prototype implementation is pretty close to what I was envisioning, I'll use/tweak that for now until it eventually makes it's way to the framework.

@Petermarcu
Copy link
Member

Sounds like this issue isn't needed to track anything. Glad you found the info you needed.

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

3 participants