Back to index

lightning-sunbird  0.9+nobinonly
plugload.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "windows.h"
00039 
00040 DWORD GetPluginsDir(char * path, DWORD maxsize)
00041 {
00042   if(!path)
00043     return 0;
00044 
00045   path[0] = '\0';
00046 
00047   DWORD res = GetModuleFileName(NULL, path, maxsize);
00048   if(res == 0)
00049     return 0;
00050 
00051   if(path[strlen(path) - 1] == '\\')
00052     path[lstrlen(path) - 1] = '\0';
00053 
00054   char *p = strrchr(path, '\\');
00055 
00056   if(p)
00057     *p = '\0';
00058 
00059   strcat(path, "\\plugins");
00060 
00061   res = strlen(path);
00062   return res;
00063 }
00064 
00065 HINSTANCE LoadRealPlugin(char * mimetype)
00066 {
00067   if(!mimetype || !strlen(mimetype))
00068     return NULL;
00069 
00070   BOOL bDone = FALSE;
00071   WIN32_FIND_DATA ffdataStruct;
00072 
00073   char szPath[_MAX_PATH];
00074   char szFileName[_MAX_PATH];
00075 
00076   GetPluginsDir(szPath, _MAX_PATH);
00077 
00078   strcpy(szFileName, szPath);
00079   strcat(szFileName, "\\00*");
00080 
00081   HANDLE handle = FindFirstFile(szFileName, &ffdataStruct);
00082   if(handle == INVALID_HANDLE_VALUE) {
00083     FindClose(handle);
00084     return NULL;
00085   }
00086 
00087   DWORD versize = 0L;
00088   DWORD zero = 0L;
00089   char * verbuf = NULL;
00090 
00091   do {
00092     strcpy(szFileName, szPath);
00093     strcat(szFileName, "\\");
00094     strcat(szFileName, ffdataStruct.cFileName);
00095     if(!(ffdataStruct. dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
00096       versize = GetFileVersionInfoSize(szFileName, &zero);
00097            if (versize > 0)
00098                   verbuf = new char[versize];
00099       else 
00100         continue;
00101 
00102       if(!verbuf)
00103                   continue;
00104 
00105       GetFileVersionInfo(szFileName, NULL, versize, verbuf);
00106 
00107       char *mimetypes = NULL;
00108       UINT len = 0;
00109 
00110       if(!VerQueryValue(verbuf, "\\StringFileInfo\\040904E4\\MIMEType", (void **)&mimetypes, &len)
00111          || !mimetypes || !len) {
00112         delete [] verbuf;
00113         continue;
00114       }
00115 
00116       // browse through a string of mimetypes
00117       mimetypes[len] = '\0';
00118       char * type = mimetypes;
00119 
00120       BOOL more = TRUE;
00121       while(more) {
00122         char * p = strchr(type, '|');
00123         if(p)
00124           *p = '\0';
00125         else
00126           more = FALSE;
00127 
00128         if(0 == stricmp(mimetype, type)) {
00129           // this is it!
00130           delete [] verbuf;
00131           FindClose(handle);
00132           HINSTANCE hLib = LoadLibrary(szFileName);
00133           return hLib;
00134         }
00135 
00136         type = p;
00137         type++;
00138       }
00139 
00140       delete [] verbuf;
00141     }
00142 
00143   } while(FindNextFile(handle, &ffdataStruct));
00144 
00145   FindClose(handle);
00146   return NULL;
00147 }
00148 
00149 void UnloadRealPlugin(HINSTANCE hLib)
00150 {
00151   if(!hLib)
00152     FreeLibrary(hLib);
00153 }