public MatEngine(bool visible) { int ret; Process[] pros = Process.GetProcessesByName("MATLAB"); _engine = engine.engOpenSingleUse(string.Empty, IntPtr.Zero, out ret); Process[] pros2 = Process.GetProcessesByName("MATLAB"); bool has = false; for (int i = 0, j = pros2.Length; i < j; i++) { has = false; for (int x = 0, y = pros.Length; x < y; x++) { if (pros2[i].Id == pros[x].Id) { has = true; break; } } if (!has) { this.Process = pros2[i]; break; } } if (_engine.IsInvalid) { throw new ApplicationException("The matlab opend failed."); } if (!visible) { engine.engSetVisible(_engine, visible); } }
public static extern int engPutArray(SafeEnginePtr engine, SafeArrayPtr mp);
public static extern int engOutputBuffer(SafeEnginePtr engine, char[] buffer, int buflen);
public static extern int engGetVisible(SafeEnginePtr engine, [MarshalAs(UnmanagedType.U1)]out bool val);
public static extern SafeArrayPtr engGetVariable(SafeEnginePtr engine, string variableName);
public static extern SafeArrayPtr engGetArray(SafeEnginePtr engine, string name);
public static extern int engEvalString(SafeEnginePtr engine, string cmdstring);
public extern static int engPutArray(SafeEnginePtr engine, SafeArrayPtr mp);
public extern static int engGetVisible(SafeEnginePtr engine, [MarshalAs(UnmanagedType.U1)] out bool val);
public extern static int engSetVisible(SafeEnginePtr engine, [MarshalAs(UnmanagedType.U1)] bool newVal);
public extern static int engPutVariable(SafeEnginePtr engine, string variableName, SafeArrayPtr ap);
public extern static SafeArrayPtr engGetVariable(SafeEnginePtr engine, string variableName);
public extern static int engOutputBuffer(SafeEnginePtr engine, char[] buffer, int buflen);
public extern static int engEvalString(SafeEnginePtr engine, string cmdstring);
public static extern int engPutVariable(SafeEnginePtr engine, string variableName, SafeArrayPtr ap);
public static extern int engSetVisible(SafeEnginePtr engine, [MarshalAs(UnmanagedType.U1)]bool newVal);
public extern static SafeArrayPtr engGetArray(SafeEnginePtr engine, string name);