Пример #1
0
        private byte[] xamStructToBytesB(ZBpara s)
        {
            int size = Marshal.SizeOf(s);

            System.IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(s, ptr, true);
            byte[] ba = new byte[size];

            Marshal.Copy(ptr, ba, 0, ba.Length);
            return(ba);
        }
Пример #2
0
        private ZBpara xamBytesToStructB(byte[] arr)
        {
            ZBpara struReturn = new ZBpara();
            int    size       = Marshal.SizeOf(struReturn);

            System.IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.Copy(arr, 0, ptr, arr.Length);

            struReturn = (ZBpara)Marshal.PtrToStructure(ptr, struReturn.GetType());
            Marshal.FreeHGlobal(ptr);
            return(struReturn);
        }
Пример #3
0
        public bool GetDefaultSetting(ref ZBpara mpara)
        {
            bool   error = false;
            string path  = Application.StartupPath;

            path = path + @"\DefaultData";
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            if (error == false)
            {
                path = path + @"\DefaultSetting.ini";
                if (System.IO.File.Exists(path))     // 参数定义文件存在!
                {
                    ZBpara mp        = new ZBpara(); // 定义一个存储 参数表 的数据结构实例
                    byte[] readblock = new byte[Marshal.SizeOf(mp)];

                    FileStream   afile = new FileStream(path, FileMode.Open);
                    BinaryReader r     = new BinaryReader(afile);    // 定义一个 二进制 文件流

                    try                                              // 开始读取 参数文件 结构,由于本参数表只有一个 结构,故只读入一个结构数据即可
                    {
                        readblock = r.ReadBytes(Marshal.SizeOf(mp)); // 从定义的二进制文件流 中读取 MatchPixel结构 的二进制数据 长度数据
                        mp        = xamBytesToStructB(readblock);

                        mpara = mp; // 将读取的值 置入需要的 MatchPixel实例中
                    }
                    catch (Exception ex)
                    {
                        error = true;
                        mpara.Init(); // 置初始化数据
                        MessageBox.Show("读参数文件出错!" + ex.Message);
                    }
                    finally
                    {
                        r.Close();
                        afile.Close();
                        afile.Dispose();
                    }
                }
                else
                {
                    mpara.Init();
                    error = true;
                }
            }
            return(!error);
        }
Пример #4
0
        public bool SetDefaultSetting(ZBpara mpara) // 保存参数表结构为 默认参数表
        {
            bool   error = false;
            string path  = Application.StartupPath;

            path = path + @"\DefaultData";
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            if (error == false)
            {
                path = path + @"\DefaultSetting.ini";
                if (System.IO.File.Exists(path))
                {
                    File.Delete(path); // 如果存在原来的 ini 文件,删除重写!
                }
                FileStream   afile = new FileStream(path, FileMode.Append);
                BinaryWriter r     = new BinaryWriter(afile);       // 定义一个 二进制 文件流

                byte[] readblock = new byte[Marshal.SizeOf(mpara)]; // 将参数表结构转换为 二进制流 数据
                readblock = xamStructToBytesB(mpara);
                try
                {
                    r.Write(readblock);
                }
                catch (Exception ex)
                {
                    error = true;
                    MessageBox.Show("参数文件保存出错!" + ex.Message);
                }
                finally
                {
                    r.Close();
                    afile.Close();
                    afile.Dispose();
                }
            }
            return(!error);
        }