TUM-Logo

libRASCH

 

Home
 

General

About libRASCH/News
Design
Screen shots
Sample programs (with source code)
License
 

Download

libRASCH
Tools
 

Documentation

User
Developer
 

Resources

Mailing list
Supported Formats
Plugins
Status
Links
 
Contact
About this site
 
Last updated
Tue Mar 27 23:03:52 2007
libRASCH: handle_plugin_win32.c Source File

handle_plugin_win32.c

Go to the documentation of this file.
00001 
00010 /*----------------------------------------------------------------------------
00011  *
00012  * Copyright (C) 2002, Raphael Schneider
00013  * See the file COPYING for information on usage and redistribution.
00014  *
00015  * $Header: /home/cvs_repos.bak/librasch/src/handle_plugin_win32.c,v 1.7 2004/06/30 16:44:26 rasch Exp $
00016  *
00017  *--------------------------------------------------------------------------*/
00018 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <string.h>
00022 #include <errno.h>
00023 
00024 #include <windows.h>
00025 
00026 #include <ra_priv.h>
00027 #include <handle_plugin.h>
00028 #include <pl_general.h>
00029 
00030 
00038 int
00039 read_plugins(struct librasch *ra)
00040 {
00041         char mask[MAX_PATH_RA];
00042         WIN32_FIND_DATA find;
00043         HANDLE h;
00044         struct plugin_struct *phead = NULL;
00045         struct plugin_struct *pcurr = phead;
00046 
00047         sprintf(mask, "%s\\*.dll", ra->config.plugin_dir);
00048         h = FindFirstFile(mask, &find);
00049         if (h == INVALID_HANDLE_VALUE)
00050                 return -1;
00051 
00052         do
00053         {
00054                 char path[MAX_PATH_RA];
00055                 struct plugin_struct *pnew;
00056 
00057                 sprintf(path, "%s%c%s", ra->config.plugin_dir, SEPERATOR, find.cFileName);
00058 
00059                 /* check if d_name is not a dir */
00060                 if (find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
00061                         continue;
00062 
00063                 /* no -> try to load the plugin */
00064                 pnew = dload_plugin(path);
00065                 if (!pnew)
00066                         continue;
00067                 pnew->ra = ra;
00068 
00069                 if (pcurr)
00070                 {
00071                         pcurr->next = pnew;
00072                         pnew->prev = pcurr;
00073                         pcurr = pcurr->next;
00074                 }
00075                 else
00076                         phead = pcurr = pnew;
00077         }
00078         while (FindNextFile(h, &find));
00079         FindClose(h);
00080         ra->pl_head = phead;
00081 
00082         /* now run init-functions for every plugin */
00083         pcurr = phead;
00084         while (pcurr)
00085         {
00086                 int (*init)(struct librasch *, struct plugin_struct *);
00087                 struct plugin_struct *p;
00088 
00089                 init = (int (*)(struct librasch *, struct plugin_struct *))
00090                         GetProcAddress(pcurr->dl, INIT_PLUGIN_FUNC);
00091                 if (!init)
00092                 {
00093                         LPVOID lpMsgBuf;
00094                         FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00095                                       FORMAT_MESSAGE_FROM_SYSTEM |
00096                                       FORMAT_MESSAGE_IGNORE_INSERTS,
00097                                       NULL, GetLastError(),
00098                                       MAKELANGID(LANG_NEUTRAL,
00099                                                  SUBLANG_DEFAULT), (LPTSTR) & lpMsgBuf, 0, NULL);
00100 
00101                         fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, (LPCTSTR) lpMsgBuf);
00102                         LocalFree(lpMsgBuf);
00103                         FreeLibrary(pcurr->dl);
00104                         if (pcurr->prev)
00105                                 pcurr->prev->next = pcurr->next;
00106                         if (pcurr == phead)
00107                                 phead = pcurr->next;
00108                         p = pcurr;
00109                         pcurr = pcurr->next;
00110                         free(p);
00111                         continue;
00112                 }
00113 
00114                 if ((*init)(ra, pcurr) != 0)
00115                 {
00116                         LPVOID lpMsgBuf;
00117                         FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00118                                       FORMAT_MESSAGE_FROM_SYSTEM |
00119                                       FORMAT_MESSAGE_IGNORE_INSERTS,
00120                                       NULL, GetLastError(),
00121                                       MAKELANGID(LANG_NEUTRAL,
00122                                                  SUBLANG_DEFAULT), (LPTSTR) & lpMsgBuf, 0, NULL);
00123 
00124                         fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, (LPCTSTR) lpMsgBuf);
00125                         LocalFree(lpMsgBuf);
00126                         FreeLibrary(pcurr->dl);
00127                         if (pcurr->prev)
00128                                 pcurr->prev->next = pcurr->next;
00129                         if (pcurr == phead)
00130                                 phead = pcurr->next;
00131                         p = pcurr;
00132                         pcurr = pcurr->next;
00133                         free(p);
00134                         continue;
00135                 }
00136                 pcurr = pcurr->next;
00137         }
00138 
00139         ra->pl_head = phead;
00140 
00141         return 0;
00142 } /* read_plugins() */
00143 
00144 
00151 struct plugin_struct *
00152 dload_plugin(char *file)
00153 {
00154         int ret = 0;
00155         struct plugin_struct *p;
00156         int (*load) (struct plugin_struct *);
00157 
00158         p = malloc(sizeof(struct plugin_struct));
00159         if (!p)
00160                 return NULL;
00161         memset(p, 0, sizeof(struct plugin_struct));
00162         p->handle_id = RA_HANDLE_PLUGIN;
00163 
00164         p->dl = LoadLibrary(file);
00165         if (!p->dl)
00166         {
00167                 LPVOID lpMsgBuf;
00168                 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00169                               FORMAT_MESSAGE_FROM_SYSTEM |
00170                               FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
00171                               GetLastError(), MAKELANGID(LANG_NEUTRAL,
00172                                                          SUBLANG_DEFAULT),
00173                               (LPTSTR) & lpMsgBuf, 0, NULL);
00174                 fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, (LPCTSTR) lpMsgBuf);
00175                 LocalFree(lpMsgBuf);
00176                 free(p);
00177                 return NULL;
00178         }
00179 
00180         load = (int (*)(struct plugin_struct *)) GetProcAddress(p->dl, LOAD_PLUGIN_FUNC);
00181         if (!load)
00182         {
00183                 LPVOID lpMsgBuf;
00184                 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00185                               FORMAT_MESSAGE_FROM_SYSTEM |
00186                               FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
00187                               GetLastError(), MAKELANGID(LANG_NEUTRAL,
00188                                                          SUBLANG_DEFAULT),
00189                               (LPTSTR) & lpMsgBuf, 0, NULL);
00190                 fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, (LPCTSTR) lpMsgBuf);
00191                 LocalFree(lpMsgBuf);
00192                 FreeLibrary(p->dl);
00193                 free(p);
00194                 return NULL;
00195         }
00196 
00197         if ((ret = (*load) (p)) != 0)
00198         {
00199                 LPVOID lpMsgBuf;
00200                 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00201                               FORMAT_MESSAGE_FROM_SYSTEM |
00202                               FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
00203                               GetLastError(), MAKELANGID(LANG_NEUTRAL,
00204                                                          SUBLANG_DEFAULT),
00205                               (LPTSTR) & lpMsgBuf, 0, NULL);
00206                 fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, (LPCTSTR) lpMsgBuf);
00207                 LocalFree(lpMsgBuf);
00208                 FreeLibrary(p->dl);
00209                 free(p);
00210                 return NULL;
00211         }
00212 
00213         if (!is_plugin_ok(p))
00214         {
00215                 LPVOID lpMsgBuf;
00216                 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00217                               FORMAT_MESSAGE_FROM_SYSTEM |
00218                               FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
00219                               GetLastError(), MAKELANGID(LANG_NEUTRAL,
00220                                                          SUBLANG_DEFAULT),
00221                               (LPTSTR) & lpMsgBuf, 0, NULL);
00222                 fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, (LPCTSTR) lpMsgBuf);
00223                 LocalFree(lpMsgBuf);
00224                 FreeLibrary(p->dl);
00225                 free(p);
00226                 return NULL;
00227         }
00228 
00229         strncpy(p->info.file, file, MAX_PATH_RA);
00230         p->info.use_plugin = 1;
00231 
00232         return p;
00233 } /* dload_plugin() */
00234 
00235 
00242 int
00243 is_plugin_ok(struct plugin_struct *p)
00244 {
00245         return 1;
00246 } /* is_plugin_ok() */
00247 
00248 
00256 void
00257 close_plugins(struct plugin_struct *p)
00258 {
00259         struct plugin_struct *next;
00260         void (*close_func) ();
00261 
00262         while (p)
00263         {
00264                 if (p->info.res)
00265                         ra_free_mem(p->info.res);
00266                 if (p->info.opt)
00267                         ra_free_mem(p->info.opt);
00268 
00269                 close_func = (void (*)()) GetProcAddress(p->dl, FINI_PLUGIN_FUNC);
00270                 if (close_func)
00271                         (*close_func) ();
00272                 if (FreeLibrary(p->dl) == 0)
00273                 {
00274                         LPVOID lpMsgBuf;
00275                         FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00276                                                         FORMAT_MESSAGE_FROM_SYSTEM |
00277                                                         FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
00278                                                         GetLastError(), MAKELANGID(LANG_NEUTRAL,
00279                                                         SUBLANG_DEFAULT), (LPTSTR) & lpMsgBuf, 0, NULL);
00280                         fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, (LPCTSTR) lpMsgBuf);
00281                         LocalFree(lpMsgBuf);
00282                 }
00283 
00284                 next = p->next;
00285                 free(p);
00286                 p = next;
00287         }
00288 } /* close_plugins() */

Generated on Fri May 27 11:32:38 2005 for libRASCH by  doxygen 1.4.2