Back to index

lightning-sunbird  0.9+nobinonly
Typedefs | Functions | Variables
control_kicker.cpp File Reference
#include "stdafx.h"
#include <sys/types.h>
#include <sys/stat.h>
#include "shlobj.h"

Go to the source code of this file.

Typedefs

typedef HRESULT(STDAPICALLTYPE * DllCanUnloadNowFn )(void)
typedef HRESULT(STDAPICALLTYPE * DllGetClassObjectFn )(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
typedef HRESULT(STDAPICALLTYPE * DllRegisterServerFn )(void)
typedef HRESULT(STDAPICALLTYPE * DllUnregisterServerFn )(void)

Functions

static BOOL LoadMozLibraryFromCurrentDir ()
static BOOL LoadMozLibraryFromRegistry (BOOL bAskUserToSetPath)
static BOOL LoadLibraryWithPATHFixup (const TCHAR *szBinDirPath)
BOOL APIENTRY DllMain (HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
static BOOL LoadMozLibrary (BOOL bAskUserToSetPath=TRUE)
static BOOL FixupInProcRegistryEntry ()
STDAPI DllCanUnloadNow (void)
STDAPI DllGetClassObject (REFCLSID rclsid, REFIID riid, LPVOID *ppv)
STDAPI DllRegisterServer (void)
STDAPI DllUnregisterServer (void)

Variables

static HMODULE ghMozCtlX = NULL
static HMODULE ghMozCtl = NULL
static LPCTSTR c_szMozCtlDll = _T("mozctl.dll")
static LPCTSTR c_szMozillaControlKey = _T("Software\\Mozilla\\")
static LPCTSTR c_szMozillaBinDirPathValue = _T("BinDirectoryPath")
static LPCTSTR c_szMozCtlInProcServerKey
static LPCTSTR c_szWarningTitle
static LPCTSTR c_szErrorTitle
static LPCTSTR c_szPrePickFolderMsg
static LPCTSTR c_szPickFolderDlgTitle
static LPCTSTR c_szRegistryErrorMsg
static LPCTSTR c_szLoadErrorMsg

Typedef Documentation

typedef HRESULT(STDAPICALLTYPE * DllCanUnloadNowFn)(void)

Definition at line 339 of file control_kicker.cpp.

typedef HRESULT(STDAPICALLTYPE * DllGetClassObjectFn)(REFCLSID rclsid, REFIID riid, LPVOID *ppv)

Definition at line 358 of file control_kicker.cpp.

typedef HRESULT(STDAPICALLTYPE * DllRegisterServerFn)(void)

Definition at line 377 of file control_kicker.cpp.

typedef HRESULT(STDAPICALLTYPE * DllUnregisterServerFn)(void)

Definition at line 401 of file control_kicker.cpp.


Function Documentation

STDAPI DllCanUnloadNow ( void  )

Definition at line 341 of file control_kicker.cpp.

{
    if (LoadMozLibrary())
    {
        DllCanUnloadNowFn pfn = (DllCanUnloadNowFn)
            GetProcAddress(ghMozCtl, _T("DllCanUnloadNow"));
        if (pfn)
        {
            return pfn();
        }
    }
    return S_OK;
}

Here is the call graph for this function:

STDAPI DllGetClassObject ( REFCLSID  rclsid,
REFIID  riid,
LPVOID ppv 
)

Definition at line 360 of file control_kicker.cpp.

{
    if (LoadMozLibrary())
    {
        DllGetClassObjectFn pfn = (DllGetClassObjectFn) 
            GetProcAddress(ghMozCtl, _T("DllGetClassObject"));
        if (pfn)
        {
            return pfn(rclsid, riid, ppv);
        }
    }
    return E_FAIL;
}

Here is the call graph for this function:

BOOL APIENTRY DllMain ( HANDLE  hModule,
DWORD  ul_reason_for_call,
LPVOID  lpReserved 
)

Definition at line 88 of file control_kicker.cpp.

{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        ghMozCtlX = (HMODULE) hModule;
        break;
    case DLL_THREAD_ATTACH:
        break;
    case DLL_THREAD_DETACH:
        break;
    case DLL_PROCESS_DETACH:
        if (ghMozCtl)
        {
            FreeLibrary(ghMozCtl);
            ghMozCtl = NULL;
        }
        break;
    }
    return TRUE;
}
STDAPI DllRegisterServer ( void  )

Definition at line 379 of file control_kicker.cpp.

{
    if (LoadMozLibrary())
    {
        DllRegisterServerFn pfn = (DllRegisterServerFn) 
            GetProcAddress(ghMozCtl, _T("DllRegisterServer"));
        if (pfn)
        {
            HRESULT hr = pfn();
            if (SUCCEEDED(hr))
            {
                FixupInProcRegistryEntry();
            }
            return hr;
        }
    }
    return E_FAIL;
}

Here is the call graph for this function:

STDAPI DllUnregisterServer ( void  )

Definition at line 403 of file control_kicker.cpp.

{
    if (LoadMozLibrary())
    {
        DllUnregisterServerFn pfn = (DllUnregisterServerFn)
            GetProcAddress(ghMozCtl, _T("DllUnregisterServer"));
        if (pfn)
        {
            return pfn();
        }
    }
    return E_FAIL;
}

Here is the call graph for this function:

static BOOL FixupInProcRegistryEntry ( ) [static]

Definition at line 311 of file control_kicker.cpp.

{
    TCHAR szMozCtlXLongPath[MAX_PATH+1];
    ZeroMemory(szMozCtlXLongPath, sizeof(szMozCtlXLongPath));
    GetModuleFileName(ghMozCtlX, szMozCtlXLongPath,
        sizeof(szMozCtlXLongPath) * sizeof(TCHAR));

    TCHAR szMozCtlXPath[MAX_PATH+1];
    ZeroMemory(szMozCtlXPath, sizeof(szMozCtlXPath));
    GetShortPathName(szMozCtlXLongPath, szMozCtlXPath,
        sizeof(szMozCtlXPath) * sizeof(TCHAR));

    HKEY hkey = NULL;
    RegOpenKeyEx(HKEY_CLASSES_ROOT, c_szMozCtlInProcServerKey, 0, KEY_ALL_ACCESS, &hkey);
    if (hkey)
    {
        RegSetValueEx(hkey, NULL, 0, REG_SZ, (LPBYTE) szMozCtlXPath,
            lstrlen(szMozCtlXPath) * sizeof(TCHAR));
        RegCloseKey(hkey);
        return TRUE;
    }

    return FALSE;
}

Here is the caller graph for this function:

static BOOL LoadLibraryWithPATHFixup ( const TCHAR szBinDirPath) [static]

Definition at line 282 of file control_kicker.cpp.

{
    TCHAR szOldPath[8192];
    TCHAR szNewPath[8192];
    
    ZeroMemory(szOldPath, sizeof(szOldPath));
    ZeroMemory(szNewPath, sizeof(szNewPath));

    // Append path to front of PATH
    GetEnvironmentVariable("PATH", szOldPath, sizeof(szOldPath) / sizeof(TCHAR));
    lstrcat(szNewPath, szBinDirPath);
    lstrcat(szNewPath, _T(";"));
    lstrcat(szNewPath, szOldPath);
    SetEnvironmentVariable("PATH", szNewPath);
    
    // Attempt load
    ghMozCtl = LoadLibrary(c_szMozCtlDll);
    if (ghMozCtl)
    {
        return TRUE;
    }

    // Restore old PATH
    SetEnvironmentVariable("PATH", szOldPath);

    return FALSE;
}

Here is the caller graph for this function:

static BOOL LoadMozLibrary ( BOOL  bAskUserToSetPath = TRUE) [static]

Definition at line 113 of file control_kicker.cpp.

{
    if (ghMozCtl)
    {
        return TRUE;
    }

    ghMozCtl = LoadLibrary(c_szMozCtlDll);
    if (ghMozCtl)
    {
        return TRUE;
    }

    if (LoadMozLibraryFromCurrentDir())
    {
        return TRUE;
    }

    if (LoadMozLibraryFromRegistry(bAskUserToSetPath))
    {
        return TRUE;
    }

    ::MessageBox(NULL, c_szLoadErrorMsg, c_szErrorTitle, MB_OK);

    return FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static BOOL LoadMozLibraryFromCurrentDir ( ) [static]

Definition at line 141 of file control_kicker.cpp.

{
    TCHAR szMozCtlXPath[MAX_PATH + 1];

    // Get this module's path
    ZeroMemory(szMozCtlXPath, sizeof(szMozCtlXPath));
    GetModuleFileName(ghMozCtlX, szMozCtlXPath,
        sizeof(szMozCtlXPath) / sizeof(szMozCtlXPath[0]));

    // Make the control path
    TCHAR szTmpDrive[_MAX_DRIVE];
    TCHAR szTmpDir[_MAX_DIR];
    TCHAR szTmpFname[_MAX_FNAME];
    TCHAR szTmpExt[_MAX_EXT];
    _tsplitpath(szMozCtlXPath, szTmpDrive, szTmpDir, szTmpFname, szTmpExt);

    TCHAR szMozCtlPath[MAX_PATH + 1];
    ZeroMemory(szMozCtlPath, sizeof(szMozCtlPath));
    _tmakepath(szMozCtlPath, szTmpDrive, szTmpDir, c_szMozCtlDll, NULL);

    // Stat to see if the control exists
    struct _stat dirStat;
    if (_tstat(szMozCtlPath, &dirStat) == 0)
    {
        TCHAR szBinDirPath[MAX_PATH + 1];
        ZeroMemory(szBinDirPath, sizeof(szBinDirPath));
        _tmakepath(szBinDirPath, szTmpDrive, szTmpDir, NULL, NULL);
        if (LoadLibraryWithPATHFixup(szBinDirPath))
        {
            return TRUE;
        }
    }

    return FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static BOOL LoadMozLibraryFromRegistry ( BOOL  bAskUserToSetPath) [static]

Definition at line 177 of file control_kicker.cpp.

{
    // 
    HKEY hkey = NULL;
    
    TCHAR szBinDirPath[MAX_PATH + 1];
    DWORD dwBufSize = sizeof(szBinDirPath);

    ZeroMemory(szBinDirPath, dwBufSize);

    // First try and read the path from the registry
    RegOpenKeyEx(HKEY_LOCAL_MACHINE, c_szMozillaControlKey, 0, KEY_READ, &hkey);
    if (hkey)
    {
        DWORD dwType = REG_SZ;
        RegQueryValueEx(hkey, c_szMozillaBinDirPathValue, NULL, &dwType,
            (LPBYTE) szBinDirPath, &dwBufSize);
        RegCloseKey(hkey);
        hkey = NULL;

        if (lstrlen(szBinDirPath) > 0 && LoadLibraryWithPATHFixup(szBinDirPath))
        {
            return TRUE;
        }

        // NOTE: We will drop through here if the registry key existed
        //       but didn't lead to the control being loaded.
    }
    
    // Ask the user to pick to pick the path?
    if (bAskUserToSetPath)
    {
        // Tell the user that they have to pick the bin dir path
        ::MessageBox(NULL, c_szPrePickFolderMsg, c_szWarningTitle, MB_OK);

        // Show a folder picker for the user to choose the bin directory
        BROWSEINFO bi;
        ZeroMemory(&bi, sizeof(bi));
        bi.hwndOwner = NULL;
        bi.pidlRoot = NULL;
        bi.pszDisplayName = szBinDirPath;
        bi.lpszTitle = c_szPickFolderDlgTitle;
        LPITEMIDLIST pItemList = SHBrowseForFolder(&bi);
        if (pItemList)
        {
            // Get the path from the user selection
            IMalloc *pShellAllocator = NULL;
            SHGetMalloc(&pShellAllocator);
            if (pShellAllocator)
            {
                char szPath[MAX_PATH + 1];

                if (SHGetPathFromIDList(pItemList, szPath))
                {
                    // Chop off the end path separator
                    int nPathSize = strlen(szPath);
                    if (nPathSize > 0)
                    {
                        if (szPath[nPathSize - 1] == '\\')
                        {
                            szPath[nPathSize - 1] = '\0';
                        }
                    }

                    // Form the file pattern
#ifdef UNICODE
                    MultiByteToWideChar(CP_ACP, 0, szPath, szBinDirPath, -1,
                        sizeof(szBinDirPath) / sizeof(TCHAR));
#else
                    lstrcpy(szBinDirPath, szPath);
#endif
                }
                pShellAllocator->Free(pItemList);
                pShellAllocator->Release();
            }
        }
        
        // Store the new path if we can
        if (lstrlen(szBinDirPath) > 0)
        {
            RegCreateKeyEx(HKEY_LOCAL_MACHINE, c_szMozillaControlKey, 0, NULL,
                REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL);
            if (hkey)
            {
                RegSetValueEx(hkey, c_szMozillaBinDirPathValue, 0, REG_SZ,
                    (LPBYTE) szBinDirPath, lstrlen(szBinDirPath) * sizeof(TCHAR));
                RegCloseKey(hkey);
            }
            else
            {
                // Tell the user the key can't be stored but the path they 
                // chose will be used for this session
                ::MessageBox(NULL, c_szRegistryErrorMsg, c_szErrorTitle, MB_OK);
            }
        }
    }

    if (lstrlen(szBinDirPath) > 0 && LoadLibraryWithPATHFixup(szBinDirPath))
    {
        return TRUE;
    }

    return FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Initial value:
    _T("Mozilla Control Error")

Definition at line 64 of file control_kicker.cpp.

Initial value:
    _T("The Mozilla control could not be loaded correctly. This is could "
       "be due to an invalid location being specified for the Mozilla "
       "layout libraries or missing files from your installation.\n\n"
       "Visit http://www.iol.ie/~locka/mozilla/mozilla.htm for in-depth"
       "troubleshooting advice.")

Definition at line 81 of file control_kicker.cpp.

LPCTSTR c_szMozCtlDll = _T("mozctl.dll") [static]

Definition at line 55 of file control_kicker.cpp.

Initial value:
    _T("CLSID\\{1339B54C-3453-11D2-93B9-000000000000}\\InProcServer32")

Definition at line 58 of file control_kicker.cpp.

LPCTSTR c_szMozillaBinDirPathValue = _T("BinDirectoryPath") [static]

Definition at line 57 of file control_kicker.cpp.

LPCTSTR c_szMozillaControlKey = _T("Software\\Mozilla\\") [static]

Definition at line 56 of file control_kicker.cpp.

Initial value:
    _T("Pick where the Mozilla bin directory is located, "
       "e.g. (c:\\mozilla\\bin)")

Definition at line 70 of file control_kicker.cpp.

Initial value:
    _T("The Mozilla control was unable to detect where your Mozilla "
       "layout libraries may be found. You will now be shown a directory "
       "picker for you to locate them.")

Definition at line 66 of file control_kicker.cpp.

Initial value:
    _T("The Mozilla control was unable to store the location of the Mozilla"
       "layout libraries in the registry. This may be due to the host "
       "program running with insufficient permissions to write there.\n\n"
       "You should consider running the control once as Administrator "
       "to enable this entry to added or alternatively move the "
       "control files mozctlx.dll and mozctl.dll into the same folder as "
       "the other libraries.")

Definition at line 73 of file control_kicker.cpp.

Initial value:
    _T("Mozilla Control Warning")

Definition at line 62 of file control_kicker.cpp.

HMODULE ghMozCtl = NULL [static]

Definition at line 53 of file control_kicker.cpp.

HMODULE ghMozCtlX = NULL [static]

Definition at line 52 of file control_kicker.cpp.