Пример #1
0
        void TestSaveLoad()
        {
            int     hr;
            IStream uis = null;
            long    siz;

            hr = m_ips.GetSizeMax(out siz);

            // Create the stream to write to
            hr = CreateStreamOnHGlobal(IntPtr.Zero, true, out uis);

            // false doesn't seem to work
            hr = m_ips.Save(uis, true);
            DsError.ThrowExceptionForHR(hr);

            // See if the dirty bit got cleared
            hr = m_ips.IsDirty();
            Debug.Assert(hr == 1, "dirty3");

            System.Runtime.InteropServices.ComTypes.STATSTG p;
            uis.Stat(out p, 0);

            // Make sure something got written
            Debug.Assert(p.cbSize > 0, "Save");

            // Read it back
            uis.Seek(0, 0, IntPtr.Zero);

            hr = m_ips.Load(uis);
            DsError.ThrowExceptionForHR(hr);
        }
Пример #2
0
        public static int SaveSize(this IPersistStream persistStream)
        {
            var tmp = new ULARGE_INTEGER[1];

            persistStream.GetSizeMax(tmp);
            return((int)tmp[0].QuadPart);
        }
Пример #3
0
        public void SaveStateToCode(bool cpp)
        {
            IPersistStream ips = BaseFilter as IPersistStream;

            if (ips == null)
            {
                return;
            }
            long sz = 0;

            Graph.CheckHR(ips.GetSizeMax(out sz), "ips.GetSizeMax");
            if (sz <= 0 || sz > 1024 * 1024)
            {
                MessageBox.Show(string.Format("Weird size returned by IPersistStream::GetSizeMax: {0}", sz));
            }
            else
            {
                IntPtr hg = Marshal.AllocHGlobal((int)sz);
                try
                {
                    IStream stream;
                    Graph.CheckHR(FilterGraphTools.CreateStreamOnHGlobal(hg, false, out stream), "CreateStreamOnHGlobal");
                    Graph.CheckHR(ips.Save(stream, false), "IPersistStream::Save");
                    IntPtr ppos = Marshal.AllocHGlobal(8);
                    stream.Seek(0, 1, ppos);
                    long pos = Marshal.ReadInt64(ppos);
                    if (pos > 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("//somewhere outside a function:");
                        if (cpp)
                        {
                            sb.Append("static unsigned char filter_data[] = {");
                        }
                        else
                        {
                            sb.AppendLine("[System.Runtime.InteropServices.DllImport(\"OLE32.DLL\", EntryPoint = \"CreateStreamOnHGlobal\")]");
                            sb.AppendLine("extern public static int CreateStreamOnHGlobal(IntPtr ptr, bool delete, out IStream pOutStm);");
                            sb.AppendLine();
                            sb.Append("byte[] filter_data = {");
                        }

                        for (int i = 0; i < pos; i++)
                        {
                            if (i > 0)
                            {
                                sb.Append(", ");
                            }
                            if (i % 16 == 0)
                            {
                                sb.Append("\r\n  ");
                            }
                            sb.AppendFormat("{0}", Marshal.ReadByte(hg, i));
                        }
                        sb.AppendLine("\r\n};");


                        sb.AppendLine("\r\n//in your graph building code:");
                        if (cpp)
                        {
                            sb.AppendLine("HGLOBAL hg = GlobalAlloc(0, sizeof(filter_data));");
                            sb.AppendLine("memcpy(hg, filter_data, sizeof(filter_data));");
                            sb.AppendLine("IStream *pStream = NULL;");
                            sb.AppendLine("if (CreateStreamOnHGlobal(hg, FALSE, &pStream)==0) {");
                            sb.AppendLine("    CComQIPtr<IPersistStream, &IID_IPersistStream> ips(pFilter);");
                            sb.AppendLine("    if (ips) {");
                            sb.AppendLine("        hr = ips->Load(pStream);");
                            sb.AppendLine("        CHECK_HR(hr, _T(\"Can't load filter state.\"));");
                            sb.AppendLine("    }");
                            sb.AppendLine("}");
                            sb.AppendLine("GlobalFree(hg);");
                        }
                        else
                        {
                            sb.AppendLine("IPersistStream ips = pFilter as IPersistStream;");
                            sb.AppendLine("if (ips != null)");
                            sb.AppendLine("{");
                            sb.AppendLine("    IStream stream;");
                            sb.AppendLine("    IntPtr hg = Marshal.AllocHGlobal(filter_data.Length);");
                            sb.AppendLine("    Marshal.Copy(filter_data, 0, hg, filter_data.Length);");
                            sb.AppendLine("    if (CreateStreamOnHGlobal(hg, false, out stream) == 0)");
                            sb.AppendLine("        checkHR(ips.Load(stream), \"Can't load filter state.\");");
                            sb.AppendLine("    Marshal.FreeHGlobal(hg);");
                            sb.AppendLine("}");
                        }
                        using (var cf = new GenerateCodeForm(sb.ToString()))
                            cf.ShowDialog();
                        //MessageBox.Show(sb.ToString());
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(hg);
                }
            }
        }
 public void GetSizeMax(out _ULARGE_INTEGER pcbSize)
 {
     persist.GetSizeMax(out pcbSize);
 }