예제 #1
0
/*--------------------------------------------------------------------------*/
BOOL LoadFunctionsJVM(char *filedynlib)
{
#ifdef _MSC_VER
    if (filedynlib == NULL)
    {
        hLibJVM = LoadDynLibraryW(L"jvm.dll");
    }
    else
    {
        wchar_t * wcfiledynlib = to_wide_string(filedynlib);
        if (wcfiledynlib)
        {
            hLibJVM = LoadDynLibraryW(wcfiledynlib);
            FREE(wcfiledynlib);
            wcfiledynlib = NULL;
        }
    }
#else
#ifdef __APPLE__
    /*
    ** After MacOSX 10.6.8 manually load libjava.jnilib make JNI_* functions crash
    ** Rely on OS by using dlopen(NULL) to find correct symbol with dlsym
    */
    hLibJVM = LoadDynLibrary(NULL);
#else
    if (filedynlib == NULL)
    {
        hLibJVM = LoadDynLibrary(NULL);
    }
    else
    {
        hLibJVM = LoadDynLibrary(filedynlib);
    }
#endif
#endif

    if (hLibJVM)
    {
        ptr_JNI_GetDefaultJavaVMInitArgs = (JNI_GetDefaultJavaVMInitArgsPROC) GetDynLibFuncPtr(hLibJVM, "JNI_GetDefaultJavaVMInitArgs" );
        ptr_JNI_CreateJavaVM = (JNI_CreateJavaVMPROC) GetDynLibFuncPtr(hLibJVM, "JNI_CreateJavaVM" );
        ptr_JNI_GetCreatedJavaVMs = (JNI_GetCreatedJavaVMsPROC) GetDynLibFuncPtr(hLibJVM, "JNI_GetCreatedJavaVMs" );

        if (ptr_JNI_GetDefaultJavaVMInitArgs && ptr_JNI_CreateJavaVM && ptr_JNI_GetCreatedJavaVMs)
        {
            bSymbolsLoaded = TRUE;
            return TRUE;
        }
    }
    return FALSE;
}
예제 #2
0
/*---------------------------------------------------------------------------*/
int Sci_dlopen( char *loaded_file)
{
    static DynLibHandle  hd1 = NULL;
    int i = 0;


#ifdef _MSC_VER
    {
        wchar_t *wcfilename = to_wide_string(loaded_file);
        if (wcfilename)
        {
            hd1 = LoadDynLibraryW(wcfilename);
            FREE(wcfilename);
            wcfilename = NULL;
        }
    }
#else
    hd1 = LoadDynLibrary (loaded_file);
#endif

    if ( hd1 == NULL )
    {
        return -1 ;    /* the shared archive was not loaded. */
    }

    for ( i = 0 ; i < Nshared ; i++ )
    {
        if ( hd[i].ok == FALSE)
        {
            hd[i].shl =  (unsigned long long)hd1;
            hd[i].ok = TRUE;
            return(i);
        }
    }

    if ( Nshared == ENTRYMAX )
    {
        if (getIlibVerboseLevel() != ILIB_VERBOSE_NO_OUTPUT)
        {
            sciprint(_("Cannot open shared files max entry %d reached.\n"), ENTRYMAX);
        }
        return(FALSE);
    }

    hd[Nshared].shl = (unsigned long long)hd1;
    hd[Nshared].ok = TRUE;
    Nshared ++;

    return Nshared - 1;
}
예제 #3
0
/*--------------------------------------------------------------------------*/
dynamic_gateway_error_code callDynamicGateway(char *moduleName,
        char *dynLibName,
        char *gw_name,
        DynLibHandle *hlib,
        PROC_GATEWAY *ptrGateway)
{
    if (*hlib == NULL)
    {
#ifdef _MSC_VER
        wchar_t *wcdynLibName = to_wide_string(dynLibName);
        if (wcdynLibName)
        {
            *hlib = LoadDynLibraryW(wcdynLibName);
            FREE(wcdynLibName);
            wcdynLibName = NULL;
        }
        if (*hlib == NULL)
        {
            return DYN_GW_LOAD_LIBRARY_ERROR;
        }
#else

        /* First step, we are considering that we are in the source tree.
         * Therefor, the lib should be in modules/xxx/.libs/
         *
         * Otherwise, dlopen will search in various places (for example, the install specified
         * by --prefix).
         * This leads to serious and unexpected bugs like #8883
         * The original bug report for this issue was the bug #2875
         */
        char *SciPath = getSCIpath();
#define PATHTOMODULE "/modules/"
#ifndef LT_OBJDIR
#define LT_OBJDIR ".libs/"
#endif

        /* Build the full path to the library */
        char *pathToLib = (char*) MALLOC((strlen(SciPath) + strlen(PATHTOMODULE) + strlen(moduleName) + strlen("/") + strlen(LT_OBJDIR) + strlen(dynLibName) + 1) * sizeof(char));
        sprintf(pathToLib, "%s%s%s/%s%s", SciPath, PATHTOMODULE, moduleName, LT_OBJDIR, dynLibName);

        /* Load the library with the Scilab source-tree paths */
        *hlib = LoadDynLibrary(pathToLib);

        if (*hlib == NULL) /* Load of the hardcoded path to the lib failed */
        {

            if (FileExist(pathToLib))
            {
                /* It fails but the file exists */
                char *previousError = GetLastDynLibError();
                if (previousError != NULL)
                {
                    sciprint("A previous error has been detected while loading %s: %s\n", dynLibName, previousError);
                }
            }

            /* Under Linux/Unix, load thanks to dlopen on the system.
             * In the binary, the LD_LIBRARY_PATH is declared in the startup script (ie bin/scilab*)
             * Note that it is not possible to update the LD_LIBRARY_PATH at run time.
             */
            *hlib = LoadDynLibrary(dynLibName);
            if (*hlib == NULL)
            {
                char *previousError = GetLastDynLibError();
                if (previousError != NULL)
                {
                    sciprint("A previous error has been detected while loading %s: %s\n", dynLibName, previousError);
                }
                if (SciPath)
                {
                    FREE(SciPath);
                    SciPath = NULL;
                }
                if (pathToLib)
                {
                    FREE(pathToLib);
                    pathToLib = NULL;
                }
                return DYN_GW_LOAD_LIBRARY_ERROR;
            }
        }
        if (SciPath)
        {
            FREE(SciPath);
            SciPath = NULL;
        }
        if (pathToLib)
        {
            FREE(pathToLib);
            pathToLib = NULL;
        }
#endif
    }

    if (*ptrGateway == NULL)
    {
        *ptrGateway = (PROC_GATEWAY) GetDynLibFuncPtr(*hlib, gw_name);
        if (*ptrGateway == NULL)
        {
            return DYN_GW_PTR_FUNCTION_ERROR ;
        }
    }

    if ( (*hlib) && (*ptrGateway) )
    {
        (*ptrGateway)();
        return DYN_GW_NO_ERROR;
    }

    return DYN_GW_CALL_FUNCTION_ERROR;
}
예제 #4
0
Callable::ReturnValue DynamicFunction::Init()
{
    /*Load library*/
    if (m_wstLibName.empty())
    {
        Scierror(999, _("%s: Library name must not be empty\n."), m_wstName.c_str());
        return Error;
    }

    DynLibHandle hLib = getDynModule(m_wstLibName.c_str());
    if (hLib == 0)
    {
        char* pstLibName = wide_string_to_UTF8(m_wstLibName.c_str());
        hLib = LoadDynLibrary(pstLibName);

        if (hLib == 0)
        {
            //2nd chance for linux !
#ifndef _MSC_VER
            char* pstError = strdup(GetLastDynLibError());

            /* Haven't been able to find the lib with dlopen...
            * This can happen for two reasons:
            * - the lib must be dynamically linked
            * - Some silly issues under Suse (see bug #2875)
            * Note that we are handling only the "source tree build"
            * because libraries are split (they are in the same directory
            * in the binary)
            */
            wchar_t* pwstScilabPath = getSCIW();
            wchar_t pwstModulesPath[] = L"/modules/";
            wchar_t pwstLTDir[] =  L".libs/";

            /* Build the full path to the library */
            int iPathToLibLen = (wcslen(pwstScilabPath) + wcslen(pwstModulesPath) + wcslen(m_wstModule.c_str()) + wcslen(L"/") + wcslen(pwstLTDir) + wcslen(m_wstLibName.c_str()) + 1);
            wchar_t* pwstPathToLib = (wchar_t*)MALLOC(iPathToLibLen * sizeof(wchar_t));
            os_swprintf(pwstPathToLib, iPathToLibLen, L"%ls%ls%ls/%ls%ls", pwstScilabPath, pwstModulesPath, m_wstModule.c_str(), pwstLTDir, m_wstLibName.c_str());
            FREE(pwstScilabPath);
            char* pstPathToLib = wide_string_to_UTF8(pwstPathToLib);
            FREE(pwstPathToLib);
            hLib = LoadDynLibrary(pstPathToLib);

            if (hLib == 0)
            {
                Scierror(999, _("An error has been detected while loading %s: %s\n"), pstLibName, pstError);
                FREE(pstError);

                pstError = GetLastDynLibError();
                Scierror(999, _("An error has been detected while loading %s: %s\n"), pstPathToLib, pstError);

                FREE(pstLibName);
                FREE(pstPathToLib);
                return Error;
            }
            FREE(pstPathToLib);
            FREE(pstError);
#else
            char* pstError = wide_string_to_UTF8(m_wstLibName.c_str());
            Scierror(999, _("Impossible to load %s library\n"), pstError);
            FREE(pstError);
            FREE(pstLibName);
            return Error;
#endif
        }
        FREE(pstLibName);
        addDynModule(m_wstLibName.c_str(), hLib);

        /*Load deps*/
        if (m_wstLoadDepsName.empty() == false && m_pLoadDeps == NULL)
        {
            char* pstLoadDepsName = wide_string_to_UTF8(m_wstLoadDepsName.c_str());
            m_pLoadDeps = (LOAD_DEPS)GetDynLibFuncPtr(hLib, pstLoadDepsName);
            FREE(pstLoadDepsName);
        }

    }

    /*Load gateway*/
    if (m_wstName != L"")
    {
        char* pstEntryPoint = wide_string_to_UTF8(m_wstEntryPoint.c_str());
        switch (m_iType)
        {
            case EntryPointCPPOpt :
                m_pOptFunc = (GW_FUNC_OPT)GetDynLibFuncPtr(hLib, pstEntryPoint);
                break;
            case EntryPointCPP :
                m_pFunc = (GW_FUNC)GetDynLibFuncPtr(hLib, pstEntryPoint);
                break;
            case EntryPointOldC :
                m_pOldFunc = (OLDGW_FUNC)GetDynLibFuncPtr(hLib, pstEntryPoint);
                break;
            case EntryPointMex:
                m_pMexFunc = (MEXGW_FUNC)GetDynLibFuncPtr(hLib, pstEntryPoint);
                break;
            case EntryPointC:
                m_pCFunc = (GW_C_FUNC)GetDynLibFuncPtr(hLib, pstEntryPoint);
                break;
        }

        FREE(pstEntryPoint);
    }

    if (m_pFunc == NULL && m_pOldFunc == NULL && m_pMexFunc == NULL && m_pOptFunc == NULL && m_pCFunc == NULL)
    {
        char* pstEntry = wide_string_to_UTF8(m_wstEntryPoint.c_str());
        char* pstLib = wide_string_to_UTF8(m_wstLibName.c_str());
        Scierror(999, _("Impossible to load %s function in %s library: %s\n"), pstEntry, pstLib, GetLastDynLibError());
        FREE(pstEntry);
        FREE(pstLib);
        return Error;
    }

    switch (m_iType)
    {
        case EntryPointCPPOpt :
            m_pFunction = new OptFunction(m_wstName, m_pOptFunc, m_pLoadDeps, m_wstModule);
            break;
        case EntryPointCPP :
            m_pFunction = new Function(m_wstName, m_pFunc, m_pLoadDeps, m_wstModule);
            break;
        case EntryPointOldC :
            m_pFunction = new WrapFunction(m_wstName, m_pOldFunc, m_pLoadDeps, m_wstModule);
            break;
        case EntryPointMex:
            m_pFunction = new WrapMexFunction(m_wstName, m_pMexFunc, m_pLoadDeps, m_wstModule);
            break;
        case EntryPointC:
            m_pFunction = new WrapCFunction(m_wstName, m_pCFunc, m_pLoadDeps, m_wstModule);
            break;
    }

    if (m_pFunction == NULL)
    {
        return Error;
    }
    return OK;
}