private void button1_Click(object sender, EventArgs e) { if (txtInput.Text.Length == 0) { txtError.Text = "Input is empty!"; txtError.Visible = true; return; } using (ScriptEngine engine = new ScriptEngine("jscript")) { ParsedScript parsed = engine.Parse(jstext.Text); txtError.Text = ""; txtError.Visible = false; try { String sC = selectedCodeID.SelectedItem.ToString().Substring(selectedCodeID.SelectedItem.ToString().Length - 1); String sI = txtInput.Text; String result = (parsed.CallMethod("dataEdit", new object[] { util.decodeWithHex(sI), sC })).ToString(); System.Diagnostics.Debug.WriteLine("result=" + util.encodeWithHex(result)); txtResult.Text = util.encodeWithHex(result); txtError.Visible = false; } catch (ScriptException ex) { System.Diagnostics.Debug.WriteLine("Exception: " + ex.Line + ":" + ex.Column + ": " + ex.Description + "\n" + ex.Message); txtError.Visible = true; txtError.Text = ex.Message; } } }
private object Parse(string text, bool expression) { const string varName = "x___"; object result; _engine.SetScriptState(ScriptState.Connected); ScriptText flags = ScriptText.None; if (expression) { flags |= ScriptText.IsExpression; } try { // immediate expression computation seems to work only for 64-bit // so hack something for 32-bit... System.Runtime.InteropServices.ComTypes.EXCEPINFO exceptionInfo; if (_parse32 != null) { if (expression) { // should work for jscript & vbscript at least... text = varName + "=" + text; } _parse32.ParseScriptText(text, null, IntPtr.Zero, null, 0, 0, flags, out result, out exceptionInfo); } else { _parse64.ParseScriptText(text, null, IntPtr.Zero, null, 0, 0, flags, out result, out exceptionInfo); } } catch { if (Site.LastException != null) { throw Site.LastException; } throw; } IntPtr dispatch; if (expression) { // continue our 32-bit hack... if (_parse32 != null) { _engine.GetScriptDispatch(null, out dispatch); object dp = Marshal.GetObjectForIUnknown(dispatch); try { return(dp.GetType().InvokeMember(varName, BindingFlags.GetProperty, null, dp, null)); } catch { if (Site.LastException != null) { throw Site.LastException; } throw; } } return(result); } _engine.GetScriptDispatch(null, out dispatch); ParsedScript parsed = new ParsedScript(this, dispatch); return(parsed); }