[Request] Permit unmanaged generics on extern functions marked with 'DLLImport' #3676
-
BackgroundA few days ago, I had to implement a P/Invoke-call to
Here, the " However, it got me thinking. What if one wants to pass RequestPermit generic parameters (constrained to [DllImport("my_library.dll")]
public static extern int MyMethod<T>(nint size, ref T data) where T : unmanaged; when e.g. calling the following function in a "safe" manner (meaning that in the code above, I do not need to use extern "C" __declspec(dllexport) int MyMethod(size_t size, void* data) { ... } I think that at least |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
(maybe the 'Backgorund'-section is a bit orthogonal to my request .... I just wanted to share my train of thought.) |
Beta Was this translation helpful? Give feedback.
-
IIRC |
Beta Was this translation helpful? Give feedback.
-
If this is your motivation, why not just wrap the P/Invoke? [DllImport("my_library.dll")]
private unsafe static extern int MyMethod(nint size, void* data);
public unsafe static int MyMethod<T>(nint size, ref T data)
where T : unmanaged
{
fixed (T* dataP = &data)
{
return MyMethod(size, dataP);
}
} |
Beta Was this translation helpful? Give feedback.
-
@PathogenDavid I don't think this
is safe. Yeah, you'll get the pointer, but the GC is still allowed to move Iif you know |
Beta Was this translation helpful? Give feedback.
-
@HaloFour: Damn! I completely forgot about @PathogenDavid You're right of course - wrapping it would be the easiest, most straight-forward solution. I just wanted an excuse to lift this restriction of not being able to use generics on |
Beta Was this translation helpful? Give feedback.
-
You're right, that was a dangerous brain fart on my part. Fixed the example. |
Beta Was this translation helpful? Give feedback.
IIRC
__arglist
can be used to call unmanaged variadic imports like this. I recall using it on some Win32 API like wsprintf.