Leaders snarfblam Posted September 19, 2005 Leaders Posted September 19, 2005 I have begun to write a program that will allow users to write scripts in VB/C#. Their scripts are compiled to in-memory assemblies using the compilers obtained by the Microsoft.VisualBasic.VBCodeProvider.GetCompiler and Microsoft.CSharp.CSharpCodeProvider.GetCompiler methods. I have tried handling the AppDomain.CurrentDomain.Unhandled exception and Application.CurrentThreadException (from both outside and inside the script) and script exceptions are not caught by these handlers. What I want to do is catch script exceptions before they crash my program, end the script, and notify the user. How can I go about catching an unhandled exception in the script so that I can do this? I can not assume that the script writer will write unhandled exception proof code. The script can actually create a GUI in a panel that will be hosted in my application and utilize event handling, so I can not simply wrap function calls with a try/catch. Is there hope for my nifty-scriptable program? Quote [sIGPIC]e[/sIGPIC]
Administrators PlausiblyDamp Posted September 19, 2005 Administrators Posted September 19, 2005 How are you compiling / executing the script? Could you post either the code or a simplified version that demonstrates this problem? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders snarfblam Posted September 21, 2005 Author Leaders Posted September 21, 2005 I am using a method nearly identical to that of a tutorial found on these forums. CompileScript, well... compiles the script. FindInterface returns an instance of the first object it finds (and there should only be one defined in the script) that implements my script interface, IScript. Public Shared Function CompileScript(ByVal Source As String) As CompilerResults 'Reference to host app's exe Dim Reference As String = Application.ExecutablePath Dim Provider As New Microsoft.VisualBasic.VBCodeProvider Dim Compiler As ICodeCompiler = Provider.CreateCompiler Dim CompilerParam As New CompilerParameters Dim Results As CompilerResults 'Set parameters CompilerParam.GenerateExecutable = False CompilerParam.GenerateInMemory = True CompilerParam.IncludeDebugInformation = False CompilerParam.ReferencedAssemblies.Add("System.Windows.Forms.dll") CompilerParam.ReferencedAssemblies.Add("System.dll") CompilerParam.ReferencedAssemblies.Add("System.Drawing.dll") If Not Reference Is Nothing AndAlso Reference.Length <> 0 Then _ CompilerParam.ReferencedAssemblies.Add(Reference) Results = Compiler.CompileAssemblyFromSource(CompilerParam, Source) Provider.Dispose() Return Results End Function Public Shared Function FindInterface(ByVal DLL As Reflection.Assembly, ByVal InterfaceName As String) As Object Dim t As Type 'Loop through types looking for one that implements the given interface For Each t In DLL.GetTypes() If Not (t.GetInterface(InterfaceName, True) Is Nothing) Then Return DLL.CreateInstance(t.FullName) End If Next Return Nothing End Function The panel for the UI is obtained by an IScript function and, for the time being, is shown in a newly generated form. Ultimately, I am going to implement a tabbed interface. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.