Loads a dynamic module into memory for use. Returns true on success, false otherwise modfilename is the name of the module (without an extension such as DLL, or so)
| 317 | // Returns true on success, false otherwise |
| 318 | // modfilename is the name of the module (without an extension such as DLL, or so) |
| 319 | bool mod_LoadModule(module *handle, const char *imodfilename, int flags) { |
| 320 | if (!imodfilename) { |
| 321 | ModLastError = MODERR_OTHER; |
| 322 | return false; |
| 323 | } |
| 324 | if (!handle) { |
| 325 | ModLastError = MODERR_INVALIDHANDLE; |
| 326 | return false; |
| 327 | } |
| 328 | handle->handle = NULL; |
| 329 | char modfilename[_MAX_PATH]; |
| 330 | mod_GetRealModuleName(imodfilename, modfilename); |
| 331 | #if defined(WIN32) |
| 332 | handle->handle = LoadLibrary(modfilename); |
| 333 | if (!handle->handle) { |
| 334 | // There was an error loading the module |
| 335 | DWORD err = GetLastError(); |
| 336 | switch (err) { |
| 337 | case ERROR_DLL_INIT_FAILED: |
| 338 | ModLastError = MODERR_MODINITFAIL; |
| 339 | break; |
| 340 | case ERROR_DLL_NOT_FOUND: |
| 341 | ModLastError = MODERR_MODNOTFOUND; |
| 342 | break; |
| 343 | case ERROR_INVALID_DLL: |
| 344 | ModLastError = MODERR_INVALIDMOD; |
| 345 | break; |
| 346 | default: |
| 347 | ModLastError = MODERR_OTHER; |
| 348 | break; |
| 349 | } |
| 350 | return false; |
| 351 | } |
| 352 | #elif defined(POSIX) |
| 353 | int f = 0; |
| 354 | if (flags & MODF_LAZY) |
| 355 | f |= RTLD_LAZY; |
| 356 | if (flags & MODF_NOW) |
| 357 | f |= RTLD_NOW; |
| 358 | if (flags & MODF_GLOBAL) |
| 359 | f |= RTLD_GLOBAL; |
| 360 | handle->handle = dlopen(modfilename, f); |
| 361 | if (!handle->handle) { |
| 362 | // ok we couldn't find the given name...try other ways |
| 363 | char dir[_MAX_PATH], fname[_MAX_PATH], nname[_MAX_PATH], ext[64]; |
| 364 | dd_SplitPath(modfilename, dir, fname, ext); |
| 365 | strcat(fname, ext); |
| 366 | |
| 367 | if (!mod_FindRealFileNameCaseInsenstive(dir, fname, nname)) { |
| 368 | mprintf(0, "Module Load Err: %s\n", dlerror()); |
| 369 | ModLastError = MODERR_MODNOTFOUND; |
| 370 | return false; |
| 371 | } else { |
| 372 | // ok we have a different filename |
| 373 | dd_MakePath(modfilename, dir, nname, NULL); |
| 374 | mprintf(0, "MOD: Attempting to open %s instead of %s\n", modfilename, fname); |
| 375 | handle->handle = dlopen(modfilename, f); |
| 376 | if (!handle->handle) { |
no test coverage detected