{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/930","labels_url":"https://api.github.com/repos/dotnet/coreclr/issues/930/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/930/comments","events_url":"https://api.github.com/repos/dotnet/coreclr/issues/930/events","html_url":"https://github.com/dotnet/coreclr/issues/930","id":73388010,"number":930,"title":"Handling p/invokes for different platforms and discussions about dllmap","user":{"login":"akoeplinger","id":1376924,"avatar_url":"https://avatars.githubusercontent.com/u/1376924?v=3","gravatar_id":"","url":"https://api.github.com/users/akoeplinger","html_url":"https://github.com/akoeplinger","followers_url":"https://api.github.com/users/akoeplinger/followers","following_url":"https://api.github.com/users/akoeplinger/following{/other_user}","gists_url":"https://api.github.com/users/akoeplinger/gists{/gist_id}","starred_url":"https://api.github.com/users/akoeplinger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akoeplinger/subscriptions","organizations_url":"https://api.github.com/users/akoeplinger/orgs","repos_url":"https://api.github.com/users/akoeplinger/repos","events_url":"https://api.github.com/users/akoeplinger/events{/privacy}","received_events_url":"https://api.github.com/users/akoeplinger/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/coreclr/labels/interop","name":"interop","color":"bfdadc"},{"url":"https://api.github.com/repos/dotnet/coreclr/labels/port","name":"port","color":"bfd4f2"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/dotnet/coreclr/milestones/5","html_url":"https://github.com/dotnet/coreclr/milestones/Future","labels_url":"https://api.github.com/repos/dotnet/coreclr/milestones/5/labels","id":1328081,"number":5,"title":"Future","description":"Milestone tracking the issues that have been triaged for after 1.0.0-rtm","creator":{"login":"joshfree","id":1302850,"avatar_url":"https://avatars.githubusercontent.com/u/1302850?v=3","gravatar_id":"","url":"https://api.github.com/users/joshfree","html_url":"https://github.com/joshfree","followers_url":"https://api.github.com/users/joshfree/followers","following_url":"https://api.github.com/users/joshfree/following{/other_user}","gists_url":"https://api.github.com/users/joshfree/gists{/gist_id}","starred_url":"https://api.github.com/users/joshfree/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joshfree/subscriptions","organizations_url":"https://api.github.com/users/joshfree/orgs","repos_url":"https://api.github.com/users/joshfree/repos","events_url":"https://api.github.com/users/joshfree/events{/privacy}","received_events_url":"https://api.github.com/users/joshfree/received_events","type":"User","site_admin":false},"open_issues":187,"closed_issues":4,"state":"open","created_at":"2015-09-29T00:26:07Z","updated_at":"2015-12-03T23:17:05Z","due_on":null,"closed_at":null},"comments":173,"created_at":"2015-05-05T18:17:41Z","updated_at":"2015-12-03T23:23:42Z","closed_at":null,"body":"Right now, coreclr has no good way to handle the differences between platforms when it comes to p/invoking native libraries.\r\n\r\nE.g. imagine I want to call an openssl function, on Unix the library is called `libssl.so/dylib` but on Windows it is `libeay32.dll` (there are countless other examples, e.g. `libz.so` vs `zlib1.dll`).\r\n\r\ncorefx \"solves\" this by conditionally compiling the p/invoke code for each platform with the correct library name. This seems like a major organizational overhead though and is brittle when new platforms are added.\r\n\r\nMono does two things to make this much easier:\r\n\r\n1. It automatically probes for variations of the string passed to `DllImport`, e.g. if I specify `DllImport(\"myFoo\")` it tries `myFoo.dll`, `myFoo.so`, `libmyFoo.so` and so on. In the happy case this is enough to find the library on all the platforms.\r\n2. [DllMap](http://www.mono-project.com/docs/advanced/pinvoke/dllmap/): this is for cases where the library name is just too different, or if only a certain function should be redirected, or only on some specific platforms.\r\n\r\nIn my opinion, coreclr should implement something along those lines. I know that using DllMap 1:1 is not really possible since there's no System.Configuration stack in .NET Core, but it should at least guide an alternate implementation.\r\n\r\nWhat do you think?\r\n\r\n**edit** I proposed adding DllMap attributes in https://github.com/dotnet/coreclr/issues/930#issuecomment-100675743 :\r\n\r\n``` csharp\r\n[assembly: DllMap(\"foo\", \"foo.dll\", OSName.Windows)]\r\n[assembly: DllMap(\"foo\", \"libfoo.dylib\", OSName.OSX)]\r\n[assembly: DllMap(Dll=\"foo\", Target=\"libfoo.so\", OS=OSName.Linux)]\r\n[assembly: DllMap(Dll=\"dl\", Name=\"dlclose\", Target=\"libc.so\", OS=\"FreeBSD\")]\r\n```"},"comment":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/comments/161821883","html_url":"https://github.com/dotnet/coreclr/issues/930#issuecomment-161821883","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/930","id":161821883,"user":{"login":"jasonwilliams200OK","id":2031765,"avatar_url":"https://avatars.githubusercontent.com/u/2031765?v=3","gravatar_id":"","url":"https://api.github.com/users/jasonwilliams200OK","html_url":"https://github.com/jasonwilliams200OK","followers_url":"https://api.github.com/users/jasonwilliams200OK/followers","following_url":"https://api.github.com/users/jasonwilliams200OK/following{/other_user}","gists_url":"https://api.github.com/users/jasonwilliams200OK/gists{/gist_id}","starred_url":"https://api.github.com/users/jasonwilliams200OK/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jasonwilliams200OK/subscriptions","organizations_url":"https://api.github.com/users/jasonwilliams200OK/orgs","repos_url":"https://api.github.com/users/jasonwilliams200OK/repos","events_url":"https://api.github.com/users/jasonwilliams200OK/events{/privacy}","received_events_url":"https://api.github.com/users/jasonwilliams200OK/received_events","type":"User","site_admin":false},"created_at":"2015-12-03T23:23:42Z","updated_at":"2015-12-03T23:23:42Z","body":"Some concepts for cross-plat support can be borrowed/inspired from mono, Ruby's `foreign function interface`: https://github.com/ffi/ffi/wiki, node.js FFI proposal: https://github.com/nodejs/node/pull/1750 (they are currently dealing with it the hard way: https://github.com/nodejs/node-gyp/issues/629).\r\n\r\nAccording to [Wikipedia](https://en.wikipedia.org/wiki/Foreign_function_interface) (*yes wikiepdia is still a thing..*):\r\n\r\n> Java refers to its FFI as the JNI ([Java Native Interface](https://en.wikipedia.org/wiki/Java_Native_Interface)) or JNA ([Java Native Access](https://en.wikipedia.org/wiki/Java_Native_Access))\r\n\r\nwhich maybe another source of inspiration in bringing cross-platform P/Invoke in CoreCLR."}}