JunkBox_Win_Lib 1.5.3
Loading...
Searching...
No Matches
WinDLLTool.cpp
Go to the documentation of this file.
1
6#include "..\stdafx.h"
7
8#include "tools++.h"
9#include "WinDLLTool.h"
10
11
12using namespace jbxl;
13using namespace jbxwl;
14
15
17//
18// DLLModule
19//
20// pgetinfo: char* (*pgetinfo)(int n) の関数へのポインタ.モジュールの情報を獲得するための関数.
21// モジュールには (*pgetinfo)(0) で情報(タイトル)を返す関数が必ず必要.
22//
23
30void DLLModule::init(LPCTSTR fname, int n)
31{
32 status = 0;
33 fnum = n;
34
35 title = NULL;
36 dllname = _T("");
37 filename = _T("");
38
39 if (fname!=NULL) funcname = fname;
40 else funcname = _T("");
41
42 hmod = NULL;
43 pgetinfo = NULL;
44 pfunc = NULL;
45 if (fnum>0) pfunc = (void**)malloc(sizeof(void*)*fnum);
46}
47
48
49
54{
55 status = 0;
56
57 hmod = NULL;
58 pgetinfo = NULL;
59 if (fnum>0 && pfunc!=NULL) ::free(pfunc);
60 fnum = 0;
61 pfunc = NULL;
62}
63
64
65
76BOOL DLLModule::set_funcname(LPCTSTR fname, int n)
77{
78 if (fname!=NULL) funcname = fname;
79 else funcname = _T("");
80
81 if (fnum>0 && pfunc!=NULL) ::free(pfunc);
82 pfunc = NULL;
83
84 fnum = n;
85 if (fnum>0) pfunc = (void**)malloc(sizeof(void*)*fnum);
86
87 if (fname==NULL || n<=0) return FALSE;
88 return TRUE;
89}
90
91
92
109{
110 if (lp==NULL) return FALSE;
111 if (funcname==_T("")) return FALSE;
112
113 status = 0;
114 hmod = (HMODULE)(lp->ldat.ptr);
115 char* mbstr = ts2mbs(funcname);
116 pgetinfo = (FUNC_STRING)GetProcAddress(hmod, mbstr);
117 ::free(mbstr);
118 if (pgetinfo==NULL) return FALSE;
119
120 dllname = mbs2ts((char*)(lp->ldat.key.buf));
121 filename = mbs2ts((char*)(lp->ldat.val.buf));
122 title = (*pgetinfo)(0);
123
124 pfunc[0] = (void*)pgetinfo;
125 for (int i=1; i<fnum; i++) {
126 char* nfn = (*pgetinfo)(i);
127 void* pfn = (void*)GetProcAddress(hmod, nfn);
128 pfunc[i] = pfn;
129 //if (pfn==NULL) {
130 // status = -1;
131 // return FALSE;
132 //}
133 }
134
135 status = 1;
136 return TRUE;
137}
138
139
140
149{
150 if (status<=0) return NULL;
151
152 if (n>=fnum) n = 0;
153 return pfunc[n];
154}
155
156
157
158
160// DLLModuleTBL
161//
162
170void DLLModuleTBL::init(LPCTSTR dirn)
171{
172 tnum = vnum = 0;
173 loaded = false;
174 lp = NULL;
175 dllmod = NULL;
176 dirname = _T("");
177
178 if (dirn!=NULL) load_module(dirn);
179}
180
181
182
187{
188
189 if (dllmod!=NULL) {
190 for (int i=0; i<vnum; i++) {
191 if (dllmod[i]!=NULL) {
192 dllmod[i]->free();
193 delete(dllmod[i]);
194 dllmod[i] = NULL;
195 }
196 }
197 }
198
199 tList* lt = lp;
200 while (lt!=NULL) {
201 if (lt->ldat.ptr!=NULL) {
202 FreeLibrary((HMODULE)(lt->ldat.ptr)); // 関数へのハンドル
203 lt->ldat.ptr = NULL;
204 lt->ldat.sz = 0;
205 }
206 lt = lt->next;
207 }
208 del_all_tList(&lp);
209
210 tnum = vnum = 0;
211 if (dllmod!=NULL) ::free(dllmod);
212 dllmod = NULL;
213 loaded = false;
214}
215
216
217
229{
230 if (dirn!=NULL) dirname = dirn;
231 else dirname = _T(".");
232
233 lp = get_dir_module((LPCTSTR)dirname);
234 if (lp==NULL) return FALSE;
235
236 tnum = 0;
237 tList* lt = lp;
238 while (lt!=NULL) {
239 tnum++;
240 lt = lt->next;
241 }
242 loaded = true;
243
244 return TRUE;
245}
246
247
248
262{
263 tList* lm;
264 tList* lt;
265
266 if (dirn==NULL) return NULL;
267 lm = lt = get_dir_files_t(dirn);
268 if (lm==NULL) return NULL;
269
270 while (lt!=NULL) {
271 HMODULE hmod = LoadLibrary((LPCTSTR)mbs2ts((char*)(lt->ldat.val.buf)));
272 lt->ldat.ptr = (void*)hmod;
273 char* fn = get_file_name((char*)(lt->ldat.val.buf));
274 lt->ldat.key = make_Buffer_bystr(fn);
275 lt = lt->next;
276 }
277
278 // ハンドラが NULLの場合は削除
279 lt = lm;
280 while (lt!=NULL) {
281 if (lt->ldat.ptr==NULL) {
282 tList* ln = lt;
283 if (lm==lt) lm = lt->next;
284 lt = lt->next;
285 del_tList_node(&ln);
286 }
287 else lt = lt->next;
288 }
289
290 return lm;
291}
292
293
294
303void DLLModuleTBL::make_module_tbl(LPCTSTR fname, int m)
304{
305 tList* lm = lp;
306
307 vnum = 0;
308 if (fname==NULL) return;
309
310 dllmod = (DLLModule**)malloc(sizeof(DLLModule*)*tnum);
311 if (dllmod==NULL) return;
312
313 int n = 0;
314 if (m<=0) m = 1;
315 while (lm!=NULL) {
316 dllmod[n] = this->new_module(fname, m);
317 int ret = dllmod[n]->get_module_info(lm);
318 if (ret) {
319 n++;
320 if (n>=tnum) break;
321 }
322 else {
323 delete(dllmod[n]);
324 dllmod[n] = NULL;
325 }
326 lm = lm->next;
327 }
328 vnum = n;
329
330 this->set_module_function();
331 return;
332}
333
334
335
void make_module_tbl(LPCTSTR fname, int n)
tList * get_dir_module(LPCTSTR dirn)
BOOL load_module(LPCTSTR dirn=NULL)
virtual void set_module_function()
Definition WinDLLTool.h:119
virtual DLLModule * new_module(LPCTSTR fname, int n)
Definition WinDLLTool.h:118
DLLModule ** dllmod
Definition WinDLLTool.h:95
void init(LPCTSTR dirn=NULL)
BOOL get_module_info(tList *lp)
BOOL set_funcname(LPCTSTR fname, int n)
void * get_funcptr(int n)
FUNC_STRING pgetinfo
Definition WinDLLTool.h:67
void init(LPCTSTR fname, int n)
char * ts2mbs(LPCTSTR str)
Definition WinTools.h:57
CString mbs2ts(char *str)
Definition WinTools.cpp:79
char *(* FUNC_STRING)(int)
Definition WinDLLTool.h:47
tList * get_dir_files_t(LPCTSTR dirn)
Definition WinTools.cpp:338