dotnet / dotnet/dotnet-api-docs
Doc possibly not reflecting actual behaviour (Marshal.GetDelegateForFunctionPointer)
- Dominant language
- C#
- Stars
- 949
- Forks
- 1.7k
- Avg merge
- 3d 27m
- Merged PRs (30d)
- 49
Description
First issue:
Method GetDelegateForFunctionPointer(IntPtr, Type) is said to have [System.Obsolete] attribute, which is not present in my assembly (at file "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\mscorlib.dll")
Second issue:
Method GetDelegateForFunctionPointer(IntPtr, Type) has following restriction in Remarks section:
> - Generics are not supported in interop scenarios
And following entry in Exceptions section:
> [ArgumentException](https://docs.microsoft.com/en-us/dotnet/api/system.argumentexception?view=netframework-4.7.2)
> The `t` parameter is not a delegate or is generic.
However method GetDelegateForFunctionPointer(IntPtr) doesn't have this restriction and corresponding Exceptions entry is:
> [ArgumentException](https://docs.microsoft.com/en-us/dotnet/api/system.argumentexception?view=netframework-4.7.2)
> The `TDelegate` generic parameter is not a delegate, or it is an open generic type.
Which would mean that it accepts closed generic type argument, unlike the previous overload (IntPtr, Type).
However this is not the case, it throws exception when providing e.g. Func:
> System.ArgumentException: 'The specified Type must not be a generic type definition. Parameter name: t'.
Code to reproduce (Windows only, but funcPtr can be replaced with any other valid function pointer):
```
using System;
using System.Runtime.InteropServices;
namespace tests
{
class Program
{
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
static void Main(string[] args)
{
IntPtr libPtr = LoadLibrary("user32.dll");
var funcPtr = GetProcAddress(libPtr, "GetKeyState");
var del = Marshal.GetDelegateForFunctionPointer>(funcPtr); //exception
}
}
}
```
Moreover error message says that t must not be generic type definition, but calling `typeof(Func)` gives following properties about being generic:
- ContainsGenericParameters = false
- IsGenericType = true
- IsGenericTypeDefinition = false
---
#### Document Details
⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*
* ID: 453dbc98-c17d-b3e9-7b4e-264abb17cc94
* Version Independent ID: 0cec9d58-7fda-5433-a2ab-c068cf068292
* Content: [Marshal.GetDelegateForFunctionPointer Method (System.Runtime.InteropServices)](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.getdelegateforfunctionpointer?view=netframework-4.7.2)
* Content Source: [xml/System.Runtime.InteropServices/Marshal.xml](https://github.com/dotnet/dotnet-api-docs/blob/master/xml/System.Runtime.InteropServices/Marshal.xml)
* Product: **dotnet-api**
* GitHub Login: @dotnet-bot
* Microsoft Alias: **dotnetcontent**
Contributor guide
Assessment
This issue has not been assessed yet.