Example #1
0
        public void Dispose()
        {
            if (_isDocumentDirty)
            {
                ComDebug.ReportInfo("{0}.Dispose Step 0 : Document is dirty -> Advise DataChanged", GetType().Name);
                SendAdvise_DataChanged(); // update the image of the graph before we close
            }

            ComDebug.ReportInfo("{0}.Dispose Step 1 : SaveObject", GetType().Name);

            SendAdvise_SaveObject();

            ComDebug.ReportInfo("{0}.Dispose Step 2 : Calling SendAdvise.HideWindow", GetType().Name);

            SendAdvise_HideWindow();

            ComDebug.ReportInfo("{0}.Dispose Step 3 : Calling SendAdvise.Closed", GetType().Name);

            SendAdvise_Closed();

            ComDebug.ReportInfo("{0}.Dispose Step 4 : ROTUnregister(ref _documentMonikerRotCookie)", GetType().Name);

            if (0 != _documentMonikerRotCookie)
            {
                RunningObjectTableHelper.ROTUnregister(ref _documentMonikerRotCookie);
                _documentMoniker = null;
            }

            // Disconnect the container.
            ComDebug.ReportInfo("{0}.Dispose Step 5 : Disconnecting this object", GetType().Name);

            Ole32Func.CoDisconnectObject(this, 0);

            ComDebug.ReportInfo("{0}.Dispose completed.", GetType().Name);
        }
Example #2
0
        private void EhFileMonikerChanged(IMoniker fileMoniker)
        {
            if (null == _document)
            {
                return;
            }

            // see Brockschmidt, Inside Ole 2nd ed., p.998
            // TODO we must pimp up this function

            RunningObjectTableHelper.ROTUnregister(ref _documentMonikerRotCookie);
            _documentMoniker = null;

            if (null != fileMoniker)
            {
                Ole32Func.CreateItemMoniker("!", DataObjectHelper.NormalStringToMonikerNameString(_document.Name), out var itemMoniker);

                if (null != itemMoniker)
                {
                    fileMoniker.ComposeWith(itemMoniker, false, out var compositeMoniker);
                    if (null != compositeMoniker)
                    {
                        _documentMoniker = compositeMoniker;
                        RunningObjectTableHelper.ROTRegisterAsRunning(_documentMoniker, this, ref _documentMonikerRotCookie, typeof(IOleObject));
                    }
                }
            }

            SendAdvise_Renamed();
        }
Example #3
0
        public void Dispose()
        {
            if (_isDocumentDirty)
            {
                ComDebug.ReportInfo("{0}.Dispose Step 0 : Document is dirty -> Advise DataChanged", GetType().Name);
                SendAdvise_DataChanged(); // update the image of the graph before we close
            }

            ComDebug.ReportInfo("{0}.Dispose Step 1 : SaveObject", GetType().Name);

            SendAdvise_SaveObject(); // make an advise to save the mini-project into the container application

            ComDebug.ReportInfo("{0}.Dispose Step 2 : Calling SendAdvise.HideWindow", GetType().Name);

            SendAdvise_HideWindow();

            ComDebug.ReportInfo("{0}.Dispose Step 3 : Calling SendAdvise.Closed", GetType().Name);

            SendAdvise_Closed();

            // if we had a document moniker, we should unregister it here
            // but since this is an embedded object,we have no document moniker

            // Disconnect the container.
            ComDebug.ReportInfo("{0}.Dispose Step 4 : Disconnecting this object", GetType().Name);

            Ole32Func.CoDisconnectObject(this, 0);

            ComDebug.ReportInfo("{0}.Dispose completed.", GetType().Name);
        }
Example #4
0
        public static void SaveMonikerToStream(IMoniker moniker, IStream strm)
        {
            ComDebug.ReportInfo("SaveMonikerToStream:{0}", GetDisplayName(moniker));
            int hr = Ole32Func.OleSaveToStream((IPersistStream)moniker, strm);

            if (!(hr == ComReturnValue.S_OK))
            {
                throw new InvalidOperationException("The COM operation was not successful");
            }
        }
Example #5
0
        public static IBindCtx CreateBindCtx()
        {
            int rc = Ole32Func.CreateBindCtx(0, out var bc);

            if (!(rc == ComReturnValue.S_OK))
            {
                throw new InvalidOperationException("The COM operation was not successful");
            }
            return(bc);
        }
Example #6
0
        public static IRunningObjectTable GetROT()
        {
            Int32 hr = Ole32Func.GetRunningObjectTable(0, out var rot);

            if (!(hr == ComReturnValue.NOERROR))
            {
                throw new InvalidOperationException("The COM operation was not successful");
            }
            return(rot);
        }
