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

Add custom entry resolver when using DllImportAttribute #1906

Closed
temfordump opened this issue Jan 18, 2020 · 7 comments
Closed

Add custom entry resolver when using DllImportAttribute #1906

temfordump opened this issue Jan 18, 2020 · 7 comments
Labels
area-Interop-coreclr backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity

Comments

@temfordump
Copy link

temfordump commented Jan 18, 2020

It will be great to add method for setting custom entry resolver when loading native libraries using DllImportAttribute.

Example:

NativeLibrary.SetDllImportEntryResolver(Assembly.GetExecutingAssembly(), (libraryHandle, entryPoint) =>
{
    if (NativeLibrary.TryGetExport(libraryHandle, entryPoint, out IntPtr handle))
    {
        return handle;
    }

    if (NativeLibrary.TryGetExport(libraryHandle, "GetProcAddress", out IntPtr getProcAddressHandle))
    {
        var getProcAddress = Marshal.GetDelegateForFunctionPointer<GetProcAddressDelegate>(getProcAddressHandle);

        return getProcAddress.Invoke(entryPoint);
    }

    return IntPtr.Zero;
});
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-Interop-coreclr untriaged New issue has not been triaged by the area owner labels Jan 18, 2020
@jkotas
Copy link
Member

jkotas commented Jan 18, 2020

Could you please explain why you need to dynamically change the names of the native entrypoints? Why can't the code have the right names of the entrypoints in the first place?

@temfordump
Copy link
Author

I don't want to change the entry point dynamically.
For a example some of OpenGL static methods are extensions and are not exported and cannot be loaded using NativeLibrary.TryGetExport and is required to load wglGetProcAddress and call it to get the actual pointer to the method.

@temfordump
Copy link
Author

It can also be used to mock some missing/unsupported entry point.

@DaZombieKiller
Copy link
Contributor

I believe it would also be useful to pass in the DllImport's associated MethodInfo so you can use attributes to control how these resolvers would behave.

// use SDL_GL_GetProcAddress instead of wglGetProcAddress
[GLImportOptions(GLImportOptions.SDL), DllImport("OpenGL32")]
unsafe static extern void glGenFramebuffers(uint size, uint* frameBuffers);

@AaronRobinsonMSFT AaronRobinsonMSFT added this to the Future milestone Jan 22, 2020
@AaronRobinsonMSFT AaronRobinsonMSFT removed the untriaged New issue has not been triaged by the area owner label Jan 22, 2020
@jkotas
Copy link
Member

jkotas commented Apr 6, 2020

For a example some of OpenGL static methods are extensions and are not exported and cannot be loaded using NativeLibrary.TryGetExport and is required to load wglGetProcAddress and call it to get the actual pointer to the method.

You can do this today by wrapping the pointer using a delegate. C# 9 is adding support for function pointers that will allow it without the delegate overhead.

Copy link
Contributor

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of our issue cleanup automation.

@dotnet-policy-service dotnet-policy-service bot added backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity labels Oct 6, 2024
Copy link
Contributor

This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

@dotnet-policy-service dotnet-policy-service bot removed this from the Future milestone Oct 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Interop-coreclr backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity
Projects
Archived in project
Development

No branches or pull requests

5 participants