11 static char fail[300];
13 static ULONG dllHandle;
14 static int handle_found;
15 static int handle_loaded;
23 char *os2error(int rc);
27 #ifdef DLOPEN_INITTERM
28 unsigned long _DLL_InitTerm(unsigned long modHandle, unsigned long flag)
33 dllHandle = modHandle;
39 dllHandle = (unsigned long)NULLHANDLE;
52 static APIRET APIENTRY (*pDosQueryModFromEIP) (HMODULE * hmod, ULONG * obj, ULONG BufLen, PCHAR Buf,
53 ULONG * Offset, ULONG Address);
54 HMODULE doscalls_h, mod;
56 ULONG obj, offset, rc;
62 doscalls_h = (HMODULE)dlopen("DOSCALLS",0);
65 /* {&doscalls_handle, NULL, 360}, */ /* DosQueryModFromEIP */
66 rc = DosQueryProcAddr(doscalls_h, 360, 0, (PFN*)&pDosQueryModFromEIP);
69 rc = pDosQueryModFromEIP(&mod, &obj, sizeof(buf), buf, &offset, (ULONG)dlopen);
79 dlopen(const char *path, int mode)
83 const char *beg, *dot;
85 unsigned fpflag = _control87(0,0);
88 if (!path) { /* Our own handle. */
89 if (handle_found || find_myself()) {
93 return (void*)dllHandle;
94 rc = DosQueryModuleName(dllHandle, sizeof(dllname), dllname);
96 strcpy(fail, "can't find my DLL name by the handle");
100 rc = DosLoadModule(fail, sizeof fail, dllname, &handle);
102 strcpy(fail, "can't load my own DLL");
109 retcode = ERROR_MOD_NOT_FOUND;
110 strcpy(fail, "can't load from myself: compiled without -DDLOPEN_INITTERM");
113 if ((rc = DosLoadModule(fail, sizeof fail, (char*)path, &handle)) == 0)
118 if (strlen(path) >= sizeof(tmp))
121 /* Not found. Check for non-FAT name and try truncated name. */
122 /* Don't know if this helps though... */
123 for (beg = dot = path + strlen(path);
124 beg > path && !strchr(":/\\", *(beg-1));
131 memmove(tmp, path, n);
132 memmove(tmp+n, dot, strlen(dot)+1);
133 if (DosLoadModule(fail, sizeof fail, tmp, &handle) == 0)
139 _control87(fpflag, MCW_EM); /* Some modules reset FP flags on load */
140 return (void *)handle;
143 #define ERROR_WRONG_PROCTYPE 0xffffffff
146 dlsym(void *handle, const char *symbol)
152 rc = DosQueryProcAddr((HMODULE)handle, 0, symbol, &addr);
154 rc = DosQueryProcType((HMODULE)handle, 0, symbol, &type);
155 if (rc == 0 && type == PT_32BIT)
157 rc = ERROR_WRONG_PROCTYPE;
166 static char buf[700];
172 if (retcode == ERROR_WRONG_PROCTYPE)
173 err = "Wrong procedure type";
175 err = os2error(retcode);
177 if (len > sizeof(buf) - 1)
178 len = sizeof(buf) - 1;
179 strncpy(buf, err, len+1);
180 if (fail[0] && len + strlen(fail) < sizeof(buf) - 100)
181 sprintf(buf + len, ", possible problematic module: '%s'", fail);
187 dlclose(void *handle)
191 if ((rc = DosFreeModule((HMODULE)handle)) == 0) return 0;