天天看點

Developing COM Components using VC-ATL(3)

Visual Basic、Visual C++元件調用互相通

n          Visual Basic調用Visual Basic元件

n          Visual C++調用Visual Basic元件

n          Visual Basic調用Visual C++元件

n          Visual C++調用Visual C++元件

n          編寫Visual Basic元件

n          元件功能:完成三種類型的信用卡号碼的合法性檢查

n          編寫步驟:

1.         建立一工程,工程類型選擇ActiveX DLL,把工程名由預設的Project1改為ValidateCardServer,并把預設的類子產品Class1改為IValidateCard,最後在此類裡敲入如下代碼。

Option Explicit

Public

Function

fnValidateCreditCard

(ByVal strCCNumber As String, ByVal strCCType As String) As Boolean

    Dim bValid As Boolean

    strCCType = Trim(strCCType)

    Select Case UCase(strCCType)

        Case "VISA"

            strCCType = "V"

        Case "MASTER"

            strCCType = "M"

        Case "American"

            strCCType = "A"

    End Select

    If fnPrefixCheck(strCCNumber, strCCType) And fnLengthCheck(strCCNumber, strCCType) And fnLuhnCheck(strCCNumber, strCCType) Then

        bValid = True

    Else

        bValid = False

    End If

    fnValidateCreditCard = bValid

End Function

Private

Function

fnPrefixCheck

(strCCNumber As String, strCCType As String) As Boolean

    Dim bValidPrefix As Boolean

    Select Case UCase(strCCType)

        Case "V"

            If InStr(1, strCCNumber, "4") = 1 Then

                bValidPrefix = True

            End If

        Case "M"

            If InStr(1, strCCNumber, "51") = 1 Or _

               InStr(1, strCCNumber, "52") = 1 Or _

               InStr(1, strCCNumber, "53") = 1 Or _

               InStr(1, strCCNumber, "54") = 1 Or _

               InStr(1, strCCNumber, "55") = 1 Then

               bValidPrefix = True

            End If

        Case "A"

            If InStr(1, strCCNumber, "34") = 1 Or _

               InStr(1, strCCNumber, "37") Then

               bValidPrefix = True

            End If

    End Select

    fnPrefixCheck = bValidPrefix

End Function

Private

Function

fnLengthCheck

(strCCNumber As String, strCCType As String) As Boolean

    Dim bValidLength As Boolean

    Select Case UCase(strCCType)

        Case "V"    'A visa card has a 13 digit or a 16 digit number

            If Len(strCCNumber) = 13 Or Len(strCCNumber) = 16 Then

                bValidLength = True

            End If

        Case "M"    'A mastercard has a 16 digit number

            If Len(strCCNumber) = 16 Then

                bValidLength = True

            End If

        Case "A"    'American Express has a 15 digit number

            If Len(strCCNumber) = 15 Then

                bValidLength = True

            End If

    End Select

    fnLengthCheck = bValidLength

End Function

Private

Function

fnLuhnCheck

(strCCNumber As String, strCCType As String) As Boolean

    Dim bValidLuhn As Boolean

    Dim strRev As String

    Dim strCh As String

    Dim intNumber As Integer

    Dim strNumberFinal As String

    Dim intSum As Integer

    Dim intTemp As Integer

    strRev = StrReverse(strCCNumber)

    For intTemp = 1 To Len(strRev)

        strCh = Mid(strRev, intTemp, 1)

        intNumber = CInt(strCh)

        If intTemp Mod 2 = 0 Then

            intNumber = intNumber * 2

            If intNumber > 9 Then

                intNumber = intNumber - 9

            End If

        End If

        strNumberFinal = strNumberFinal & intNumber

    Next intTemp

    For intTemp = 1 To Len(strNumberFinal)

        intSum = intSum + Mid(strNumberFinal, intTemp, 1)

    Next intTemp

    If intSum Mod 10 = 0 Then

        bValidLuhn = True

    Else

        bValidLuhn = False

    End If

    fnLuhnCheck = bValidLuhn

End Function

2.         編繹元件。從File菜單中選擇 Make ValidateCardServer. dll…

n          Visual Basic調用Visual Basic元件

n          編寫步驟:

1.         建立一個标準工程,工程名設定為VBTestVBComProj;引入元件,選擇菜單Project->References…,點選浏覽,選擇C:/VBCom/ ValidateCardServer.dll;敲入如下窗體代碼。

Private Sub Command1_Click()

Dim comobj As New

ValidateCardServer

.

IValidateCard

If comobj.fnValidateCreditCard(Text1.Text, Text2.Text) = True Then

    MsgBox "valid card"

Else

    MsgBox "invalid card"

End If

End Sub

2.         編繹運作測試程式。

n          代碼測試:在信用卡号碼中輸入4567890123456783,在信用卡類型中輸入visa,點選确定,彈出"valid card"的提示框;在信用卡号碼中輸入4567890123456789,在信用卡類型中輸入visa,點選确定按鈕,彈出"invalid card"的提示框。

作者Blog: http://blog.csdn.net/callzjy/