private static unsafe bool Initialize(IntPtr xlAddInExportInfoAddress, IntPtr hModuleXll, string pathXll) { XlAddIn.hModuleXll = hModuleXll; XlAddIn.pathXll = pathXll; // NOTE: Too early for logging - the TraceSource in ExcelDna.Integration has not been initialized yet. Debug.Print("In sandbox AppDomain with Id: {0}, running on thread: {1}", AppDomain.CurrentDomain.Id, Thread.CurrentThread.ManagedThreadId); Debug.Assert(xlAddInExportInfoAddress != IntPtr.Zero, "InitializationInfo address is null"); Debug.Print("InitializationInfo address: 0x{0:x8}", xlAddInExportInfoAddress); XlAddInExportInfo *pXlAddInExportInfo = (XlAddInExportInfo *)xlAddInExportInfoAddress; int exportInfoVersion = pXlAddInExportInfo->ExportInfoVersion; if (exportInfoVersion != 8) { Debug.Print("ExportInfoVersion '{0}' not supported", exportInfoVersion); return(false); } fn_short_void fnXlAutoOpen = (fn_short_void)XlAutoOpen; GCHandle.Alloc(fnXlAutoOpen); pXlAddInExportInfo->pXlAutoOpen = Marshal.GetFunctionPointerForDelegate(fnXlAutoOpen); fn_short_void fnXlAutoClose = (fn_short_void)XlAutoClose; GCHandle.Alloc(fnXlAutoClose); pXlAddInExportInfo->pXlAutoClose = Marshal.GetFunctionPointerForDelegate(fnXlAutoClose); fn_short_void fnXlAutoRemove = (fn_short_void)XlAutoRemove; GCHandle.Alloc(fnXlAutoRemove); pXlAddInExportInfo->pXlAutoRemove = Marshal.GetFunctionPointerForDelegate(fnXlAutoRemove); fn_void_intptr fnXlAutoFree12 = (fn_void_intptr)XlAutoFree12; GCHandle.Alloc(fnXlAutoFree12); pXlAddInExportInfo->pXlAutoFree12 = Marshal.GetFunctionPointerForDelegate(fnXlAutoFree12); fn_void_intptr fnSetExcel12EntryPt = (fn_void_intptr)XlCallImpl.SetExcel12EntryPt; GCHandle.Alloc(fnSetExcel12EntryPt); pXlAddInExportInfo->pSetExcel12EntryPt = Marshal.GetFunctionPointerForDelegate(fnSetExcel12EntryPt); fn_hresult_void fnDllRegisterServer = (fn_hresult_void)DllRegisterServer; GCHandle.Alloc(fnDllRegisterServer); pXlAddInExportInfo->pDllRegisterServer = Marshal.GetFunctionPointerForDelegate(fnDllRegisterServer); fn_hresult_void fnDllUnregisterServer = (fn_hresult_void)DllUnregisterServer; GCHandle.Alloc(fnDllUnregisterServer); pXlAddInExportInfo->pDllUnregisterServer = Marshal.GetFunctionPointerForDelegate(fnDllUnregisterServer); fn_get_class_object fnDllGetClassObject = (fn_get_class_object)DllGetClassObject; GCHandle.Alloc(fnDllGetClassObject); pXlAddInExportInfo->pDllGetClassObject = Marshal.GetFunctionPointerForDelegate(fnDllGetClassObject); fn_hresult_void fnDllCanUnloadNow = (fn_hresult_void)DllCanUnloadNow; GCHandle.Alloc(fnDllCanUnloadNow); pXlAddInExportInfo->pDllCanUnloadNow = Marshal.GetFunctionPointerForDelegate(fnDllCanUnloadNow); fn_void_double fnSyncMacro = (fn_void_double)SyncMacro; GCHandle.Alloc(fnSyncMacro); pXlAddInExportInfo->pSyncMacro = Marshal.GetFunctionPointerForDelegate(fnSyncMacro); fn_intptr_intptr fnRegistrationInfo = (fn_intptr_intptr)RegistrationInfo; GCHandle.Alloc(fnRegistrationInfo); pXlAddInExportInfo->pRegistrationInfo = Marshal.GetFunctionPointerForDelegate(fnRegistrationInfo); fn_void_void fnCalculationCanceled = (fn_void_void)CalculationCanceled; GCHandle.Alloc(fnCalculationCanceled); pXlAddInExportInfo->pCalculationCanceled = Marshal.GetFunctionPointerForDelegate(fnCalculationCanceled); fn_void_void fnCalculationEnded = (fn_void_void)CalculationEnded; GCHandle.Alloc(fnCalculationEnded); pXlAddInExportInfo->pCalculationEnded = Marshal.GetFunctionPointerForDelegate(fnCalculationEnded); // Thunk table for registered functions thunkTableLength = pXlAddInExportInfo->ThunkTableLength; thunkTable = pXlAddInExportInfo->ThunkTable; // This is the place where we call into Excel - this causes SecurityPermission exception // when run from VSTO. I don't know how to deal with this problem yet. // TODO: Learn more about the special security stuff in VSTO. // See ExecutionContext.SuppressFlow links below. try { XlAddIn.xlCallVersion = XlCallImpl.XLCallVer() / 256; } catch (DllNotFoundException) { // This is expected if we are running under HPC or Regsvr32. Debug.Print("XlCall library not found - probably running in HPC host or Regsvr32.exe"); // For the HPC support, I ignore error here and just assume we are under new Excel. // This will cause the common error here to get pushed to later ... XlAddIn.xlCallVersion = 12; // return false; } catch (Exception e) { Debug.Print("XlAddIn: XLCallVer Error: {0}", e); // CONSIDER: Is this right / needed - I'm not actually sure what happens under HPC host, // so I'll leave this case in here too.? XlAddIn.xlCallVersion = 12; // return false; } try { IntegrationLoader.LoadIntegration(); } catch (InvalidOperationException ioe) { Debug.Print("XlAddIn: Initialize Error - Invalid ExcelIntegration version detected: {0}", ioe); return(false); } catch (Exception e) { Debug.Print("XlAddIn: Initialize Error:", e); return(false); } // File.AppendAllText(Path.ChangeExtension(pathXll, ".log"), string.Format("{0:u} XlAddIn.Initialize OK\r\n", DateTime.Now)); return(true); }
public static unsafe bool Initialize(int xlAddInExportInfoAddress, int hModuleXll, string pathXll) { Debug.Assert(xlAddInExportInfoAddress != 0); Debug.Print("InitializationInfo Address: 0x{0:x8}", xlAddInExportInfoAddress); XlAddInExportInfo *pXlAddInExportInfo = (XlAddInExportInfo *)xlAddInExportInfoAddress; if (pXlAddInExportInfo->ExportInfoVersion != 1) { Debug.Print("ExportInfoVersion not supported."); return(false); } fn_short_void fnXlAutoOpen = (fn_short_void)XlAutoOpen; GCHandle.Alloc(fnXlAutoOpen); pXlAddInExportInfo->pXlAutoOpen = Marshal.GetFunctionPointerForDelegate(fnXlAutoOpen); fn_short_void fnXlAutoClose = (fn_short_void)XlAutoClose; GCHandle.Alloc(fnXlAutoClose); pXlAddInExportInfo->pXlAutoClose = Marshal.GetFunctionPointerForDelegate(fnXlAutoClose); fn_short_void fnXlAutoAdd = (fn_short_void)XlAutoAdd; GCHandle.Alloc(fnXlAutoAdd); pXlAddInExportInfo->pXlAutoAdd = Marshal.GetFunctionPointerForDelegate(fnXlAutoAdd); fn_short_void fnXlAutoRemove = (fn_short_void)XlAutoRemove; GCHandle.Alloc(fnXlAutoRemove); pXlAddInExportInfo->pXlAutoRemove = Marshal.GetFunctionPointerForDelegate(fnXlAutoRemove); fn_void_intptr fnXlAutoFree = (fn_void_intptr)XlAutoFree; GCHandle.Alloc(fnXlAutoFree); pXlAddInExportInfo->pXlAutoFree = Marshal.GetFunctionPointerForDelegate(fnXlAutoFree); fn_void_intptr fnXlAutoFree12 = (fn_void_intptr)XlAutoFree12; GCHandle.Alloc(fnXlAutoFree12); pXlAddInExportInfo->pXlAutoFree12 = Marshal.GetFunctionPointerForDelegate(fnXlAutoFree12); fn_intptr_intptr fnXlAddInManagerInfo = (fn_intptr_intptr)XlAddInManagerInfo; GCHandle.Alloc(fnXlAddInManagerInfo); pXlAddInExportInfo->pXlAddInManagerInfo = Marshal.GetFunctionPointerForDelegate(fnXlAddInManagerInfo); fn_intptr_intptr fnXlAddInManagerInfo12 = (fn_intptr_intptr)XlAddInManagerInfo12; GCHandle.Alloc(fnXlAddInManagerInfo12); pXlAddInExportInfo->pXlAddInManagerInfo12 = Marshal.GetFunctionPointerForDelegate(fnXlAddInManagerInfo12); thunkTableLength = pXlAddInExportInfo->ThunkTableLength; thunkTable = pXlAddInExportInfo->ThunkTable; XlAddIn.xlCallVersion = XlCallImpl.XLCallVer() / 256; XlAddIn.hModuleXll = (IntPtr)hModuleXll; XlAddIn.pathXll = pathXll; AssemblyManager.Initialize((IntPtr)hModuleXll, pathXll); bool result = false; try { LoadIntegration(); result = true; } catch (Exception e) { Debug.WriteLine("XlAddIn: Initialize Exception: " + e); } return(result); }