BCryptNative中有一个名为GetInt32Property的方法。它有以下签名:
internal static int GetInt32Property<T>(T algorithm, string property) where T : SafeHandle此方法仅在T为SafeBCryptAlgorithmHandle或SafeBCryptHashHandle类型时才有效。它调用使用以下句柄类型显式定义的本机方法:
[DllImport("bcrypt.dll", EntryPoint = "BCryptGetProperty", CharSet = CharSet.Unicode)]
internal static extern ErrorCode BCryptGetAlgorithmProperty(SafeBCryptAlgorithmHandle hObject,
string pszProperty,
[MarshalAs(UnmanagedType.LPArray), In, Out] byte[] pbOutput,
int cbOutput,
[In, Out] ref int pcbResult,
int flags);
[DllImport("bcrypt.dll", EntryPoint = "BCryptGetProperty", CharSet = CharSet.Unicode)]
internal static extern ErrorCode BCryptGetHashProperty(SafeBCryptHashHandle hObject,
string pszProperty,
[MarshalAs(UnmanagedType.LPArray), In, Out] byte[] pbOutput,
int cbOutput,
[In, Out] ref int pcbResult,
int flags);Microsoft使用函数指针/委托来指向正确的本机函数。我的问题是,为什么微软没有用以下签名实现GetInt32Property方法:
internal static int GetInt32Property(SafeHandle algorithm, string property)使用以下本机方法:
[DllImport("bcrypt.dll", CharSet = CharSet.Unicode)]
internal static extern ErrorCode BCryptGetProperty(SafeHandle hObject,
string pszProperty,
[MarshalAs(UnmanagedType.LPArray), In, Out] byte[] pbOutput,
int cbOutput,
[In, Out] ref int pcbResult,
int flags);这有什么坏处吗?(假设传递给GetInt32Property的GetInt32Property总是SafeBCryptAlgorithmHandle或SafeBCryptHashHandle)。
我只是想知道为什么微软这么复杂地实现了这一点。
是否与下列事项有关:
根据文档,类必须是继承的,但是,当给定SafeHandle的抽象类时,P/被调用的函数是否正确地处理它?它是否适当地增加和减少引用数?
发布于 2015-09-26 22:00:01
很难说为什么微软选择以这样或那样的方式实现某些东西,但我可以回答你的观点。
GetInt32Property(algorithm, str) )。SafeHandle。typeof(T) == typeof(SafeBCryptHashHandle)类型检查不是在运行时完成的,而是在JIT时间内完成的。这意味着该方法的执行速度应略快于常规运行时检查(如algorith is SafeBCrypthHashHandle )。SafeHandle类是一个抽象类。这意味着您不能创建它的实例,但可以继承它。本机函数只获得封送数据,它们无法获得对对象的实际引用。别担心推荐信计数。
https://stackoverflow.com/questions/32751585
复制相似问题