1 | /* $Id: windlllx.cpp,v 1.2 1999-09-18 17:47:10 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 LX Dll class (compiled in OS/2 using Odin32 api)
|
---|
5 | *
|
---|
6 | * Copyright 1999 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * TODO: Unloading of dlls probably needs to be fixed due to OS/2 bug
|
---|
10 | * (wrong unload order of dlls)
|
---|
11 | *
|
---|
12 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
13 | *
|
---|
14 | */
|
---|
15 | #define INCL_DOSFILEMGR /* File Manager values */
|
---|
16 | #define INCL_DOSERRORS /* DOS Error values */
|
---|
17 | #define INCL_DOSPROCESS /* DOS Process values */
|
---|
18 | #define INCL_DOSMODULEMGR
|
---|
19 | #define INCL_DOSMISC /* DOS Miscellanous values */
|
---|
20 | #define INCL_WIN
|
---|
21 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <string.h>
|
---|
24 | #include <stdlib.h>
|
---|
25 | #include <iostream.h>
|
---|
26 | #include <fstream.h>
|
---|
27 | #include <misc.h>
|
---|
28 | #include <win32type.h>
|
---|
29 | #include <windllbase.h>
|
---|
30 | #include <windlllx.h>
|
---|
31 | #include <odinlx.h>
|
---|
32 | #include "oslibmisc.h"
|
---|
33 |
|
---|
34 | //******************************************************************************
|
---|
35 | //Create LX Dll object and send process attach message
|
---|
36 | //System dlls set EntryPoint to 0
|
---|
37 | //******************************************************************************
|
---|
38 | BOOL WIN32API RegisterLxDll(HINSTANCE hInstance, WIN32DLLENTRY EntryPoint,
|
---|
39 | PVOID pResData)
|
---|
40 | {
|
---|
41 | Win32LxDll *windll;
|
---|
42 |
|
---|
43 | windll = (Win32LxDll *)Win32DllBase::findModule(hInstance);
|
---|
44 | if(windll) {
|
---|
45 | char *name = OSLibGetDllName(hInstance);
|
---|
46 | dprintf(("RegisterLxDll: Register existing dll %x %s", hInstance, name));
|
---|
47 | return FALSE;
|
---|
48 | }
|
---|
49 | windll = new Win32LxDll(hInstance, EntryPoint, pResData);
|
---|
50 | if(windll == NULL) {
|
---|
51 | dprintf(("RegisterLxDll: windll == NULL!!!"));
|
---|
52 | return FALSE;
|
---|
53 | }
|
---|
54 | return windll->attachProcess();
|
---|
55 | }
|
---|
56 | //******************************************************************************
|
---|
57 | //Destroy LX Dll object
|
---|
58 | //******************************************************************************
|
---|
59 | BOOL WIN32API UnregisterLxDll(HINSTANCE hInstance)
|
---|
60 | {
|
---|
61 | #if 1
|
---|
62 | return TRUE;
|
---|
63 | #else
|
---|
64 | Win32LxDll *windll;
|
---|
65 |
|
---|
66 | windll = (Win32LxDll *)Win32DllBase::findModule(hInstance);
|
---|
67 | if(!windll) {
|
---|
68 | dprintf(("UnregisterLxDll: Can't find dll with handle %x!!!", hInstance));
|
---|
69 | return FALSE;
|
---|
70 | }
|
---|
71 | windll->detachProcess();
|
---|
72 | delete windll;
|
---|
73 | return TRUE;
|
---|
74 | #endif
|
---|
75 | }
|
---|
76 | //******************************************************************************
|
---|
77 | //******************************************************************************
|
---|
78 | Win32LxDll::Win32LxDll(HINSTANCE hInstance, WIN32DLLENTRY EntryPoint, PVOID pResData)
|
---|
79 | : Win32ImageBase(hInstance),
|
---|
80 | Win32LxImage(hInstance, pResData),
|
---|
81 | Win32DllBase(hInstance, EntryPoint)
|
---|
82 | {
|
---|
83 | if(EntryPoint == NULL) {
|
---|
84 | fSkipEntryCalls = TRUE;
|
---|
85 | fAttachedToProcess = TRUE;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | //******************************************************************************
|
---|
89 | //******************************************************************************
|
---|
90 | Win32LxDll::~Win32LxDll()
|
---|
91 | {
|
---|
92 | dprintf(("Win32LxDll::~Win32LxDll %s", szModule));
|
---|
93 | }
|
---|
94 | //******************************************************************************
|
---|
95 | //ASSUMPTION: called by FreeLibrary
|
---|
96 | //******************************************************************************
|
---|
97 | ULONG Win32LxDll::Release()
|
---|
98 | {
|
---|
99 | ULONG ret = --referenced;
|
---|
100 |
|
---|
101 | if(ret == 0) {
|
---|
102 | dprintf(("Win32LxDll::Release, referenced == 0\n"));
|
---|
103 | //DosFreeModule sends a termination message to the dll.
|
---|
104 | //The LX dll informs us when it's removed (UnregisterDll call)
|
---|
105 | DosFreeModule(hinstance);
|
---|
106 | }
|
---|
107 | return(ret);
|
---|
108 | }
|
---|
109 | //******************************************************************************
|
---|
110 | //******************************************************************************
|
---|
111 | ULONG Win32LxDll::getApi(char *name)
|
---|
112 | {
|
---|
113 | APIRET rc;
|
---|
114 | ULONG apiaddr;
|
---|
115 |
|
---|
116 | rc = DosQueryProcAddr(hinstance, 0, name, (PFN *)&apiaddr);
|
---|
117 | if(rc)
|
---|
118 | {
|
---|
119 | if(rc == ERROR_INVALID_HANDLE)
|
---|
120 | {//handle invalid for some silly reason, so load module again (initterm entrypoint not called twice)
|
---|
121 | char szErrName[CCHMAXPATH];
|
---|
122 |
|
---|
123 | rc = DosLoadModule(szErrName, sizeof(szErrName), szFileName, &hinstance);
|
---|
124 | if(!rc)
|
---|
125 | rc = DosQueryProcAddr(hinstance, 0, name, (PFN *)&apiaddr);
|
---|
126 | }
|
---|
127 | if(rc) return(0);
|
---|
128 | }
|
---|
129 | return(apiaddr);
|
---|
130 | }
|
---|
131 | //******************************************************************************
|
---|
132 | //******************************************************************************
|
---|
133 | ULONG Win32LxDll::getApi(int ordinal)
|
---|
134 | {
|
---|
135 | APIRET rc;
|
---|
136 | ULONG apiaddr;
|
---|
137 |
|
---|
138 | rc = DosQueryProcAddr(hinstance, ordinal, NULL, (PFN *)&apiaddr);
|
---|
139 | if(rc) {
|
---|
140 | if(rc == ERROR_INVALID_HANDLE)
|
---|
141 | {//handle invalid for some silly reason, so load module again (initterm entrypoint not called twice)
|
---|
142 | char szErrName[CCHMAXPATH];
|
---|
143 |
|
---|
144 | rc = DosLoadModule(szErrName, sizeof(szErrName), szFileName, &hinstance);
|
---|
145 | if(!rc)
|
---|
146 | rc = DosQueryProcAddr(hinstance, ordinal, NULL, (PFN *)&apiaddr);
|
---|
147 | }
|
---|
148 | if(rc) return(0);
|
---|
149 | }
|
---|
150 | return(apiaddr);
|
---|
151 | }
|
---|
152 | //******************************************************************************
|
---|
153 | //******************************************************************************
|
---|
154 | BOOL Win32LxDll::isLxDll()
|
---|
155 | {
|
---|
156 | return TRUE;
|
---|
157 | }
|
---|
158 | //******************************************************************************
|
---|
159 | //******************************************************************************
|
---|