CryptAcquireContext Lib "advapi32"

S

Stephen Rasey

I have a new computer in which I installed Office 2003, then Office 2007,
retaining the 2003 programs.

An Excel application I use has a set of user functions that make use of the
CryptAcquireContext and CryptCreateHash funtions to create a hash on a set
of Excel Ranges. The purpose is to create a unique, single cell value to
identifiy whether any changes have been made to several hundred cells used
as hash input.

In this new computer, the CryptAcquireContext is returning zero, not some
random-type long integer.

My guess is that I have an incorrect advapi32.dll installed, or Excel is
confused where to look for it. The two versions of Excel installed might
have caused the confusion.
Currently, there are 4 copies of advape32.dll on this computer:
<pre>
C:\Windows\system32 603 KB 3/15/2006
C:\SWSetup\MSM2006S\IE\Setupw95.cab 41KB 8/29/2002
C:\Program Files\Online services\PeoplePC\IE\EN\SetupW95.CAB 41KB
8/29/2002
C:\SWSetup\MSWorks\US\REDIST\IE6\SETUPW95.CAB 41KB
8/29/2002
</pre>
I have performed a repair on the Office 2003

Before I un-install everything and try things one at a time, I want to ask
if anyone has some simple fix to suggest.

'a Hash Function from Stephen Bullen.
'From: Stephen Bullen ([email protected])
'Subject: Re: Hash (MD5) in Excel
'Newsgroups: microsoft.public.Excel.programming Date: 2004-02-05
12:50:20 PST
'Modified (bug fix) by Stephen Rasey June 2004

Option Explicit

Declare Function CryptAcquireContext Lib "advapi32" Alias
"CryptAcquireContextA" (ByRef hProv As Long, ByVal sContainer As String, _
ByVal sProvider As String, ByVal lProvType As Long, ByVal lFlags As
Long) As Long

Declare Function CryptCreateHash Lib "advapi32" (ByVal hProv As Long, ByVal
lALG_ID As Long, _
ByVal hKey As Long, ByVal
lFlags As Long, ByRef hhash As Long) As Long

Declare Function CryptHashData Lib "advapi32" (ByVal hhash As Long, ByVal
lDataPtr As Long, ByVal lLen As Long, ByVal lFlags As Long) As Long

Declare Function CryptGetHashParam Lib "advapi32" (ByVal hhash As Long,
ByVal lParam As Long, ByVal sBuffer As String, _
ByRef lLen As Long, ByVal
lFlags As Long) As Long

Declare Function CryptDestroyHash Lib "advapi32" (ByVal hhash As Long) As
Long

Declare Function CryptReleaseContext Lib "advapi32" (ByVal hProv As Long,
ByVal lFlags As Long) As Long

Const MS_DEF_PROV = "Microsoft Base Cryptographic Provider v1.0"
Const PROV_RSA_FULL As Long = 1
Const CRYPT_NEWKEYSET As Long = 8
Const CALG_MD5 As Long = 32771
Const HP_HASHVAL As Long = 2



' wwGetMD5Hash - GetMD5Hash (written by Stephen Bullen) modified by
Stephen Rasey 040612
' Changes: Use StrPtr and coerse all cell values to strings
' Empty cells are not ignored, but the cell number in the
range
' is used to generate more data for the hash

Public Function wwGetMD5Hash(rngData As Range) As String

Dim hProv As Long
Dim hhash As Long
Dim lLen As Long
Dim ocell As Range
Dim baData() As Byte
Dim sBuffer As String
Dim vValue As String
Dim vU2 As Variant
Dim lresult As Long
Dim lcellCounter As Long
On Error GoTo E1
'Get/create a cryptography context
CryptAcquireContext hProv, vbNullString, MS_DEF_PROV, PROV_RSA_FULL, 0
If hProv = 0 Then
CryptAcquireContext hProv, vbNullString, MS_DEF_PROV, PROV_RSA_FULL,
CRYPT_NEWKEYSET
End If

'If we got one...
If hProv <> 0 Then

'Create an MD5 Hash
CryptCreateHash hProv, CALG_MD5, 0, 0, hhash

'If that was OK...
If hhash <> 0 Then

'Fill it with the contents of the range
(and it continues...)
the full function can be found here:
http://www.excelsig.org/VBA/wwHash.htm

-Stephen Rasey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top