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"的提示框。
本文地址:http://com.8s8s.com/it/it1303.htm