⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dll.cpp

📁 MudMaster 2000 的C++源码
💻 CPP
字号:
/************************************************************************************
	Copyright (c) 2000 Aaron O'Neil
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

		1) Redistributions of source code must retain the above copyright notice, 
				this list of conditions and the following disclaimer.
		2) Redistributions in binary form must reproduce the above copyright notice, 
				this list of conditions and the following disclaimer in the documentation
				and/or other materials provided with the distribution.
		3) Redistributions in binary form must reproduce the above copyright notice on
				program startup. Additional credits for program modification are acceptable
				but original copyright and credits must be visible at startup.
		4) You may charge a reasonable copying fee for any distribution of Mud Master. 
				You may charge any fee you choose for support of Mud Master. You may not 
				charge a fee for Mud Master itself.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**************************************************************************************/

#include "StdAfx.h"
#include "Dll.h"
#include "Notifications.h"
#include "Extern.h"

CDll::CDll()
{
	m_ptrList.SetSize(DEFAULT_DLL_ARRAY_SIZE,DEFAULT_DLL_GROW_SIZE);
	m_nCount = 0;
	m_nGetIndex = 0;
}

CDll::~CDll()
{
	RemoveAll();
}

void CDll::RemoveAll()
{
	DLL *pDll;
	for (int i=0;i<m_nCount;i++)
	{
		pDll = (DLL *)m_ptrList.GetAt(i);
		ClearNotifications(pDll->hDLL);
		FreeLibrary(pDll->hDLL);
		delete pDll;
	}
	m_ptrList.RemoveAll();
	m_nCount = 0;
	m_nGetIndex = 0;
	HashIt();
}

int CDll::Add(const char *pszName)
{
	CString strName(pszName);
	strName.TrimRight();
	strName.TrimLeft();
	if (FindExact(strName) != NULL)
		return(FALSE);

	HINSTANCE hDll = LoadLibrary(pszName);
	if (hDll == NULL)
		return(FALSE);

	// Check for the Version function.  If it doesn't exists consider
	// it not to be a MM DLL.
	*_pszDLLResult = '\x0';
	MYPROC proc = (MYPROC)GetProcAddress(hDll,"Version");
	if (proc == NULL)
	{
		PrintMessage("Version proc not found.  Invalid DLL.");
		FreeLibrary(hDll);
		return(FALSE);
	}

	(proc)(pszName,_pszDLLResult);

	DLL *pNew = new DLL;
	pNew->strName = pszName;
	pNew->hDLL = hDll;
	pNew->strVersion = _pszDLLResult;

	DLL *pDll;
	for (int i=0;i<m_nCount;i++)
	{
		pDll = (DLL *)m_ptrList.GetAt(i);
		if (!pDll)
			continue;

		if (pNew->strName < pDll->strName)
		{
			m_ptrList.InsertAt(i,pNew);
			m_nCount++;
			HashIt();
			return(i+1);
		}
	}

	m_ptrList.SetAtGrow(m_nCount,pNew);
	m_nCount++;
	HashIt();
	return(m_nCount);
}

BOOL CDll::Remove(const char *pszName)
{
	DLL *pDll;
	for (int i=0;i<m_nCount;i++)
	{
		pDll = (DLL *)m_ptrList.GetAt(i);
		if (pDll->strName == pszName)
		{
			ClearNotifications(pDll->hDLL);
			FreeLibrary(pDll->hDLL);
			m_ptrList.RemoveAt(i);
			m_nCount--;
			delete pDll;
			HashIt();
			return(TRUE);
		}
	}
	return(FALSE);
}

BOOL CDll::Remove(int nIndex)
{
	if (nIndex < 0 || nIndex >= m_nCount)
		return(FALSE);

	DLL *pDll = (DLL *)m_ptrList.GetAt(nIndex);
	if (pDll == NULL)
		return(FALSE);

	ClearNotifications(pDll->hDLL);
	FreeLibrary(pDll->hDLL);
	m_ptrList.RemoveAt(nIndex);
	m_nCount--;
	delete pDll;
	HashIt();
	return(TRUE);
}

DLL* CDll::GetFirst()
{
	if (!m_nCount)
		return(NULL);
	m_nGetIndex = 0;
	return((DLL *)m_ptrList.GetAt(m_nGetIndex));
}

DLL* CDll::GetNext()
{
	if (m_nGetIndex+1 == m_nCount)
		return(NULL);
	m_nGetIndex++;
	return((DLL *)m_ptrList.GetAt(m_nGetIndex));
}

DLL* CDll::FindExact(const char *pszName)
{
	if (!*pszName)
		return(NULL);

	HASHITEM *phi = m_htHash[*pszName];
	if (!phi || phi->nStart == HASHITEM_EMPTY || phi->nEnd == HASHITEM_EMPTY)
		return(NULL);

	DLL *pDll;
	for (int i=phi->nStart;i<=phi->nEnd && i<m_nCount;i++)
	{
		pDll = (DLL *)m_ptrList.GetAt(i);
		if (pDll != NULL && pDll->strName == pszName)
			return(pDll);
	}
	return(NULL);
}

DLL* CDll::FindExact(HINSTANCE hDll)
{
	DLL *pDll;
	for (int i=0;i<m_nCount;i++)
	{
		pDll = (DLL *)m_ptrList.GetAt(i);
		if (pDll != NULL && pDll->hDLL == hDll)
			return(pDll);
	}
	return(NULL);
}

void CDll::ClearNotifications(HINSTANCE hDll)
{
	_notifyConnect.Remove(hDll);
	_notifyDisconnect.Remove(hDll);
}

void CDll::HashIt()
{
	DLL *pDll;
	UINT nCurEntry;		// Current hash table entry.
	UINT nEntry;			// Entry of the item being checked.
	int i;
	int nStart;
	int nEnd;

 	m_htHash.Clear();

	nStart = HASHITEM_EMPTY;
	nEnd = HASHITEM_EMPTY;
	for (i=0;i<m_nCount;i++)
	{
		pDll = (DLL *)m_ptrList.GetAt(i);
		if (!pDll)
			continue;

		nEntry = pDll->strName.GetAt(0);

		// Will only be this value first time thru.
		if (!i)
		{
			nCurEntry = nEntry;
			nStart = i;
		}

		// If the entry we just found is not equal the to the entry we are working
		// with, need to finish off the last entry and start a new one.
		if (nEntry != nCurEntry)
		{
			m_htHash.SetAt(nCurEntry,nStart,nEnd);
			nStart = i;
			nCurEntry = nEntry;
		}

		nEnd = i;
	}

	// Last time thru loop need to add last hash entry.
	if (m_nCount)
		m_htHash.SetAt(nCurEntry,nStart,nEnd);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -