You are on page 1of 7

C# goi maple package _ nh Th

2013

TO PACKAGE TRONG MAPLE, GI TRONG C#


Bc 1: To package trong Maple...............................................................................................................2 Bc 2: S dng trong C#...........................................................................................................................3

C# goi maple package _ nh Th

2013

Bc 1: To package trong Maple


V d mun to package TEST trong c hm Tong(a,b,c), tr v tng a,b,c. Package to ra c lu trong th mc no , vd : D:/TAM. Ta son file TEST.mw bnh thng sau dng lnh lu li, n s thnh file TEST.m lu trong D:/TAM/TEST.m, copy file ny vo th mc Lib ca Maple l ok. Ch nu bn ci Maple 12 v Maple 16 th copy n vo Maple 16 tng ng. file : TEST.mw
> TEST := table(): > TEST[init] := proc() end: > TEST[Tong] := proc (a,b,c) local tong; tong:= a + b + c; printf("Tong cua %A , %A va %A la %A :",a,b,c, tong); end proc: #g Enter #g Enter sau lnh save #To th mc D:/TAM trc > save(TEST,"D:/TAM/TEST.m"); > restart; #g Enter

Copy : file TEST.m trong D:/TAM vo Maple 16/Lib. Xong ri , test th


>

Tong cua 2 , 3 va 4 la 9 :

--Ngon lnh, xong bc 1.

C# goi maple package _ nh Th

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); } }

C# goi maple package _ nh Th

2013

- X l chnh : Form1.cs To link vi Maple ngay sau khi to 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; } 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) { } }

Cc hm khc cho MapleEngine Xut kt qu


public void cbText(IntPtr data, int tag, IntPtr output) { //Xut kt qu ra ngoi txtOutput.Text = Marshal.PtrToStringAnsi(output); }

Cc hm khc, khng hiu lm nn mnh trng


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) { }

Ti hm tnh theo input


private void Tinh_Click(object sender, EventArgs e) { MapleEngine.MapleCallbacks cb; byte[] err = new byte[2048]; IntPtr kv;

C# goi maple package _ nh Th


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 {

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) { } }

Code hon chnh Form1.cs


using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; System.Runtime.InteropServices;

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; }

C# goi maple package _ nh Th

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) { }

} }

C# goi maple package _ nh Th

2013

Kt qu

May qu ng ri. @Tham kho : http://www.mapleprimes.com/posts/38048-Using-OpenMaple-With-C

You might also like