📄 beeper.cpp
字号:
/*
* BEEPER.CPP
* Beeper Automation Object #3 Chapter 14
*
* Implementation of the CBeeper class demonstrating the use of
* standard error handling that ties in with ITypeInfo::Invoke
* (as well as DispInvoke). This includes an implementation of
* ISupportErrorInfo.
*
* Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
*
* Kraig Brockschmidt, Microsoft
* Internet : kraigb@microsoft.com
* Compuserve: >INTERNET:kraigb@microsoft.com
*/
#include "beeper.h"
extern HINSTANCE g_hInst;
#ifdef WIN32
extern DWORD g_dwTLS;
#else
LANGID g_langID;
#endif
/*
* CBeeper::CBeeper
* CBeeper::~CBeeper
*
* Parameters (Constructor):
* pUnkOuter LPUNKNOWN of a controlling unknown.
* pfnDestroy PFNDESTROYED to call when an object
* is destroyed.
*/
CBeeper::CBeeper(LPUNKNOWN pUnkOuter, PFNDESTROYED pfnDestroy)
{
m_cRef=0;
m_pUnkOuter=pUnkOuter;
m_pfnDestroy=pfnDestroy;
m_lSound=0;
m_pImpIDispatch=NULL;
return;
}
CBeeper::~CBeeper(void)
{
DeleteInterfaceImp(m_pImpIDispatch);
return;
}
/*
* CBeeper::Init
*
* Purpose:
* Performs any intiailization of a CBeeper that's prone to failure
* that we also use internally before exposing the object outside.
*
* Parameters:
* None
*
* Return Value:
* BOOL TRUE if the function is successful,
* FALSE otherwise.
*/
BOOL CBeeper::Init(void)
{
LPUNKNOWN pIUnknown=this;
if (NULL!=m_pUnkOuter)
pIUnknown=m_pUnkOuter;
m_pImpIDispatch=new CImpIDispatch(this, pIUnknown);
if (NULL==m_pImpIDispatch)
return FALSE;
return TRUE;
}
/*
* CBeeper::QueryInterface
* CBeeper::AddRef
* CBeeper::Release
*/
STDMETHODIMP CBeeper::QueryInterface(REFIID riid, PPVOID ppv)
{
*ppv=NULL;
/*
* The only calls for IUnknown are either in a nonaggregated
* case or when created in an aggregation, so in either case
* always return our IUnknown for IID_IUnknown.
*
* Note that IBeeper is implemented on CBeeper, and we must
* also expose that custom interface through QueryInterface.
*/
if (IID_IUnknown==riid || IID_IBeeper==riid)
*ppv=this;
/*
* QueryInterface must respond not only to IID_IDispatch for
* the primary automation interface, but also to the IID of the
* dispinterface itself, which in our case is DIID_DIBeeper.
*/
if (IID_IDispatch==riid || DIID_DIBeeper==riid)
*ppv=m_pImpIDispatch;
//Indicate that we support error information
if (IID_ISupportErrorInfo==riid)
*ppv=m_pImpISuppErr;
//AddRef any interface we'll return.
if (NULL!=*ppv)
{
((LPUNKNOWN)*ppv)->AddRef();
return NOERROR;
}
return ResultFromScode(E_NOINTERFACE);
}
STDMETHODIMP_(ULONG) CBeeper::AddRef(void)
{
return ++m_cRef;
}
STDMETHODIMP_(ULONG) CBeeper::Release(void)
{
if (0L!=--m_cRef)
return m_cRef;
if (NULL!=m_pfnDestroy)
(*m_pfnDestroy)();
delete this;
return 0L;
}
//IBeeper interface functions
/*
* CBeeper::get_Sound
* CBeeper::put_Sound
*
* Purpose:
* Functions called from DispInvoke to handle the Sound property.
*
* Parameters (Set only):
* lSound long, new sound to save after validation
*
* Return Value: (Get only):
* long Current sound.
*/
STDMETHODIMP_(long) CBeeper::get_Sound(void)
{
return m_lSound;
}
STDMETHODIMP_(void) CBeeper::put_Sound(long lSound)
{
if (MB_OK!=lSound && MB_ICONEXCLAMATION!=lSound
&& MB_ICONQUESTION!=lSound && MB_ICONHAND!=lSound
&& MB_ICONASTERISK!=lSound)
{
/*
* Since we cannot return a value to ITypeInfo::Invoke to
* indicate an exception condition, we'll use
* m_pImpIDispatch->Exception to raise them. This
* Exception function now uses the error info mechanism
* to raise exceptions rather than relying on our own
* single-threaded model as used in Beeper #2.
*/
m_pImpIDispatch->Exception(EXCEPTION_INVALIDSOUND);
return;
}
m_lSound=lSound;
return;
}
/*
* CBeeper::Beep
*
* Purpose:
* Function called from DispInvoke to invoke the Beep method.
*
* Return Value:
* long The sound played.
*/
STDMETHODIMP_(long) CBeeper::Beep(void)
{
MessageBeep((UINT)m_lSound);
return m_lSound;
}
//IDispatch interface implementation
/*
* CImpIDispatch::CImpIDispatch
* CImpIDispatch::~CImpIDispatch
*
* Parameters (Constructor):
* pObj PCBeeper of the object we're in.
* pUnkOuter LPUNKNOWN to which we delegate.
*/
CImpIDispatch::CImpIDispatch(PCBeeper pObj, LPUNKNOWN pUnkOuter)
{
m_cRef=0;
m_pObj=pObj;
m_pUnkOuter=pUnkOuter;
//These are created as needed in GetTypeInfo
m_pITINeutral=NULL;
m_pITIGerman=NULL;
return;
}
CImpIDispatch::~CImpIDispatch(void)
{
ReleaseInterface(m_pITIGerman);
ReleaseInterface(m_pITINeutral);
return;
}
/*
* CImpIDispatch::QueryInterface
* CImpIDispatch::AddRef
* CImpIDispatch::Release
*
* Purpose:
* IUnknown members for CImpIDispatch object.
*/
STDMETHODIMP CImpIDispatch::QueryInterface(REFIID riid, PPVOID ppv)
{
return m_pUnkOuter->QueryInterface(riid, ppv);
}
STDMETHODIMP_(ULONG) CImpIDispatch::AddRef(void)
{
++m_cRef;
return m_pUnkOuter->AddRef();
}
STDMETHODIMP_(ULONG) CImpIDispatch::Release(void)
{
--m_cRef;
return m_pUnkOuter->Release();
}
/*
* CImpIDispatch::GetTypeInfoCount
*
* Purpose:
* Returns the number of type information (ITypeInfo) interfaces
* that the object provides (0 or 1).
*
* Parameters:
* pctInfo UINT * to the location to receive
* the count of interfaces.
*
* Return Value:
* HRESULT NOERROR or a general error code.
*/
STDMETHODIMP CImpIDispatch::GetTypeInfoCount(UINT *pctInfo)
{
//We implement GetTypeInfo so return 1
*pctInfo=1;
return NOERROR;
}
/*
* CImpIDispatch::GetTypeInfo
*
* Purpose:
* Retrieves type information for the automation interface. This
* is used anywhere that the right ITypeInfo interface is needed
* for whatever LCID is applicable. Specifically, this is used
* from within GetIDsOfNames and Invoke.
*
* Parameters:
* itInfo UINT reserved. Must be zero.
* lcid LCID providing the locale for the type
* information. If the object does not support
* localization, this is ignored.
* ppITypeInfo ITypeInfo ** in which to store the ITypeInfo
* interface for the object.
*
* Return Value:
* HRESULT NOERROR or a general error code.
*/
STDMETHODIMP CImpIDispatch::GetTypeInfo(UINT itInfo, LCID lcid
, ITypeInfo **ppITypeInfo)
{
HRESULT hr;
ITypeLib *pITypeLib;
ITypeInfo **ppITI=NULL;
if (0!=itInfo)
return ResultFromScode(TYPE_E_ELEMENTNOTFOUND);
if (NULL==ppITypeInfo)
return ResultFromScode(E_POINTER);
*ppITypeInfo=NULL;
/*
* Since we returned one from GetTypeInfoCount, this function
* can be called for a specific locale. We support English,
* German, and neutral (defaults to English) locales. Anything
* else is an error.
*
* After this switch statement, ppITI will either be NULL or
* a valid pointer in it after. If NULL, we know we need to
* load type information, retrieve the ITypeInfo we want, and
* then store it in *ppITI.
*/
switch (PRIMARYLANGID(lcid))
{
case LANG_NEUTRAL:
case LANG_ENGLISH:
ppITI=&m_pITINeutral;
break;
case LANG_GERMAN:
ppITI=&m_pITIGerman;
break;
default:
return ResultFromScode(DISP_E_UNKNOWNLCID);
}
//Load a type lib if we don't have the information already.
if (NULL==*ppITI)
{
/*
* The type libraries are registered under 0 (neutral),
* 7 (German), and 9 (English) with no specific sub-
* language, which would make them 407 or 409 and such.
* If you are sensitive to sub-languages, then use the
* full LCID instead of just the LANGID as done here.
*/
hr=LoadRegTypeLib(LIBID_BeeperTypeLibrary, 1, 0
, PRIMARYLANGID(lcid), &pITypeLib);
/*
* If LoadRegTypeLib fails, try loading directly with
* LoadTypeLib, which will register the library for us.
* Note that there's no default case here because the
* prior switch will have filtered lcid already.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -