Professional Documents
Culture Documents
2013
2013
Tong cua 2 , 3 va 4 la 9 :
2013
Bc 2: S dng trong C#
To Project, ConnectToMaple
ConnecToMaple s s dng package TEST ta mi to, khi nhp 2,3,4 n s tnh ra tng (hi vng ra 9). Thit k giao din tng t nh hnh : - txtInput : nhp tham s vo - txtOutput : ra kt qu - tinh : Tnh kt qu Cc lp : - MapleEngine.cs : ci ny dng link vi Maple
using using using using using using System; System.Collections.Generic; System.Linq; System.Text; System.Runtime.InteropServices; System.ComponentModel;
namespace ConnectToMaple //ch i tn namespace trng tn project nu mun dng li { static class MapleEngine { public delegate void TextCallBack(IntPtr data, int tag, IntPtr output); public delegate void ErrorCallBack(IntPtr data, IntPtr offset, IntPtr msg); public delegate void StatusCallBack(IntPtr data, IntPtr used, IntPtr alloc, double time); public delegate IntPtr ReadLineCallBack(IntPtr data, IntPtr debug); public delegate long RedirectCallBack(IntPtr data, IntPtr name, IntPtr mode); public delegate IntPtr StreamCallBack(IntPtr data, IntPtr stream, int nargs, IntPtr args); public delegate long QueryInterrupt(IntPtr data); public delegate IntPtr CallBackCallBack(IntPtr data, IntPtr output); public struct MapleCallbacks { public TextCallBack textCallBack; public ErrorCallBack errorCallBack; public StatusCallBack statusCallBack; public ReadLineCallBack readlineCallBack; public RedirectCallBack redirectCallBack; public StreamCallBack streamCallBack; public QueryInterrupt queryInterrupt; public CallBackCallBack callbackCallBack; } [DllImport(@"maplec.dll")] public static extern IntPtr StartMaple(int argc, String[] argv, ref MapleCallbacks cb, IntPtr data, IntPtr info, byte[] err); [DllImport(@"maplec.dll")] public static extern IntPtr EvalMapleStatement(IntPtr kv, byte[] statement); [DllImport(@"maplec.dll")] public static extern IntPtr IsMapleStop(IntPtr kv, IntPtr obj); [DllImport(@"maplec.dll")] public static extern void StopMaple(IntPtr kv); } }
2013
kv = MapleEngine.StartMaple(2, argv, ref cb, IntPtr.Zero, IntPtr.Zero, err); } catch (DllNotFoundException) { return; } catch (EntryPointNotFoundException) { return; } if (kv.ToInt64() == 0) { MessageBox.Show("Fatal Error, could not start Maple: " + System.Text.Encoding.ASCII.GetString(err, 0, Array.IndexOf(err, (byte)0)), "Li", MessageBoxButtons.OK); return; } //Load package mnh va vit, tn l TEST try { MapleEngine.EvalMapleStatement(kv, Encoding.ASCII.GetBytes("with(TEST):")); } catch (Exception) { } }
2013
kv = MapleEngine.StartMaple(2, argv, ref cb, IntPtr.Zero, IntPtr.Zero, err); try { //s dng hm Tong trong package TEST String expr = "Tong("; expr += txtInput.Text; expr += ");"; IntPtr val = MapleEngine.EvalMapleStatement(kv, Encoding.ASCII.GetBytes(expr)); } catch (Exception) { MessageBox.Show("Khng th load Maple", "Li", MessageBoxButtons.OK); }
} catch (Exception) { } }
namespace ConnectToMaple { public partial class Form1 : Form { public Form1() { InitializeComponent(); //load Maple MapleEngine.MapleCallbacks cb; byte[] err = new byte[2048]; IntPtr kv; String[] argv = new String[2]; argv[0] = "maple"; argv[1] = "-A2"; cb.textCallBack = cbText; cb.errorCallBack = cbError; cb.statusCallBack = cbStatus; cb.readlineCallBack = null; cb.redirectCallBack = null; cb.streamCallBack = null; cb.queryInterrupt = null; cb.callbackCallBack = null; try { kv = MapleEngine.StartMaple(2, argv, ref cb, IntPtr.Zero, IntPtr.Zero, err); } catch (DllNotFoundException) { return; } catch (EntryPointNotFoundException) { return; }
2013
if (kv.ToInt64() == 0) { MessageBox.Show("Fatal Error, could not start Maple: " + System.Text.Encoding.ASCII.GetString(err, 0, Array.IndexOf(err, (byte)0)), "Li", MessageBoxButtons.OK); return; } //Load package mnh va vit, tn l TEST try { MapleEngine.EvalMapleStatement(kv, Encoding.ASCII.GetBytes("with(TEST):")); } catch (Exception) { } } public void cbText(IntPtr data, int tag, IntPtr output) { //Xut kt qu ra ngoi txtOutput.Text = Marshal.PtrToStringAnsi(output); } public static void cbError(IntPtr data, IntPtr offset, IntPtr msg) { string s = Marshal.PtrToStringAnsi(msg); } public static void cbStatus(IntPtr data, IntPtr used, IntPtr alloc, double time) { } private void Tinh_Click(object sender, EventArgs e) { MapleEngine.MapleCallbacks cb; byte[] err = new byte[2048]; IntPtr kv; String[] argv = new String[2]; argv[0] = "maple"; argv[1] = "-A2"; cb.textCallBack = cbText; cb.errorCallBack = cbError; cb.statusCallBack = cbStatus; cb.readlineCallBack = null; cb.redirectCallBack = null; cb.streamCallBack = null; cb.queryInterrupt = null; cb.callbackCallBack = null; try { kv = MapleEngine.StartMaple(2, argv, ref cb, IntPtr.Zero, IntPtr.Zero, err); try { //s dng hm Tong trong package TEST String expr = "Tong("; expr += txtInput.Text; expr += ");"; IntPtr val = MapleEngine.EvalMapleStatement(kv, Encoding.ASCII.GetBytes(expr)); } catch (Exception) { MessageBox.Show("Khng th load Maple", "Li", MessageBoxButtons.OK); }
} catch (Exception) { }
} }
2013
Kt qu