nsresult XPCOMGlueLoad(const char *xpcomFile, GetFrozenFunctionsFunc *func) { CHAR pszError[_MAX_PATH]; ULONG ulrc = NO_ERROR; HMODULE h; if (xpcomFile[0] == '.' && xpcomFile[1] == '\0') { xpcomFile = XPCOM_DLL; } else { char xpcomDir[MAXPATHLEN]; _fullpath(xpcomDir, xpcomFile, sizeof(xpcomDir)); char *lastSlash = ns_strrpbrk(xpcomDir, "/\\"); if (lastSlash) { *lastSlash = '\0'; XPCOMGlueLoadDependentLibs(xpcomDir, ReadDependentCB); snprintf(lastSlash, MAXPATHLEN - strlen(xpcomDir), "\\" XUL_DLL); DosLoadModule(pszError, _MAX_PATH, xpcomDir, &sXULLibrary); } } ulrc = DosLoadModule(pszError, _MAX_PATH, xpcomFile, &h); if (ulrc != NO_ERROR) return nsnull; AppendDependentLib(h); GetFrozenFunctionsFunc sym; ulrc = DosQueryProcAddr(h, 0, "_NS_GetFrozenFunctions", (PFN*)&sym); if (ulrc != NO_ERROR) { XPCOMGlueUnload(); return NS_ERROR_NOT_AVAILABLE; } *func = sym; return NS_OK; }
int StartXPCOM(){ nsresult rv = NS_ERROR_BASE; const GREVersionRange vr = { "1.9.2", PR_TRUE, "2.0", PR_FALSE }; char xpcomPath[_MAX_PATH]; rv = GRE_GetGREPathWithProperties(&vr, 1, nsnull, 0, xpcomPath, sizeof(xpcomPath)); if (NS_FAILED(rv)){ char *fxXpcomPath = "C:\\Program Files\\Mozilla Firefox\\xpcom.dll"; if(::PathFileExistsA( fxXpcomPath )){ strcpy(xpcomPath, fxXpcomPath); rv = NS_OK; }else{ rv = NS_ERROR_BASE; } } if (NS_FAILED(rv)) return 1; char *lastslash = ns_strrpbrk(xpcomPath, "/\\"); if (!lastslash) return 2; rv = XPCOMGlueStartup(xpcomPath); if (NS_FAILED(rv)) return 3; *lastslash = '\0'; char xulPath[_MAX_PATH]; _snprintf(xulPath, sizeof(xulPath), "%s\\xul.dll", xpcomPath); xulPath[sizeof(xulPath) - 1] = '\0'; HINSTANCE xulModule = LoadLibraryEx(CString(xulPath), NULL, 0); if (!xulModule) return 4; TCHAR temp[_MAX_PATH]; GetModuleFileName(xulModule, temp, sizeof(temp)); XRE_InitEmbedding = (XRE_InitEmbeddingType) GetProcAddress(xulModule, "XRE_InitEmbedding"); if (!XRE_InitEmbedding) { //fprintf(stderr, "Error: %i\n", GetLastError()); return 5; } XRE_TermEmbedding = (XRE_TermEmbeddingType) GetProcAddress(xulModule, "XRE_TermEmbedding"); if (!XRE_TermEmbedding) { fprintf(stderr, "Error: %i\n", GetLastError()); return 5; } XRE_NotifyProfile = (XRE_NotifyProfileType) GetProcAddress(xulModule, "XRE_NotifyProfile"); if (!XRE_NotifyProfile) { fprintf(stderr, "Error: %i\n", GetLastError()); return 5; } // Scope all the XPCOM stuff { nsCOMPtr<nsILocalFile> xuldir; rv = NS_NewNativeLocalFile(nsCString(xpcomPath), PR_FALSE, getter_AddRefs(xuldir)); if (NS_FAILED(rv)) return 6; TCHAR self[_MAX_PATH]; HINSTANCE hInstanceApp = NULL; hInstanceApp = GetModuleHandle(NULL); GetModuleFileName(hInstanceApp, self, sizeof(self)); lastslash = ns_strrpbrk(xpcomPath, "/\\"); if (!lastslash) return 7; *lastslash = '\0'; nsCOMPtr<nsILocalFile> appdir; nsEmbedCString cself; NS_UTF16ToCString(nsEmbedString(self), NS_CSTRING_ENCODING_UTF8, cself); rv = NS_NewNativeLocalFile(cself, PR_FALSE, getter_AddRefs(appdir)); if (NS_FAILED(rv)) return 8; kDirSvcProvider = new DonutDirectoryServiceProvider(); kDirSvcProvider->AddRef(); rv = XRE_InitEmbedding(xuldir, appdir, kDirSvcProvider, nsnull, 0); if (NS_FAILED(rv)) return 9; XRE_NotifyProfile(); RegisterAdditionalComponents(); } return rv; }
int main(int argc, char *argv[]) { nsresult rv; printf("You are embedded, man!\n\n"); printf("******************************************************************\n"); printf("* *\n"); printf("* IMPORTANT NOTE: *\n"); printf("* *\n"); printf("* WinEmbed is not supported!!! Do not raise bugs on it unless *\n"); printf("* it is badly broken (e.g. crash on start/exit, build errors) *\n"); printf("* or you have the patch to make it better! MFCEmbed is now our *\n"); printf("* embedding test application on Win32 and all testing should *\n"); printf("* be done on that. *\n"); printf("* *\n"); printf("******************************************************************\n"); printf("\n\n"); // Sophisticated command-line parsing in action char *szFirstURL = "http://www.mozilla.org/projects/embedding/"; int argn; for (argn = 1; argn < argc; argn++) { szFirstURL = argv[argn]; } strncpy(gFirstURL, szFirstURL, sizeof(gFirstURL) - 1); ghInstanceApp = GetModuleHandle(nullptr); // Initialize global strings TCHAR szTitle[MAX_LOADSTRING]; LoadString(ghInstanceApp, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); MyRegisterClass(ghInstanceApp); char path[_MAX_PATH]; GetModuleFileName(ghInstanceApp, path, sizeof(path)); char* lastslash = ns_strrpbrk(path, "/\\"); if (!lastslash) return 7; strcpy(lastslash, "\\xulrunner\\xpcom.dll"); rv = XPCOMGlueStartup(path); if (NS_FAILED(rv)) return 3; strcpy(lastslash, "\\xulrunner\\xul.dll"); HINSTANCE xulModule = LoadLibraryEx(path, nullptr, 0); if (!xulModule) return 4; XRE_InitEmbedding2 = (XRE_InitEmbedding2Type) GetProcAddress(xulModule, "XRE_InitEmbedding2"); if (!XRE_InitEmbedding2) { fprintf(stderr, "Error: %i\n", GetLastError()); return 5; } XRE_TermEmbedding = (XRE_TermEmbeddingType) GetProcAddress(xulModule, "XRE_TermEmbedding"); if (!XRE_TermEmbedding) { fprintf(stderr, "Error: %i\n", GetLastError()); return 5; } int result = 0; // Scope all the XPCOM stuff { strcpy(lastslash, "\\xulrunner"); nsCOMPtr<nsIFile> xuldir; rv = NS_NewNativeLocalFile(nsCString(path), false, getter_AddRefs(xuldir)); if (NS_FAILED(rv)) return 6; *lastslash = '\0'; nsCOMPtr<nsIFile> appdir; rv = NS_NewNativeLocalFile(nsCString(path), false, getter_AddRefs(appdir)); if (NS_FAILED(rv)) return 8; rv = XRE_InitEmbedding2(xuldir, appdir, nullptr); if (NS_FAILED(rv)) return 9; if (NS_FAILED(StartupProfile())) { result = 8; } else { InitializeWindowCreator(); // Open the initial browser window OpenWebPage(gFirstURL); // Main message loop. // NOTE: We use a fake event and a timeout in order to process idle stuff for // Mozilla every 1/10th of a second. bool runCondition = true; result = AppCallbacks::RunEventLoop(runCondition); } } XRE_TermEmbedding(); return result; }