VC 2008 加 include "stdafx.h"程序出错
手动写ATL的简单demo.在vc6.0下可以编译成dll.但在vc 2008中
如果在代码头中加入 include "stdafx.h", 程序无法编译通过
error:
Error 21 fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source? d:\developer\atlverdll1.0.0\registry.cpp 233 ATLVerDll1.0.0
若在程序中加入#include "stdafx.h"
则很多的错误都出现了包括:
Error 2 error C2653: 'Math' : is not a class or namespace name d:\developer\atldllver1.0.1\math.cpp 5 AtlDllver1.0.1
代码结构如下:
// {376DB866-7079-44b6-ACCA-0EB684E88377}
DEFINE_GUID(CLSID_Math,
0x376db866, 0x7079, 0x44b6, 0xac, 0xca, 0xe, 0xb6, 0x84, 0xe8, 0x83, 0x77);
// {B1823CE9-1C56-401b-AEF9-94AE7204BF54}
DEFINE_GUID(IID_IMath,
0xb1823ce9, 0x1c56, 0x401b, 0xae, 0xf9, 0x94, 0xae, 0x72, 0x4, 0xbf, 0x54);
// {5F7F9EAA-29A1-49ac-BDC4-6DD3B0250582}
DEFINE_GUID(IID_IAdvancedMath,
0x5f7f9eaa, 0x29a1, 0x49ac, 0xbd, 0xc4, 0x6d, 0xd3, 0xb0, 0x25, 0x5, 0x82);
class IMath: public IUnknown
{
STDMETHOD(Add) (long ,long ,long *) PURE;
STDMETHOD(Sub) (long,long,long *) PURE;
STDMETHOD(Mul) (long,long,long *) PURE;
STDMETHOD(Dev) (long,long,long *) PURE;
};
class IAdvancedMath: public IUnknown
{
STDMETHOD(Factorail) (short, long *) PURE;
STDMETHOD(Fibonal) (short, long *) PURE;
};
#include "Math.h"
#include <iostream>
#include "stdafx.h"
//Math class
Math::Math()
{
m_cRef = 0;
cout << "Math construction" << endl;
InterlockedIncrement(&g_lObjs);
}
Math::~Math()
{
cout << "Math deconstruction" << endl;
InterlockedDecrement(&g_lObjs);
}
STDMETHODIMP Math::QueryInterface(REFIID iid, void **ppv)
{
if (iid == IID_IUnknown)
{
*ppv = static_cast<IMath*>(this);
}
else if (iid == IID_IMath)
{
*ppv = static_cast<IMath*>(this);
}
else if (iid == IID_IAdvancedMath)
{
*ppv = static_cast<IAdvancedMath*>(this);
}
else
{
*ppv = NULL;
return E_NOINTERFACE;
}
reinterpret_cast<IUnknown*>(*ppv)->AddRef();
return S_OK;
}
STDMETHODIMP_(ULONG) Math::AddRef()
{
return InterlockedIncrement(&m_cRef);
}
STDMETHODIMP_(ULONG) Math::Release()
{
if (InterlockedDecrement(&m_cRef) == 0)
{
delete this;
return 0;
}
return m_cRef;
}
STDMETHODIMP Math::Add(long op1, long op2, long *pResult)
{
*pResult = op1 + op2;
return S_OK;
}
STDMETHODIMP Math::Sub(long op1, long op2, long *pResult)
{
*pResult = op1 - op2;
return S_OK;
}
STDMETHODIMP Math::Mul(long op1,long op2,long *pResult)
{
*pResult = op1 * op2;
return S_OK;
}
STDMETHODIMP Math::Dev(long op1, long op2, long *pResult)
{
*pResult = op1/op2;
return S_OK;
}
long calcFactorail(short op1)
{
if (op1 == 1)
{return 1;}
else
{
return op1 * calcFactorail(op1-1);
}
}
STDMETHODIMP Math::Factorail(short op1, long *pResult)
{
*pResult = calcFactorail(op1);
return S_OK;
}
long calcFibonal(short op1)
{
if (op1 <= 1)
{
return 1;
}
else
{
return calcFibonal(op1-1)+ calcFibonal(op1-2);
}
}
STDMETHODIMP Math::Fibonal(short op1, long *pResult)
{
*pResult = calcFibonal(op1);
return S_OK;
}
//classFactory
MathClassFactory::MathClassFactory
{
m_cRef = 0;
Cout << "MathClass Constuction" << endl;
}
MathClassFactory::~MathClassFactory()
{
cout << "MathClass Deconstruction" << endl;
}
STDMETHODIMP MathClassFactory::QueryInterface(REFIID iid,void **ppv)
{
if (iid == IID_IUnknown || iid == IID_IClassFacory)
{
*ppv = static_cast<IClassFactory*>(this);
}
else
{
*ppv = NULL;
return E_NOINTERFACE;
}
reinterpret_cast<IUnknown*>(*ppv)->AddRef();
return S_OK;
}
STDMETHODIMP_(ULONG) MathClassFactory::AddRef()
{
return InterlockedIncrement(&m_cRef);
}
STDMETHODIMP_(ULONG) MathClassFactory::Release()
{
if (InterlockedDecrement(&m_cRef) == 0 )
{
delete this;
return 0;
}
return m_cRef;
}
STDMETHODIMP MathClassFactory::CreateInstance(LPUNKNOWN pOutUnk,REFIID iid,void **ppv)
{
Math *pMth = NULL;
pMth = new Math;
if (pMth == NULL)
{
return E_OUTOFMEMORY;
}
HRESULT hr = pMth->QueryInterface(iid,ppv);
if (FAILED(hr))
{
delete pMth;
}
return hr;
}
STDMETHODIMP MathClassFactory::LockServer(BOOL fLock)
{
if (fLock)
{
InterlockedIncrement(&g_lLocks);
}
else
{
InterlockedDecrement(&g_lLocks);
}
return S_OK;
}