【发布时间】:2015-09-18 00:57:58
【问题描述】:
我对 .Net 以外的了解非常有限 - 但我花了很多时间阅读许多相关文章。
HCRYPTPROV datatype documentaion 表示它是ULONG_PTR 类型。
以下参考建议使用IntPtr 对应于此。
- Is there a definitive guide to cross platform (x86 and x64) PInvoke and windows data types?
- Using MS crypto library on server 2012 - CryptCreateHash error code 87: ERROR_INVALID_PARAMETER
- Calling AuditQuerySystemPolicy() (advapi32.dll) from C# returns "The parameter is incorrect"
但是,在下面的代码中,我使用了long 数据类型,它工作得很好。是否有任何情况会给出不正确的结果?它与long 合作的原因是什么?
框架:.Net 2.0;
架构:64 位;
操作系统:Windows Server 2012 R2;
视觉工作室:2013
代码
Module Module1
Private Declare Function CryptAcquireContext Lib "advapi32.dll" _
Alias "CryptAcquireContextA" ( _
ByRef phProv As Long, ByVal pszContainer As String, ByVal pszProvider As String, _
ByVal dwProvType As Integer, ByVal dwFlags As Integer) As Integer
Private Declare Function CryptCreateHash Lib "advapi32.dll" (ByVal hProv As Long, _
ByVal Algid As Integer, ByVal hKey As Integer, ByVal dwFlags As Integer, _
ByRef phHash As Integer) As Integer
Private Declare Function GetLastError Lib "kernel32" () As Integer
Sub Main()
Dim sClearText As String
sClearText = "test1"
Dim lHCryptprov As Long
Dim sProvider As String
Const MS_DEF_PROV As String = "Microsoft Base Cryptographic Provider v1.0"
Dim lHHash As Integer
Dim sInputBuffer As String
Const ALG_CLASS_HASH As Integer = 32768
Const ALG_TYPE_ANY As Integer = 0
Const ALG_SID_MD5 As Integer = 3
Const PROV_RSA_FULL As Integer = 1
Const CRYPT_MACHINE_KEYSET As Integer = &H20
Const CALG_MD5 As Integer = ((ALG_CLASS_HASH Or ALG_TYPE_ANY) Or ALG_SID_MD5)
sInputBuffer = sClearText
'Get handle to the default CSP
sProvider = MS_DEF_PROV & vbNullChar
Dim errorCode As Integer
Dim r As Long
r = CryptAcquireContext(lHCryptprov, "", sProvider, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET)
errorCode = GetLastError()
Dim hashResult As Boolean
hashResult = CBool(CryptCreateHash(lHCryptprov, CALG_MD5, 0, 0, lHHash))
errorCode = GetLastError()
Console.WriteLine(hashResult)
Console.ReadLine()
End Sub
End Module
【问题讨论】:
-
可能有帮助:C/C++ 数据类型:IntPtr msdn.microsoft.com/en-us/library/windows/desktop/…
标签: .net vb.net marshalling native-code