Attribute VB_Name = "Licenser"
' Guide to the Perplexed
' Copyright © 1997 by Desaware Inc. All Rights Reserved

' Options supported by this module include:
' DEMOVERSION - Verifies ok in VB environment only
' DLLCHECK - Requires license file in VB environment


Option Explicit

Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As Long) As Long

#If DEMOVERSION Or DLLCHECK Then
' Return True if
' The demo can run on any VB platform, design
' or runtime
Public Function IsVBEnvironment() As Boolean
   Dim thismod&
   Dim thisfile$
   Dim basename$
   Dim thispos%
   Dim thischar$
   
   On Error GoTo nogo
   thismod = GetModuleHandle(0)
   thisfile = String$(262, Chr$(0))
   Call GetModuleFileName(thismod, thisfile, 261)
   thisfile = Left$(thisfile, InStr(thisfile, Chr$(0)) - 1)
   thispos = Len(thisfile)
   Do
      thischar = Mid$(thisfile, thispos, 1)
      If thischar = "\" Or thischar = ":" Then Exit Do
      thispos = thispos - 1
   Loop While thispos > 0
   basename = LCase$(Mid$(thisfile, thispos + 1))
   If basename = "vb.exe" Or basename = "vb32.exe" Or _
      basename = "vb5.exe" Or basename = "vb5cce.exe" Or _
      basename = "vb6.exe" Then
         IsVBEnvironment = True
   End If
   Exit Function
nogo:
End Function
#End If

Public Function VerifyLicense() As Boolean
   Static PriorVerification As Boolean
   Static VerifiedOnce As Boolean
   ' Fast return 2nd time through
   If PriorVerification Then
      VerifyLicense = VerifiedOnce
      Exit Function
   End If
   PriorVerification = True
   #If DEMOVERSION Then
      If Not IsVBEnvironment() Then
         ' License failure screen
         frmAbout.MessageType = 1
         frmAbout.Show vbModal
         VerifiedOnce = False
      Else
         ' Optional splash screen
         frmAbout.MessageType = 0
         frmAbout.Show vbModal
         VerifiedOnce = True
      End If
      VerifyLicense = VerifiedOnce
      Exit Function
   #End If
   #If DLLCHECK Then
      If IsVBEnvironment Then
         ' Check for DLL or object here.
         ' If not found, Set VerifyLicense to False
      Else
         VerifiedOnce = True
      End If
      VerifyLicense = VerifiedOnce
      Exit Function
   #End If
   VerifiedOnce = True    ' Default ok
   VerifyLicense = True
End Function

