-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Interop request: Support exported variable symbols #10929
Comments
@MV10 This is an interesting idea, but I think there are some serious issues with anything that needs to be marshaled. In the example in the link provided I can imagine how Could you provide an example scenario - just so we have an idea on how to think about this? edit Also what kind of memory guarantees would you expect? If we have to marshal something it, by definition, is no longer the same as what it was marshaled from. |
As a way of exploring cross-platform development, I began yet-another-wrapper for the ncurses console-output library. It exports a couple of integers that reflect the size of the terminal as public static int Lines => GetInt("LINES");
public static int Columns => GetInt("COLS");
private static int GetInt(string symbolName)
{
var handle = GetModuleHandle(CursesLibName);
var symaddr = GetProcAddress(handle, symbolName);
return (int)Marshal.PtrToStructure(symaddr, typeof(int));
} Obviously it would be smart to store the module address, and maybe the symbol address (my Win32/C/C++ is rusty, can't remember if that would be safe to do without doing some Google work). I imagined something along these lines: [DllImport(MarshalAs=typeof(int))]
public static int Lines; I realize this is basically read-only access, whereas others might expect two-way. I haven't really thought about it in those terms. |
As an aside, I'm curious about why this discussion belongs in coreclr instead of corefx where the runtime interop code lives. When it comes to picking .net repos for new issues, my hit-miss ratio isn't great... |
The runtime interop code lives in CoreCLR.
No worries. |
This would be a useful feature for HDF.PInvoke. The native HDF5 C library it wraps exports values representing certain default types (akin to .NET's That platform-specific logic could go away if |
This can be done in platform neutral way using System.Runtime.InteropServices.NativeLibrary APIs introduced in .NET Core 3.0. |
From @MV10 on August 19, 2018 17:5
Request: Define a new attribute which maps a managed variable to an exported symbol, exactly as we get today using
[DllImport]
for functions.Right now I'm facing the need to read data (defined constants and live variables) from Windows, Linux, and OSX native libraries. At the moment I'm not sure how to accomplish the same thing in Linux and OSX (I've just started down this rabbit-hole), but it definitely feels like the type of "it just works" magic that .NET ought to handle for us, because this gets ugly fast.
This is the Win32 way (although in my case I think I can skip
LoadLibrary
andFreeLibrary
and just useGetModuleHandle
since pinvoke will have already loaded it):https://limbioliong.wordpress.com/2011/11/11/accessing-exported-data-from-a-dll-in-managed-code/
I suppose I'll have to target each OS with
#if
blocks based on Runtime Identifier build constants once I figure out how it works on the other platforms.(On a related note, I noticed in dotnet/coreclr#24444 the OP asks about data and interfaces but it seems the data portion of the question was overlooked.)
Copied from original issue: dotnet/corefx#31836
The text was updated successfully, but these errors were encountered: