Having issue with advapi32.dll and PockPC



have written a code that does encryption string to be stored into my
SQL server in the column called ID.

The encryption string is of something like this
{6F0E54FA-2C51-487E-A2A5-0412DA9FA3C6} and the code I am developing for
my PocketPC is like this, this is the class I am using in my program:

Imports System.Security.Permissions
Imports System.Runtime.InteropServices
Imports System.Net.Sockets

Public Class Utility
' Generate GUIDs on the .NET Compact Framework.
Public Class CoreGuid

' guid variant types
Private Enum GuidVariant
ReservedNCS = &H0
Standard = &H2
ReservedMicrosoft = &H6
ReservedFuture = &H7
End Enum

' guid version types
Private Enum GuidVersion
TimeBased = &H1
Reserved = &H2
NameBased = &H3
Random = &H4
End Enum

' constants that are used in the class
Private Class ConstValues
' number of bytes in guid
Public Const ByteArraySize As Integer = 16

' multiplex variant info
Public Const VariantByte As Integer = 8
Public Const VariantByteMask As Integer = &H3F
Public Const VariantByteShift As Integer = 6

' multiplex version info
Public Const VersionByte As Integer = 7
Public Const VersionByteMask As Integer = &HF
Public Const VersionByteShift As Integer = 4
End Class

' imports for the crypto api functions
Private Class WinApi
Public Const PROV_RSA_FULL As Integer = 1
Public Const CRYPT_VERIFYCONTEXT As Integer = &HF0000000

<DllImport("advapi32.dll")> _
Public Shared Function CryptAcquireContext( _
ByRef phProv As IntPtr, ByVal pszContainer As String, _
ByVal pszProvider As String, ByVal dwProvType As
Integer, _
ByVal dwFlags As Integer) As Boolean
End Function

<DllImport("advapi32.dll")> _
Public Shared Function CryptReleaseContext( _
ByVal hProv As IntPtr, ByVal dwFlags As Integer) As
End Function

<DllImport("advapi32.dll")> _
Public Shared Function CryptGenRandom( _
ByVal hProv As IntPtr, ByVal dwLen As Integer, _
ByVal pbBuffer() As Byte) As Boolean
End Function
End Class

' all static methods
Public Sub New()
End Sub

' Return a new System.Guid object.
Public Function NewGuid() As Guid
Dim hCryptProv As IntPtr = IntPtr.Zero
Dim guid As Guid = guid.Empty

' holds random bits for guid
Dim bits(ConstValues.ByteArraySize - 1) As Byte

' get crypto provider handle
If Not WinApi.CryptAcquireContext(hCryptProv, Nothing,
Nothing, _
Throw New SystemException( _
"Failed to acquire cryptography handle.")
End If

' generate a 128 bit (16 byte) cryptographically random
If Not WinApi.CryptGenRandom(hCryptProv, bits.Length,
bits) Then
Throw New SystemException( _
"Failed to generate cryptography random
End If

' set the variant
bits(ConstValues.VariantByte) =
bits(ConstValues.VariantByte) And _
bits(ConstValues.VariantByte) =
bits(ConstValues.VariantByte) Or _
CByte(GuidVariant.Standard <<

' set the version
bits(ConstValues.VersionByte) =
bits(ConstValues.VersionByte) And _
bits(ConstValues.VersionByte) =
bits(ConstValues.VersionByte) Or _
CByte(GuidVersion.Random <<

' create the new System.Guid object
guid = New Guid(bits)
' release the crypto provider handle
If Not hCryptProv.Equals(IntPtr.Zero) Then
WinApi.CryptReleaseContext(hCryptProv, 0)
End If
End Try

Return guid
End Function
End Class
End Class

It compiles correctly in my program but when I run the program in the
PockPC I get the following error:

Can't find PInvoke DLL 'advapi32.dll' error number 438. This is the
line of code I am using the function mentioned above:

Dim objGUID As New Utility.CoreGuid
strSQL = "INSERT INTO InventoryLocation
(ID,ItemNumber,Location, Lasalle) VALUES ('" &
Convert.ToString(objGUID.NewGuid) & "','" & strItemNumber & "','" &
txtNewLoc.Text & "'," & Val(txtQty.Text) & ")"

objComm = New SqlCommand(strSQL, objConn)
objDataReader = objComm.ExecuteReader
MsgBox(Err.Description & " - " & Err.Number)
Exit Sub
End Try
MsgBox("Operating completed.")
lngTotalNewLasalle = 0 : lngNewLasalle = 0 :
lngLasalle = 0
lngTotalLasalle = 0
Exit Sub

Can anyone help me please? It will be greatly appreciate it. Do I need
to install Advapi32.dll into the pocketPC? If so where can I find the
install for this? Ones again appreciate it for your help in advance.

Paul G. Tobey [eMVP]

Where did you get "advapi32.dll" from? That doesn't seem to be a Windows CE
DLL name to me. You have to put the *correct* DLL names in your P/Invoke
calls or things won't work. OpenNETCF's Smart Device Framework wraps the
cryptography stuff for you. www.opennetcf.org.

Paul T.

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