示例#1
0
 internal static extern int gpgme_data_new_from_cbs(
     [Out] out IntPtr dh,
     [In][MarshalAs(UnmanagedType.FunctionPtr)] _gpgme_data_cbs cbs,  //gpgme_data_cbs_t
     [In] IntPtr handle);
        private void Init(bool canRead, bool canWrite, bool canSeek, bool canRelease)
        {
            #if (VERBOSE_DEBUG)
            DebugOutput("GpgmeCbsData.Init(" + canRead.ToString() + ","
                        + canWrite.ToString() + ","
                        + canSeek.ToString() + ","
                        + canRelease.ToString() + ")");
            #endif
            handle = IncGlobalHandle(); // increment the global handle

            cbs = new _gpgme_data_cbs();
            cbs_lfs = new _gpgme_data_cbs_lfs();

            // Read function
            if (canRead)
            {
                cbs.read = new gpgme_data_read_cb_t(_read_cb);
                cbs_lfs.read = new gpgme_data_read_cb_t(_read_cb);
            }
            else
            {
                cbs.read = null;
                cbs_lfs.read = null;
            }

            // Write function
            if (canWrite)
            {
                cbs.write = new gpgme_data_write_cb_t(_write_cb);
                cbs_lfs.write = new gpgme_data_write_cb_t(_write_cb);
            }
            else
            {
                cbs.write = null;
                cbs_lfs.write = null;
            }

            // Seek function
            if (canSeek)
            {
                cbs.seek = new gpgme_data_seek_cb_t(_seek_cb);
                cbs_lfs.seek = new gpgme_data_seek_cb_t_lfs(_seek_cb_lfs);
            }
            else
            {
                cbs.seek = null;
                cbs_lfs.seek = null;
            }

            // Release
            if (canRelease)
            {
                cbs.release = new gpgme_data_release_cb_t(_release_cb);
                cbs_lfs.release = new gpgme_data_release_cb_t(_release_cb);
            }
            else
            {
                cbs.release = null;
                cbs_lfs.release = null;
            }

            pinnedCbs = GCHandle.Alloc(cbs);
            pinnedCbs_lfs = GCHandle.Alloc(cbs_lfs);
            if (libgpgme.use_lfs)
            {
                cbsPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(cbs_lfs));
                Marshal.StructureToPtr(cbs_lfs, cbsPtr, false);
            } else {
                cbsPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(cbs));
                Marshal.StructureToPtr(cbs, cbsPtr, false);
            }

            int err = libgpgme.gpgme_data_new_from_cbs(
                out dataPtr,
                cbsPtr,
                handle);

            #if (VERBOSE_DEBUG)
            DebugOutput("gpgme_data_new_from_cbs(..) DONE.");
            #endif
            gpg_err_code_t errcode = libgpgme.gpgme_err_code(err);

            if (errcode == gpg_err_code_t.GPG_ERR_NO_ERROR)
                return;

            if (errcode == gpg_err_code_t.GPG_ERR_ENOMEM)
                throw new OutOfMemoryException("Not enough memory available to create user defined GPGME data object.");

            throw new GeneralErrorException("Unknown error " + errcode + " (" + err + ")");
        }