Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

NativeCallableAttribute Issue #1506

Closed
Closed

Description

@wjk

While brainstorming ideas on how to workaround Marshal.GetFunctionPointerForDelegate() being unimplemented, I came up with the idea of using NativeCallableAttribute to give the method in question a C name, and then use a C stub method (exposed to C# as [DllImport("*")] method) to convert the name of the function into its pointer, which then can (hopefully) be passed safely into Win32.

Unfortunately, ILC is not emitting the entry-point thunk for the method in question. The symbol does not appear in the emitted object file, and as such the linker fails when I try to link in the name-to-pointer stub method. I can prove the method itself is compiled (both by running dumpbin /symbols on my object and by examining the verbose ILC output); the NativeCallableAttribute is just not honored. Weirdly, though, the NativeCallable functions in the CoreRT runtime are exported (and visible in dumpbin /symbols) -- good thing, too, or else my app would emit far more linker errors than it does.

If it helps, I am defining System.Runtime.InteropServices.NativeCallableAttribute in a separate assembly than System.Private.CoreLib, because this makes it easier for me to reference the type from my application code. Having reviewed ILC's handling of this type, I do not think that the assembly containing the attribute is considered, but I could be wrong.

Activity

  1. jkotas commented on Jul 12, 2016

    @jkotas
    Member

    It should work.

    I would set at breakpoint here to check why your custom methods are not getting picked up:

  2. wjk commented on Jul 12, 2016

    @wjk
    ContributorAuthor

    I figured it out, actually. It seems that only the module containing the application's entry point and System.Private.CoreLib are scanned for methods marked with NativeCallableAttribute. @jkotas I can prepare a PR with a fix for this, if desired.

  3. wjk commented on Jul 12, 2016

    @wjk
    ContributorAuthor

    Closing as resolved; please see #1510 for the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions