📄 bindctx.c
字号:
/***************************************************************************************
* BindCtx implementation
*
* Copyright 1999 Noomen Hamza
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************************/
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#define COBJMACROS
#include "winerror.h"
#include "windef.h"
#include "winbase.h"
#include "objbase.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(ole);
/* represent the first size table and it's increment block size */
#define BLOCK_TAB_SIZE 10
#define MAX_TAB_SIZE 0xFFFFFFFF
/* data structure of the BindCtx table elements */
typedef struct BindCtxObject{
IUnknown* pObj; /* point on a bound object */
LPOLESTR pkeyObj; /* key associated to this bound object */
BYTE regType; /* registration type: 1 if RegisterObjectParam and 0 if RegisterObjectBound */
} BindCtxObject;
/* BindCtx data strucrture */
typedef struct BindCtxImpl{
const IBindCtxVtbl *lpVtbl; /* VTable relative to the IBindCtx interface.*/
LONG ref; /* reference counter for this object */
BindCtxObject* bindCtxTable; /* this is a table in which all bounded objects are stored*/
DWORD bindCtxTableLastIndex; /* first free index in the table */
DWORD bindCtxTableSize; /* size table */
BIND_OPTS2 bindOption2; /* a structure which contains the bind options*/
} BindCtxImpl;
/* IBindCtx prototype functions : */
static HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx*);
static HRESULT BindCtxImpl_GetObjectIndex(BindCtxImpl*, IUnknown*, LPOLESTR, DWORD *);
/*******************************************************************************
* BindCtx_QueryInterface
*******************************************************************************/
static HRESULT WINAPI
BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject)
{
BindCtxImpl *This = (BindCtxImpl *)iface;
TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
/* Perform a sanity check on the parameters.*/
if ( (This==0) || (ppvObject==0) )
return E_INVALIDARG;
/* Initialize the return parameter.*/
*ppvObject = 0;
/* Compare the riid with the interface IDs implemented by this object.*/
if (IsEqualIID(&IID_IUnknown, riid) ||
IsEqualIID(&IID_IBindCtx, riid))
{
*ppvObject = (IBindCtx*)This;
IBindCtx_AddRef(iface);
return S_OK;
}
return E_NOINTERFACE;
}
/******************************************************************************
* BindCtx_AddRef
******************************************************************************/
static ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface)
{
BindCtxImpl *This = (BindCtxImpl *)iface;
TRACE("(%p)\n",This);
return InterlockedIncrement(&This->ref);
}
/******************************************************************************
* BindCtx_Destroy (local function)
*******************************************************************************/
static HRESULT BindCtxImpl_Destroy(BindCtxImpl* This)
{
TRACE("(%p)\n",This);
/* free the table space memory */
HeapFree(GetProcessHeap(),0,This->bindCtxTable);
/* free the bindctx structure */
HeapFree(GetProcessHeap(),0,This);
return S_OK;
}
/******************************************************************************
* BindCtx_Release
******************************************************************************/
static ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface)
{
BindCtxImpl *This = (BindCtxImpl *)iface;
ULONG ref;
TRACE("(%p)\n",This);
ref = InterlockedDecrement(&This->ref);
if (ref == 0)
{
/* release all registered objects */
BindCtxImpl_ReleaseBoundObjects((IBindCtx*)This);
BindCtxImpl_Destroy(This);
}
return ref;
}
/******************************************************************************
* BindCtx_RegisterObjectBound
******************************************************************************/
static HRESULT WINAPI
BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk)
{
BindCtxImpl *This = (BindCtxImpl *)iface;
DWORD lastIndex=This->bindCtxTableLastIndex;
TRACE("(%p,%p)\n",This,punk);
if (punk==NULL)
return E_POINTER;
IUnknown_AddRef(punk);
/* put the object in the first free element in the table */
This->bindCtxTable[lastIndex].pObj = punk;
This->bindCtxTable[lastIndex].pkeyObj = NULL;
This->bindCtxTable[lastIndex].regType = 0;
lastIndex= ++This->bindCtxTableLastIndex;
if (lastIndex == This->bindCtxTableSize){ /* the table is full so it must be resized */
if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
FIXME("This->bindCtxTableSize: %ld is out of data limite\n", This->bindCtxTableSize);
return E_FAIL;
}
This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
This->bindCtxTableSize * sizeof(BindCtxObject));
if (!This->bindCtxTable)
return E_OUTOFMEMORY;
}
return S_OK;
}
/******************************************************************************
* BindCtx_RevokeObjectBound
******************************************************************************/
static HRESULT WINAPI
BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk)
{
DWORD index,j;
BindCtxImpl *This = (BindCtxImpl *)iface;
TRACE("(%p,%p)\n",This,punk);
/* check if the object was registered or not */
if (BindCtxImpl_GetObjectIndex(This,punk,NULL,&index)==S_FALSE)
return MK_E_NOTBOUND;
if(This->bindCtxTable[index].pObj)
IUnknown_Release(This->bindCtxTable[index].pObj);
HeapFree(GetProcessHeap(),0,This->bindCtxTable[index].pkeyObj);
/* left-shift all elements in the right side of the current revoked object */
for(j=index; j<This->bindCtxTableLastIndex-1; j++)
This->bindCtxTable[j]= This->bindCtxTable[j+1];
This->bindCtxTableLastIndex--;
return S_OK;
}
/******************************************************************************
* BindCtx_ReleaseBoundObjects
******************************************************************************/
static HRESULT WINAPI
BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface)
{
DWORD i;
BindCtxImpl *This = (BindCtxImpl *)iface;
TRACE("(%p)\n",This);
for(i=0;i<This->bindCtxTableLastIndex;i++)
{
if(This->bindCtxTable[i].pObj)
IUnknown_Release(This->bindCtxTable[i].pObj);
HeapFree(GetProcessHeap(),0,This->bindCtxTable[i].pkeyObj);
}
This->bindCtxTableLastIndex = 0;
return S_OK;
}
/******************************************************************************
* BindCtx_SetBindOptions
******************************************************************************/
static HRESULT WINAPI
BindCtxImpl_SetBindOptions(IBindCtx* iface,BIND_OPTS *pbindopts)
{
BindCtxImpl *This = (BindCtxImpl *)iface;
TRACE("(%p,%p)\n",This,pbindopts);
if (pbindopts==NULL)
return E_POINTER;
if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
{
WARN("invalid size\n");
return E_INVALIDARG; /* FIXME : not verified */
}
memcpy(&This->bindOption2, pbindopts, pbindopts->cbStruct);
return S_OK;
}
/******************************************************************************
* BindCtx_GetBindOptions
******************************************************************************/
static HRESULT WINAPI
BindCtxImpl_GetBindOptions(IBindCtx* iface,BIND_OPTS *pbindopts)
{
BindCtxImpl *This = (BindCtxImpl *)iface;
TRACE("(%p,%p)\n",This,pbindopts);
if (pbindopts==NULL)
return E_POINTER;
if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
{
WARN("invalid size\n");
return E_INVALIDARG; /* FIXME : not verified */
}
memcpy(pbindopts, &This->bindOption2, pbindopts->cbStruct);
return S_OK;
}
/******************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -