-
Notifications
You must be signed in to change notification settings - Fork 1
/
Module.cpp
128 lines (103 loc) · 3.12 KB
/
Module.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*******************************************************************
// Copyright (c) 2000, Robert Umbehant
// mailto:rumbehant@wheresjames.com
//
// 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
//
*******************************************************************/
// Module.cpp: implementation of the CModule class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CModule::CModule()
{_STT();
m_hModule = NULL;
}
CModule::CModule( LPCTSTR pFile )
{_STT();
CModule();
LoadLibrary( pFile );
}
CModule::~CModule()
{_STT();
Destroy();
}
BOOL CModule::LoadLibrary(LPCTSTR pFile)
{_STT();
// Do we already have this module loaded?
if ( NULL != m_hModule && pFile && strcmpi( pFile, m_sFile ) )
return TRUE;
// Lose module
Destroy();
// Punt if bad filename
if ( !pFile || !*pFile ) return FALSE;
// Save file name
m_sFile = pFile;
// Load the module
m_hModule = ::LoadLibrary( pFile );
if ( m_hModule == NULL ) return FALSE;
return TRUE;
}
void CModule::Destroy()
{_STT();
// Free library
if ( m_hModule != NULL )
{ FreeLibrary( m_hModule );
m_hModule = NULL;
} // end if
CHList::Destroy();
}
void* CModule::AddFunction(LPCTSTR pFunctionName)
{_STT();
// Sanity check
if ( pFunctionName == NULL ) return NULL;
void *pf = Addr( pFunctionName );
if ( pf != NULL ) return pf;
pf = (void*)GetProcAddress( m_hModule, pFunctionName );
if ( pf == NULL ) return FALSE;
// Save index
DWORD index = Size();
// Allocate memory
LPFUNCTIONINFO pfi = (LPFUNCTIONINFO)New( NULL, 0, pFunctionName );
if ( pfi == NULL ) return FALSE;
// Save function name and address
pfi->addr = pf;
// Need more space?
if ( m_ptrs.size() <= index )
{ if ( index < 8 ) m_ptrs.grow( 8 ); else m_ptrs.grow( index * 2 ); }
m_ptrs[ index ] = pf;
return pf;
}
LPVOID CModule::Addr(LPCTSTR pFunctionName)
{_STT();
LPFUNCTIONINFO pfi = (LPFUNCTIONINFO)Find( pFunctionName );
if ( pfi == NULL ) return NULL;
return pfi->addr;
}
LPVOID CModule::Addr(DWORD i)
{_STT();
if ( i >= m_ptrs.size() ) return NULL;
return m_ptrs[ i ];
}