Example #7
0
        public static void InternalSaveMiniProject(IStorage pStgSave, AltaxoDocument projectToSave, string graphDocumentName)
        {
            ComDebug.ReportInfo("GraphDocumentDataObject.InternalSaveMiniProject BEGIN");

            try
            {
                Exception saveEx = null;
                Ole32Func.WriteClassStg(pStgSave, typeof(GraphDocumentEmbeddedComObject).GUID);

                // Store the version of this assembly
                {
                    var     assembly = System.Reflection.Assembly.GetExecutingAssembly();
                    Version version  = assembly.GetName().Version;
                    using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoVersion", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
                    {
                        string text      = version.ToString();
                        byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(text);
                        stream.Write(nameBytes, 0, nameBytes.Length);
                    }
                }

                // Store the name of the item
                using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoGraphName", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
                {
                    byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(graphDocumentName);
                    stream.Write(nameBytes, 0, nameBytes.Length);
                }

                // Store the project
                using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoProjectZip", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
                {
                    using (var zippedStream = new System.IO.Compression.ZipArchive(stream, System.IO.Compression.ZipArchiveMode.Create))
                    {
                        var info = new Altaxo.Serialization.Xml.XmlStreamSerializationInfo();
                        projectToSave.SaveToZippedFile(zippedStream, info);
                    }
                    stream.Close();
                }

                if (null != saveEx)
                {
                    throw saveEx;
                }
            }
            catch (Exception ex)
            {
                ComDebug.ReportError("InternalSaveMiniProject, Exception ", ex);
            }
            finally
            {
                Marshal.ReleaseComObject(pStgSave);
            }

            ComDebug.ReportInfo("GraphDocumentDataObject.InternalSaveMiniProject END");
        }
Example #8
0
        public void Save(IStorage pStgSave, bool fSameAsLoad)
        {
            ComDebug.ReportInfo("{0}.IPersistStorage.Save fSameAsLoad={1}", GetType().Name, fSameAsLoad);

            try
            {
                Exception saveEx = null;
                Ole32Func.WriteClassStg(pStgSave, GetType().GUID);

                // Store the version of this assembly
                {
                    var     assembly = System.Reflection.Assembly.GetExecutingAssembly();
                    Version version  = assembly.GetName().Version;
                    using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoVersion", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
                    {
                        string text      = version.ToString();
                        byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(text);
                        stream.Write(nameBytes, 0, nameBytes.Length);
                    }
                }

                // Store the name of the item
                using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoGraphName", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
                {
                    byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(_document.Name);
                    stream.Write(nameBytes, 0, nameBytes.Length);
                }

                // Store the project
                using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoProjectZip", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
                {
                    _comManager.InvokeGuiThread(() =>
                    {
                        saveEx = Current.IProjectService.SaveProject(stream);
                    }
                                                );
                }

                _isDocumentDirty = false;

                if (null != saveEx)
                {
                    throw saveEx;
                }
            }
            catch (Exception ex)
            {
                ComDebug.ReportError("{0}.IPersistStorage:Save threw an exception: {1}", GetType().Name, ex);
            }
            finally
            {
                Marshal.ReleaseComObject(pStgSave);
            }
        }
        public ManagedOleAdviseHolderUO()
        {
            _invokeableThread = new InvokeableThread("OleAdviseThread", Current.Dispatcher);

            Invoke("Creation of IOleAdviseHolder", () =>
            {
                int res = Ole32Func.CreateOleAdviseHolder(out _oleAdviseHolder);
                if (!(res == ComReturnValue.S_OK))
                {
                    throw new InvalidOperationException("The COM operation was not successful");
                }
            });
        }
