PUBLIC int mprLoadNativeModule(MprModule *mp) { MprModuleEntry fn; MprPath info; char *at; void *handle; assert(mp); /* Search the image incase the module has been statically linked */ #ifdef RTLD_DEFAULT handle = RTLD_DEFAULT; #else #ifdef RTLD_MAIN_ONLY handle = RTLD_MAIN_ONLY; #else handle = 0; #endif #endif if (!mp->entry || !dlsym(handle, mp->entry)) { if ((at = mprSearchForModule(mp->path)) == 0) { mprError("Cannot find module \"%s\", cwd: \"%s\", search path \"%s\"", mp->path, mprGetCurrentPath(), mprGetModuleSearchPath()); return MPR_ERR_CANT_ACCESS; } mp->path = at; mprGetPathInfo(mp->path, &info); mp->modified = info.mtime; mprLog(2, "Loading native module %s", mprGetPathBase(mp->path)); if ((handle = dlopen(mp->path, RTLD_LAZY | RTLD_GLOBAL)) == 0) { mprError("Cannot load module %s\nReason: \"%s\"", mp->path, dlerror()); return MPR_ERR_CANT_OPEN; } mp->handle = handle; } else if (mp->entry) { mprLog(2, "Activating native module %s", mp->name); } if (mp->entry) { if ((fn = (MprModuleEntry) dlsym(handle, mp->entry)) != 0) { if ((fn)(mp->moduleData, mp) < 0) { mprError("Initialization for module %s failed", mp->name); dlclose(handle); return MPR_ERR_CANT_INITIALIZE; } } else { mprError("Cannot load module %s\nReason: can't find function \"%s\"", mp->path, mp->entry); dlclose(handle); return MPR_ERR_CANT_READ; } } return 0; }
PUBLIC int mprLoadNativeModule(MprModule *mp) { MprModuleEntry fn; void *handle; assert(mp); if ((handle = (HANDLE) MPR->appInstance) == 0) { handle = GetModuleHandle(NULL); } if (!handle || !mp->entry || !GetProcAddress(handle, mp->entry)) { #if ME_STATIC mprLog("error mpr", 0, "Cannot load module %s, product built static", mp->name); return MPR_ERR_BAD_STATE; #else MprPath info; char *at, *baseName; if ((at = mprSearchForModule(mp->path)) == 0) { mprLog("error mpr", 0, "Cannot find module \"%s\", cwd=\"%s\", search=\"%s\"", mp->path, mprGetCurrentPath(), mprGetModuleSearchPath()); return MPR_ERR_CANT_ACCESS; } mp->path = at; mprGetPathInfo(mp->path, &info); mp->modified = info.mtime; baseName = mprGetPathBase(mp->path); mprLog("info mpr", 4, "Loading native module %s", baseName); if ((handle = LoadLibrary(wide(mp->path))) == 0) { mprLog("error mpr", 0, "Cannot load module %s, errno=\"%d\"", mp->path, mprGetOsError()); return MPR_ERR_CANT_READ; } mp->handle = handle; #endif /* !ME_STATIC */ } else if (mp->entry) { mprLog("info mpr", 4, "Activating native module %s", mp->name); } if (mp->entry) { if ((fn = (MprModuleEntry) GetProcAddress((HINSTANCE) handle, mp->entry)) == 0) { mprLog("error mpr", 0, "Cannot load module %s, cannot find function \"%s\"", mp->name, mp->entry); FreeLibrary((HINSTANCE) handle); return MPR_ERR_CANT_ACCESS; } if ((fn)(mp->moduleData, mp) < 0) { mprLog("error mpr", 0, "Initialization for module %s failed", mp->name); FreeLibrary((HINSTANCE) handle); return MPR_ERR_CANT_INITIALIZE; } } return 0; }
MprModule *mprLoadModule(MprCtx ctx, cchar *moduleName, cchar *initFunction) { MprModule *mp; MprModuleEntry fn; char *module; char *path, *name; void *handle; mprAssert(moduleName && *moduleName); mp = 0; name = path = 0; module = mprGetNormalizedPath(ctx, moduleName); if (mprSearchForModule(ctx, module, &path) < 0) { mprError(ctx, "Can't find module \"%s\" in search path \"%s\"", moduleName, mprGetModuleSearchPath(ctx)); } else { name = mprGetPathBase(ctx, module); mprLog(ctx, MPR_INFO, "Loading module %s from %s", name, path); if ((handle = GetModuleHandle(name)) == 0 && (handle = LoadLibrary(path)) == 0) { mprError(ctx, "Can't load module %s\nReason: \"%d\"\n", path, mprGetOsError()); } else if (initFunction) { if ((fn = (MprModuleEntry) GetProcAddress((HINSTANCE) handle, initFunction)) != 0) { if ((mp = (fn)(ctx, path)) == 0) { mprError(ctx, "Initialization for module %s failed", name); FreeLibrary((HINSTANCE) handle); } else { mp->handle = handle; } } else { mprError(ctx, "Can't load module %s\nReason: can't find function \"%s\"\n", name, initFunction); FreeLibrary((HINSTANCE) handle); } } } mprFree(name); mprFree(path); mprFree(module); return mp; }