{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/19112","repository_url":"https://api.github.com/repos/dotnet/coreclr","labels_url":"https://api.github.com/repos/dotnet/coreclr/issues/19112/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/19112/comments","events_url":"https://api.github.com/repos/dotnet/coreclr/issues/19112/events","html_url":"https://github.com/dotnet/coreclr/issues/19112","id":344060418,"node_id":"MDU6SXNzdWUzNDQwNjA0MTg=","number":19112,"title":"Lightweight and dynamic driving of P/Invoke","user":{"login":"migueldeicaza","id":36863,"node_id":"MDQ6VXNlcjM2ODYz","avatar_url":"https://avatars2.githubusercontent.com/u/36863?v=4","gravatar_id":"","url":"https://api.github.com/users/migueldeicaza","html_url":"https://github.com/migueldeicaza","followers_url":"https://api.github.com/users/migueldeicaza/followers","following_url":"https://api.github.com/users/migueldeicaza/following{/other_user}","gists_url":"https://api.github.com/users/migueldeicaza/gists{/gist_id}","starred_url":"https://api.github.com/users/migueldeicaza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/migueldeicaza/subscriptions","organizations_url":"https://api.github.com/users/migueldeicaza/orgs","repos_url":"https://api.github.com/users/migueldeicaza/repos","events_url":"https://api.github.com/users/migueldeicaza/events{/privacy}","received_events_url":"https://api.github.com/users/migueldeicaza/received_events","type":"User","site_admin":false},"labels":[{"id":244582102,"node_id":"MDU6TGFiZWwyNDQ1ODIxMDI=","url":"https://api.github.com/repos/dotnet/coreclr/labels/area-Interop","name":"area-Interop","color":"d4c5f9","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/dotnet/coreclr/milestones/18","html_url":"https://github.com/dotnet/coreclr/milestone/18","labels_url":"https://api.github.com/repos/dotnet/coreclr/milestones/18/labels","id":3246413,"node_id":"MDk6TWlsZXN0b25lMzI0NjQxMw==","number":18,"title":"3.0","description":".NET Core 3.0, next major release after 2.1. See [official release dates](https://github.com/dotnet/core/blob/master/roadmap.md) for details.\r\n\r\nWe will move important issues from Future to 3.0 as the priorities and dates for next release become more clear.","creator":{"login":"karelz","id":5776068,"node_id":"MDQ6VXNlcjU3NzYwNjg=","avatar_url":"https://avatars2.githubusercontent.com/u/5776068?v=4","gravatar_id":"","url":"https://api.github.com/users/karelz","html_url":"https://github.com/karelz","followers_url":"https://api.github.com/users/karelz/followers","following_url":"https://api.github.com/users/karelz/following{/other_user}","gists_url":"https://api.github.com/users/karelz/gists{/gist_id}","starred_url":"https://api.github.com/users/karelz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/karelz/subscriptions","organizations_url":"https://api.github.com/users/karelz/orgs","repos_url":"https://api.github.com/users/karelz/repos","events_url":"https://api.github.com/users/karelz/events{/privacy}","received_events_url":"https://api.github.com/users/karelz/received_events","type":"User","site_admin":false},"open_issues":145,"closed_issues":167,"state":"open","created_at":"2018-04-05T22:09:22Z","updated_at":"2018-08-10T00:24:00Z","due_on":null,"closed_at":null},"comments":29,"created_at":"2018-07-24T14:22:42Z","updated_at":"2018-08-10T12:49:30Z","closed_at":null,"author_association":"MEMBER","body":"While Mono has `<dllmap>` and there is a long-standing discussion going on in https://github.com/dotnet/coreclr/issues/930 and efforts like https://github.com/dotnet/corefx/issues/17135 to work around the limitations of P/Invoke and even [`NativeLibrary`](https://github.com/dotnet/coreclr/pull/16409) was introduced, I feel that we could come up with a simple solution that leverages the existing P/Invoke capabilities, without the overhead and ugly machinery that comes from variations of `GetDelegateForFunctionPointer` and similar hacks that have been attempted to work around the limitations of P/Invoke.\r\n\r\nThe proposal is to add an API to inform the runtime how we where we want a particular file referenced in the `DllImport` attribute to be loaded from.   Developers would then annotate their `DllImport` attributes with a custom name, and at startup, their own logic would determine which library to load.\r\n\r\nFor example:\r\n\r\n```csharp\r\n//\r\n// We declare our DllImport, and by convention we use reverse domains, to avoid clashes:\r\n//\r\nusing System.Runtime.InteropServices;\r\n\r\n[DllImport (\"github.com/migueldeicaza/gui.cs/curses\")]\r\nextern static void initscr ();\r\n\r\n// At startup, we decide what we want to do.\r\nstatic void Main ()\r\n{\r\n    string library = \"github.com/migueldeicaza/gui.cs/curses\";\r\n\r\n    if (File.Exists (\"/usr/lib/libncurses_6.so\"))\r\n        PInvoke.RegisterLibrary (library, \"/usr/lib/libncurses_6.so\");\r\n   else if (File.Exists (\"/usr/lib/libncurses_7.so\"))\r\n        PInvoke.RegisterLibrary (library, \"/usr/lib/libncurses_7.so\"); \r\n   else\r\n        throw ();\r\n}\r\n```\r\n\r\nWorking around this today requires ugly hacks, from Gui.cs having doubled definitions, to Grpc generating proxies and entirer class hierarchies and tons of delegates to achieve the desired effect.   And the result produces more junk than the current P/Invoke does.\r\n\r\n```Bonus points:``` we could make it so that the string passed to DllImport could have parameters, similar in spirit to say a ConnectionString in SQL, so we could provide defaults, or even simple inline switching that is evaluated at resolution time.\r\n\r\nFor example:\r\n\r\n```csharp\r\n// Built-in switching capabilities, similar to Dllmap:\r\n[DllImport (\"(switch 'osMac:libSystem 'osLinux:libc 'osWindows:user32)\")]\r\n\r\n// Define a key that can be referenced by PInvoke.RegisterLibrary, but also provide a default if the API is not called\r\n[DllImport (\"(key 'github.com/migueldeicaza/gui.cs)(default 'ncurses)\")]\r\n```\r\n\r\nWhile I can certainly add a bag of hacks to gui.cs (for forked, different naming versions) and TensorFlowSharp (for CPU vs GPU, vs various SIMD operation builds), none of those libraries are particularly affected by the transition speed.   But it would be a shame if we did not implement something for every other user that needs to cope with different bits of native code, but does not want to pay the performance price of the `GetDelegateFrom...`\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/comments/412072459","html_url":"https://github.com/dotnet/coreclr/issues/19112#issuecomment-412072459","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/19112","id":412072459,"node_id":"MDEyOklzc3VlQ29tbWVudDQxMjA3MjQ1OQ==","user":{"login":"damageboy","id":125730,"node_id":"MDQ6VXNlcjEyNTczMA==","avatar_url":"https://avatars3.githubusercontent.com/u/125730?v=4","gravatar_id":"","url":"https://api.github.com/users/damageboy","html_url":"https://github.com/damageboy","followers_url":"https://api.github.com/users/damageboy/followers","following_url":"https://api.github.com/users/damageboy/following{/other_user}","gists_url":"https://api.github.com/users/damageboy/gists{/gist_id}","starred_url":"https://api.github.com/users/damageboy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/damageboy/subscriptions","organizations_url":"https://api.github.com/users/damageboy/orgs","repos_url":"https://api.github.com/users/damageboy/repos","events_url":"https://api.github.com/users/damageboy/events{/privacy}","received_events_url":"https://api.github.com/users/damageboy/received_events","type":"User","site_admin":false},"created_at":"2018-08-10T12:49:30Z","updated_at":"2018-08-10T12:49:30Z","author_association":"NONE","body":"No I don't, I can use `dotnet publish` with FDD deployment"}}
{"actor":{"display_login":"damageboy"}}