Example #10
0
        /// <summary>
        /// Unregisters the class factory.
        /// </summary>
        /// <returns></returns>
        public bool RevokeClassObject()
        {
            int i = Ole32Func.CoRevokeClassObject(_cookie);

            if (i == 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #11
0
        public static bool ResumeClassObjects()
        {
            int i = Ole32Func.CoResumeClassObjects();

            if (i == 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #12
0
        public override void SendAdvise_DataChanged()
        {
            base.SendAdvise_DataChanged();

            // we must also note the change time to the running object table, see
            // Brockschmidt, Inside Ole 2nd ed., page 989
            var rotCookie = _documentMonikerRotCookie;

            if (rotCookie != 0)
            {
                var ft = new System.Runtime.InteropServices.ComTypes.FILETIME();
                Ole32Func.CoFileTimeNow(out ft);
                RunningObjectTableHelper.GetROT().NoteChangeTime(rotCookie, ref ft);
            }
        }
Example #13
0
        /// <summary>
        /// Creates a new stream and renders the moniker into this stream. This function is intended for use with GetData(), but (!) not with GetDataHere().
        /// </summary>
        /// <param name="tymed">The tymed.</param>
        /// <param name="moniker">The moniker to render into the stream.</param>
        /// <returns>Newly created stream with the moniker rendered into.</returns>
        public static IntPtr RenderMonikerToNewStream(TYMED tymed, IMoniker moniker)
        {
            if (!(tymed == TYMED.TYMED_ISTREAM))
            {
                throw new ArgumentException(nameof(tymed) + " is not TYMED_ISTREAM");
            }

            int hr = Ole32Func.CreateStreamOnHGlobal(IntPtr.Zero, true, out var strm);

            if (!(hr == ComReturnValue.S_OK))
            {
                throw new InvalidOperationException("The COM operation was not successful");
            }
            SaveMonikerToStream(moniker, strm);
            return(Marshal.GetIUnknownForObject(strm)); // Increments the reference count
        }
Example #14
0
        /// <summary>
        /// Creates a new IStream as global data and renders data into it.
        /// </summary>
        /// <param name="tymed">The tymed. Used only to make sure we have the right tymed.</param>
        /// <param name="RenderToStreamProcedure">The render to stream procedure.</param>
        /// <returns>Global pointer to that stream.</returns>
        public static IntPtr RenderToNewStream(TYMED tymed, Action <IStream> RenderToStreamProcedure)
        {
            if (!(tymed == TYMED.TYMED_ISTREAM))
            {
                throw new ArgumentException(nameof(tymed) + " is not TYMED_ISTREAM");
            }

            int hr = Ole32Func.CreateStreamOnHGlobal(IntPtr.Zero, true, out var strm);

            if (!(hr == ComReturnValue.S_OK))
            {
                throw new InvalidOperationException();
            }

            RenderToStreamProcedure(strm);
            return(Marshal.GetIUnknownForObject(strm)); // Increments the reference count
        }
Example #15
0
        private IMoniker CreateNewDocumentMoniker()
        {
            // create the moniker on the fly
            IMoniker documentMoniker = null;

            var fileMoniker = _comManager.FileComObject.FileMoniker;

            if (null != fileMoniker)
            {
                Ole32Func.CreateItemMoniker("!", DataObjectHelper.NormalStringToMonikerNameString(_graphDocumentName), out var itemMoniker);
                if (null != itemMoniker)
                {
                    fileMoniker.ComposeWith(itemMoniker, false, out documentMoniker);
                }
            }
            return(documentMoniker);
        }
Example #16
0
        public static IntPtr RenderToNewStream(TYMED tymed, Action <System.IO.Stream> RenderToStreamProcedure)
        {
            if (!(tymed == TYMED.TYMED_ISTREAM))
            {
                throw new ArgumentException(nameof(tymed) + " is not TYMED_ISTREAM");
            }

            int hr = Ole32Func.CreateStreamOnHGlobal(IntPtr.Zero, true, out var strm);

            if (!(hr == ComReturnValue.S_OK))
            {
                throw new InvalidOperationException("The COM operation was not successful");
            }
            using (var strmWrapper = new ComStreamWrapper(strm, true))
            {
                RenderToStreamProcedure(strmWrapper);
            }
            return(Marshal.GetIUnknownForObject(strm)); // Increments the reference count
        }
Example #17
0
        private void EhCurrentProjectFileNameChanged(string fileName)
        {
            // see Brockschmidt, Inside Ole 2nd ed., page 996

            // make sure that if we have a valid file name, then the ComManager should be Active in order that this Com object is made public
            if (!string.IsNullOrEmpty(fileName) && !_comManager.IsActive)
            {
                ComDebug.ReportInfo("{0}.EhCurrentProjectFileNameChanged StartLocalServer because we have a valid file name. FileName: {1}", GetType().Name, fileName);
                _comManager.StartLocalServer();
            }

            ComDebug.ReportInfo("{0}.EhCurrentProjectFileNameChanged", GetType().Name);

            RunningObjectTableHelper.ROTUnregister(ref _fileWithWildCardItemMonikerRotCookie);
            _fileWithWildCardItemMoniker = null;
            RunningObjectTableHelper.ROTUnregister(ref _fileMonikerRotCookie);
            _fileMoniker = null;

            if (!string.IsNullOrEmpty(fileName))
            {
                Ole32Func.CreateFileMoniker(fileName, out _fileMoniker);
                if (null != _fileMoniker)
                {
                    RunningObjectTableHelper.ROTRegisterAsRunning(_fileMoniker, this, ref _fileMonikerRotCookie, typeof(IPersistFile));

                    // Notify all other item Com objects of the new _fileMoniker
                    if (null != FileMonikerChanged)
                    {
                        FileMonikerChanged(_fileMoniker);
                    }

                    // now register also a file moniker with a wild card item, that handles all items that are not open in the moment
                    Ole32Func.CreateItemMoniker("!", "\\", out var wildCardItemMoniker);
                    if (null != wildCardItemMoniker)
                    {
                        _fileMoniker.ComposeWith(wildCardItemMoniker, false, out _fileWithWildCardItemMoniker);
                        RunningObjectTableHelper.ROTRegisterAsRunning(_fileWithWildCardItemMoniker, this, ref _fileWithWildCardItemMonikerRotCookie, typeof(IOleItemContainer));
                    }
                }
            }
        }
Example #18
0
        /// <summary>
        /// Registers the class factory.
        /// </summary>
        /// <returns></returns>
        public bool RegisterClassObject()
        {
            // Register the class factory
            int i = Ole32Func.CoRegisterClassObject
                    (
                ref _classId,
                this,
                ClassContext,
                Flags,
                out _cookie
                    );

            ComDebug.ReportInfo("{0}.RegisterClassObject, i={1}, _cookie={2}", GetType().Name, i, _cookie);

            if (i == 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }