Developing COM Components using VC-ATL(3-5)

类别:VC语言 点击:0 评论:0 推荐:

 

4.         编写实现函数功能的代码

 

     ValidateCard.cpp清单

        

     // ValidateCard.cpp : Implementation of CValidateCard

#include "stdafx.h"

#include "ValidateCreditCardServer.h"

#include "ValidateCard.h"

 

/////////////////////////////////////////////////////////////////////////////

// CValidateCard

 

 

STDMETHODIMP CValidateCard::ValidateCreditCard(BSTR bstrCCNo, BSTR bstrCCType, long *val)

{

         AFX_MANAGE_STATE(AfxGetStaticModuleState())

 

         // TODO: Add your implementation code here

    CString strType(bstrCCType);

         strType.MakeUpper();

         if(strType.Compare("VISA")==0)

                   bstrCCType=SysAllocString(OLESTR("V"));

         else if(strType.Compare("MASTER")==0)

                   bstrCCType=SysAllocString(OLESTR("M"));

         else if(strType.Compare("AMERICAN")==0)

                   bstrCCType=SysAllocString(OLESTR("A"));

         else

         {

                   *val=0;

                   return S_FALSE;

         }

 

         if((PrefixCheck(bstrCCNo,bstrCCType)==S_OK) && (LengthCheck(bstrCCNo,bstrCCType)==S_OK) && (LuhnCheck(bstrCCNo,bstrCCType)==S_OK))

         {

                   *val=1;

                   return S_OK;

         }

         else

         {

                   *val=0;

                   return S_FALSE;

         }

         return S_FALSE;

}

 

STDMETHODIMP CValidateCard::LuhnCheck(BSTR bstrCCNo, BSTR bstrCCType)

{

         AFX_MANAGE_STATE(AfxGetStaticModuleState())

 

         // TODO: Add your implementation code here

         CString strobj(bstrCCNo);

         CString strReverse,strFinal;

         int iNum,iSum;

         char cTemp;

 

         strReverse=strobj;

         strReverse.MakeReverse();

 

         for(int iTemp=0;iTemp<strReverse.GetLength();iTemp++)

         {

                   cTemp=strReverse.GetAt(iTemp);

                   iNum=atoi(&cTemp);

                   if(iTemp % 2 !=0)

                   {

                            iNum=iNum*2;

                            if(iNum>9)

                                     iNum-=9;

                   }

                   cTemp=iNum+'0';

                   strFinal+=cTemp;

         }

 

         for(iTemp=0;iTemp<strFinal.GetLength();iTemp++)

         {

                   cTemp=strFinal.GetAt(iTemp);

                   iSum+=atoi(&cTemp);

         }

         if(iSum % 10 == 0)

                   return S_OK;

         else

                   return S_FALSE;

}

 

STDMETHODIMP CValidateCard::LengthCheck(BSTR bstrCCNo, BSTR bstrCCType)

{

         AFX_MANAGE_STATE(AfxGetStaticModuleState())

 

         // TODO: Add your implementation code here

         CString strType(bstrCCType),strLength(bstrCCNo);

         int ilen=strLength.GetLength();

         if(strType=="V")

         {

                   if(ilen==16)return S_OK;

         }

         else if(strType=="M")

         {

                   if(ilen==16)return S_OK;

         }

         else if(strType=="A")

         {

                   if(ilen==15)return S_OK;

         }

         return S_FALSE;

}

 

STDMETHODIMP CValidateCard::PrefixCheck(BSTR bstrCCNo, BSTR bstrCCType)

{

         AFX_MANAGE_STATE(AfxGetStaticModuleState())

 

         // TODO: Add your implementation code here

         CString strNumber(bstrCCNo),strType(bstrCCType);

         if(strType=="V")

         {

                   if(strNumber.Find("4",0)>=0)

                            return S_OK;

         }

         else if(strType=="M")

         {

                   if(strNumber.Find("51",0)>=0 || strNumber.Find("52",0)>=0 || strNumber.Find("53",0)>=0 || strNumber.Find("54",0)>=0)

                            return S_OK;

         }

         else if(strType=="A")

         {

                   if(strNumber.Find("34",0)>=0 || strNumber.Find("37",0)>=0)

                            return S_OK;

         }

         return S_FALSE;

}

5.         编绎组件

编绎会读取ValidateCard.rgs文件进行组件的自动注册,若在Windows 9X环境下,请运行regsvr32程序进行注册,具体参见

Developing COM Components using VC-ATL(2-3)  COM对象的注册角色

同时编绎完成后,会

在Debug目录下生成ValidateCreditCardServer.dll

在Debug上一层目录下,即工程名工作空间目录下生成ValidateCreditCardServer.tlb

这两个文件都可以导入到客户端,测试时分别会介绍这两种方法。

本文地址:http://com.8s8s.com/it/it1308.htm