MainPy.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <FCConfig.h>
00025
00026 #ifdef _PreComp_
00027 # undef _PreComp_
00028 #endif
00029
00030 #ifdef FC_OS_LINUX
00031 # include <unistd.h>
00032 #endif
00033
00034 #ifdef FC_OS_MACOSX
00035 # include <mach-o/dyld.h>
00036 # include <string>
00037 #endif
00038
00039 #if HAVE_CONFIG_H
00040 # include <config.h>
00041 #endif // HAVE_CONFIG_H
00042
00043 #include <stdio.h>
00044 #include <sstream>
00045
00046
00047
00048 #include <Base/Exception.h>
00049 #include <App/Application.h>
00050
00051
00052 #if defined(FC_OS_WIN32)
00053 # include <windows.h>
00054
00057 BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
00058 {
00059 switch (ul_reason_for_call) {
00060 case DLL_PROCESS_ATTACH: {
00061
00062
00063 char szFileName [MAX_PATH];
00064 GetModuleFileName((HMODULE)hModule, szFileName, MAX_PATH-1);
00065 App::Application::Config()["AppHomePath"] = szFileName;
00066 }
00067 break;
00068 default:
00069 break;
00070 }
00071
00072 return TRUE;
00073 }
00074 #elif defined(FC_OS_LINUX)
00075 # ifndef GNU_SOURCE
00076 # define GNU_SOURCE
00077 # endif
00078 # include <dlfcn.h>
00079 #elif defined(FC_OS_CYGWIN)
00080 # include <windows.h>
00081 #endif
00082
00083 #ifdef FC_OS_WIN32
00084 # define MainExport __declspec(dllexport)
00085 #else
00086 # define MainExport
00087 #endif
00088
00089 extern "C"
00090 {
00091 void MainExport initFreeCAD() {
00092
00093
00094 App::Application::Config()["ExeName"] = "FreeCAD";
00095 App::Application::Config()["ExeVendor"] = "FreeCAD";
00096 App::Application::Config()["AppDataSkipVendor"] = "true";
00097
00098
00099 int argc=1;
00100 char** argv;
00101 argv = (char**)malloc(sizeof(char*)* (argc+1));
00102
00103 #if defined(FC_OS_WIN32)
00104 argv[0] = (char*)malloc(MAX_PATH);
00105 strncpy(argv[0],App::Application::Config()["AppHomePath"].c_str(),MAX_PATH);
00106 argv[0][MAX_PATH-1] = '\0';
00107 #elif defined(FC_OS_CYGWIN)
00108 HMODULE hModule = GetModuleHandle("FreeCAD.dll");
00109 char szFileName [MAX_PATH];
00110 GetModuleFileName(hModule, szFileName, MAX_PATH-1);
00111 argv[0] = (char*)malloc(MAX_PATH);
00112 strncpy(argv[0],szFileName,MAX_PATH);
00113 argv[0][MAX_PATH-1] = '\0';
00114 #elif defined(FC_OS_LINUX)
00115
00116 Dl_info info;
00117 int ret = dladdr((void*)initFreeCAD, &info);
00118 if ((ret == 0) || (!info.dli_fname)) {
00119 PyErr_SetString(PyExc_ImportError, "Cannot get path of the FreeCAD module!");
00120 return;
00121 }
00122
00123 argv[0] = (char*)malloc(PATH_MAX);
00124 strncpy(argv[0], info.dli_fname,PATH_MAX);
00125 argv[0][PATH_MAX-1] = '\0';
00126 #elif defined(FC_OS_MACOSX)
00127 uint32_t sz = 0;
00128 char *buf;
00129
00130 _NSGetExecutablePath(NULL, &sz);
00131 buf = (char*) malloc(++sz);
00132 int err=_NSGetExecutablePath(buf, &sz);
00133 if (err != 0) {
00134 PyErr_SetString(PyExc_ImportError, "Cannot get path of the FreeCAD module!");
00135 return;
00136 }
00137
00138 argv[0] = (char*)malloc(PATH_MAX);
00139 strncpy(argv[0], buf, PATH_MAX);
00140 argv[0][PATH_MAX-1] = '\0';
00141 free(buf);
00142 #else
00143 # error "Implement: Retrieve the path of the module for your platform."
00144 #endif
00145 argv[argc] = 0;
00146
00147 try {
00148
00149 App::Application::init(argc,argv);
00150 }
00151 catch (const Base::Exception& e) {
00152 std::string appName = App::Application::Config()["ExeName"];
00153 std::stringstream msg;
00154 msg << "While initializing " << appName << " the following exception occurred: '"
00155 << e.what() << "'\n\n";
00156 msg << "\nPlease contact the application's support team for more information.\n\n";
00157 printf("Initialization of %s failed:\n%s", appName.c_str(), msg.str().c_str());
00158 }
00159
00160 free(argv[0]);
00161 free(argv);
00162
00163 return;
00164 }
00165 }
00166