Back to index

lightning-sunbird  0.9+nobinonly
nsGlueLinkingWin.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 6; 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 XPCOM.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Benjamin Smedberg <benjamin@smedbergs.us>
00019  *
00020  * Portions created by the Initial Developer are Copyright (C) 2005
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsGlueLinking.h"
00040 #include "nsXPCOMGlue.h"
00041 
00042 #include <windows.h>
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045 
00046 struct DependentLib
00047 {
00048     HINSTANCE     libHandle;
00049     DependentLib *next;
00050 };
00051 
00052 static DependentLib *sTop;
00053 HINSTANCE sXULLibrary;
00054 
00055 static void
00056 AppendDependentLib(HINSTANCE libHandle)
00057 {
00058     DependentLib *d = new DependentLib;
00059     if (!d)
00060         return;
00061 
00062     d->next = sTop;
00063     d->libHandle = libHandle;
00064 
00065     sTop = d;
00066 }
00067 
00068 static void
00069 ReadDependentCB(const char *aDependentLib)
00070 {
00071     HINSTANCE h =
00072         LoadLibraryEx(aDependentLib, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
00073     if (!h)
00074         return;
00075 
00076     AppendDependentLib(h);
00077 }
00078 
00079 // like strpbrk but finds the *last* char, not the first
00080 static char*
00081 ns_strrpbrk(char *string, const char *strCharSet)
00082 {
00083     char *found = NULL;
00084     for (; *string; ++string) {
00085         for (const char *search = strCharSet; *search; ++search) {
00086             if (*search == *string) {
00087                 found = string;
00088                 // Since we're looking for the last char, we save "found"
00089                 // until we're at the end of the string.
00090             }
00091         }
00092     }
00093 
00094     return found;
00095 }
00096 
00097 GetFrozenFunctionsFunc
00098 XPCOMGlueLoad(const char *xpcomFile)
00099 {
00100     if (xpcomFile[0] == '.' && xpcomFile[1] == '\0') {
00101         xpcomFile = XPCOM_DLL;
00102     }
00103     else {
00104         char xpcomDir[MAXPATHLEN];
00105 
00106         _fullpath(xpcomDir, xpcomFile, sizeof(xpcomDir));
00107         char *lastSlash = ns_strrpbrk(xpcomDir, "/\\");
00108         if (lastSlash) {
00109             *lastSlash = '\0';
00110 
00111             XPCOMGlueLoadDependentLibs(xpcomDir, ReadDependentCB);
00112 
00113             _snprintf(lastSlash, MAXPATHLEN - strlen(xpcomDir), "\\" XUL_DLL);
00114 
00115             sXULLibrary =
00116                 LoadLibraryEx(xpcomDir, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
00117         }
00118     }
00119 
00120     HINSTANCE h =
00121         LoadLibraryEx(xpcomFile, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
00122 
00123     if (!h)
00124         return nsnull;
00125 
00126     AppendDependentLib(h);
00127 
00128     GetFrozenFunctionsFunc sym =
00129         (GetFrozenFunctionsFunc) GetProcAddress(h, "NS_GetFrozenFunctions");
00130 
00131     if (!sym)
00132         XPCOMGlueUnload();
00133 
00134     return sym;
00135 }
00136 
00137 void
00138 XPCOMGlueUnload()
00139 {
00140     while (sTop) {
00141         FreeLibrary(sTop->libHandle);
00142 
00143         DependentLib *temp = sTop;
00144         sTop = sTop->next;
00145 
00146         delete temp;
00147     }
00148 
00149     if (sXULLibrary) {
00150         FreeLibrary(sXULLibrary);
00151         sXULLibrary = nsnull;
00152     }
00153 }
00154 
00155 nsresult
00156 XPCOMGlueLoadXULFunctions(const nsDynamicFunctionLoad *symbols)
00157 {
00158     if (!sXULLibrary)
00159         return NS_ERROR_NOT_INITIALIZED;
00160 
00161     nsresult rv = NS_OK;
00162     while (symbols->functionName) {
00163         *symbols->function = 
00164             (NSFuncPtr) GetProcAddress(sXULLibrary, symbols->functionName);
00165         if (!*symbols->function)
00166             rv = NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
00167 
00168         ++symbols;
00169     }
00170 
00171     return rv;
00172 }