IUnknown接口

来源:互联网 发布:抓东西的软件 编辑:程序博客网 时间:2024/05/09 17:20

creditverification.dll

// CreditVerification.h
#pragma once

#ifdef _DLL
#define DLLSPEC __declspec (dllexport)
#else
#define DLLSPEC __declspec (dllimport)
#endif

#define interface struct

interface IUnknown
{
       virtual long QueryInterface(IID& iid, void** pp) = 0;
       virtual long AddRef() = 0;
       virtual long Release() = 0;
};

interface ICreditVerification : IUnknown
{
 virtual void SetCardType(char* s) = 0;
 virtual bool VerifyCardNumber(long n) = 0;
};

DLLSPEC ICreditVerification* GetCreditVerificationObject();

 

// creditverification.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"

#define _DLL
#include "creditverification.h"

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
      )
{
    return TRUE;
}

class CCreditVerification : public ICreditVerification
{
private:
 enum CardType {Visa, MasterCard, Amex} m_type;
 long m_refCount;
public:
 CCreditVerification() : m_refCount(0) {}
 long QueryInterface(IID& iid, void** pp) { return 0; }

 long AddRef() { return ++m_refCount; }
 long Release()
 {
  if (--m_refCount == 0)
  {
   delete this;
   return 0;
  }
  return m_refCount;
 }

 void SetCardType(char* s)
 {
  if (0 == strcmp("Visa", s))
   m_type = Visa;
  else if (0 == strcmp("MasterCard", s))
   m_type = MasterCard;
  else m_type = Amex;
 }
 bool VerifyCardNumber(long n)
 {
  if (m_type == Visa)
   return (n % 2);
  else if (m_type == MasterCard)
   return (n % 3);
  else if (m_type == Amex)
   return (n % 5);
  else return false;
 }
};

DLLSPEC ICreditVerification* GetCreditVerificationObject()
{
 ICreditVerification* p = new CCreditVerification;
 p->AddRef();
 return p;
}

建DLL时选空project的话:

f:/vs.netprojects/creditverification/creditverification.h(14): error C2061: syntax error : identifier 'IID'
f:/vs.netprojects/creditverification/creditverification.cpp(56): error C2259: 'CCreditVerification' : cannot instantiate abstract class
原因未知。

另#define WIN32_LEAN_AND_MEAN作用?
 

原创粉丝点击