packages.cpp

Go to the documentation of this file.
00001 /* ****************************************************************** **
00002 **    OpenSees - Open System for Earthquake Engineering Simulation    **
00003 **          Pacific Earthquake Engineering Research Center            **
00004 **                                                                    **
00005 **                                                                    **
00006 ** (C) Copyright 1999, The Regents of the University of California    **
00007 ** All Rights Reserved.                                               **
00008 **                                                                    **
00009 ** Commercial use of this program without express permission of the   **
00010 ** University of California, Berkeley, is strictly prohibited.  See   **
00011 ** file 'COPYRIGHT'  in main directory for information on usage and   **
00012 ** redistribution,  and for a DISCLAIMER OF ALL WARRANTIES.           **
00013 **                                                                    **
00014 ** Developed by:                                                      **
00015 **   Frank McKenna (fmckenna@ce.berkeley.edu)                         **
00016 **   Gregory L. Fenves (fenves@ce.berkeley.edu)                       **
00017 **                                                                    **
00018 ** ****************************************************************** */
00019                                                                         
00020 // $Revision: 1.5 $
00021 // $Date: 2006/10/02 20:10:46 $
00022 // $Source: /usr/local/cvs/OpenSees/SRC/package/packages.cpp,v $
00023                                                                         
00024 // Written: fmk 
00025 // Created: 06/05
00026 
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <OPS_Globals.h>
00030 
00031 
00032 #ifdef _WIN32
00033 
00034 #include <windows.h>
00035 #else
00036 #include <dlfcn.h>
00037 #endif
00038 
00039 int 
00040 getLibraryFunction(const char *libName, const char *funcName, void **libHandle, void **funcHandle) {
00041 
00042   int result = 0;
00043   
00044   *libHandle = NULL;
00045   *funcHandle = NULL;
00046   
00047 #ifdef _WIN32
00048   
00049   //
00050   // first try and open dll
00051   //
00052   
00053   int libNameLength = strlen(libName);
00054   char *localLibName = new char[libNameLength+5];
00055   strcpy(localLibName, libName);
00056   strcpy(&localLibName[libNameLength], ".dll");
00057   
00058   HINSTANCE hLib = LoadLibrary(localLibName);
00059   
00060   delete [] localLibName;
00061   
00062   if (hLib != NULL) {
00063     
00064     char mod[124];
00065     GetModuleFileName((HMODULE)hLib, (LPTSTR)mod, 124);
00066     
00067     //
00068     // now look for function with funcName
00069     //
00070     
00071     (*funcHandle) = (void *)GetProcAddress((HMODULE)hLib, funcName);
00072     
00073     if (*funcHandle == NULL) {
00074       FreeLibrary((HMODULE)hLib);
00075       return -2;
00076     }   
00077     
00078     //
00079     // we need to set the OpenSees pointer global variables if function there
00080     //
00081     
00082     typedef void(_cdecl *setGlobalPointersFunction)(OPS_Stream *);
00083     setGlobalPointersFunction funcPtr;
00084     
00085     // look for pointer function
00086     funcPtr = (setGlobalPointersFunction)GetProcAddress((HMODULE)hLib,"setGlobalPointers");
00087     if (!funcPtr) {
00088       FreeLibrary((HMODULE)hLib);
00089       return -2;
00090     }
00091     
00092     // invoke pointer function
00093     (funcPtr)(opserrPtr);
00094     
00095   } else // no lib exists
00096     return -1;
00097   
00098   libHandle =  (void **)&hLib;
00099 
00100 #else
00101   
00102   int libNameLength = strlen(libName);
00103   char *localLibName = new char[libNameLength+4];
00104   strcpy(localLibName, libName);
00105   strcpy(&localLibName[libNameLength], ".so");
00106   
00107   char *error;
00108   *libHandle = dlopen (localLibName, RTLD_NOW);
00109 
00110   if (*libHandle != NULL) {    
00111     
00112     *funcHandle = dlsym(*libHandle, funcName);
00113     error = dlerror();
00114     if (funcHandle == NULL ) 
00115       if (error != NULL) {
00116       opserr << *error;
00117       opserr << "Could not find function: " << funcName << endln;
00118       result = -2; // could not find function
00119       } else {
00120         opserr << "Could not find function: " << funcName << endln;
00121         result = -1;
00122       }
00123 
00124   } else
00125     result = -3;
00126 
00127   delete [] localLibName;
00128   
00129 #endif
00130  
00131   return result;
00132 }

Generated on Mon Oct 23 15:05:24 2006 for OpenSees by doxygen 1.5.0