End-user embedded programable VB inside C# - c#

How would it be possible to build a complete C# application with the feature of creating new functionalities thru new VB files. Those files shouldn't have to be compiled but interpreted in runtime.
I think of it as an embedded VB interpreter, but don't know how it can be accomplished.
You could build a robust base application and then let your technicians adapt it to the particularities of each client (databases, tables, filters, network services,...)
A client of mine has a software with that open functionality but I ignore the details.
It also be great if python could be integrated!

Using VBCodeProvider you can compile VB.NET code at run-time.
The following example, compiles a piece of VB.NET code at run-time and run it:
private void button1_Click(object sender, EventArgs e)
{
using (var vbc = new VBCodeProvider())
{
var parameters = new CompilerParameters(new[] {
"mscorlib.dll",
"System.Windows.Forms.dll",
"System.dll",
"System.Drawing.dll",
"System.Core.dll",
"Microsoft.VisualBasic.dll"});
var results = vbc.CompileAssemblyFromSource(parameters,
#"
Imports System.Windows.Forms
Imports System.Drawing
Public Class Form1
Inherits Form
public Sub New ()
Dim b as Button = new Button()
b.Text = ""Button1""
AddHandler b.Click,
Sub (s,e)
MessageBox.Show(""Hello from runtime!"")
End Sub
Me.Controls.Add(b)
End Sub
End Class");
//Check if compilation is successful, run the code
if (!results.Errors.HasErrors)
{
var t = results.CompiledAssembly.GetType("Form1");
Form f = (Form)Activator.CreateInstance(t);
f.ShowDialog();
}
else
{
var errors = string.Join(Environment.NewLine,
results.Errors.Cast<CompilerError>()
.Select(x => x.ErrorText));
MessageBox.Show(errors);
}
}
}

Related

how to use Utils.FormIsOpen in c#

I need help using Utils in C#.
private void manageUsersToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!Utils.FormIsOpen("ManageUsers"))// this line gives an error
{
var manageUsers = new ManageUsers();
manageUsers.MdiParent = this;
manageUsers.Show();
}
}
I am using Microsoft SQL Server Management Studio 18 along with visual studio and I am trying to manage my users in the database while not physically adding them to the database but adding them at runtime.
It seems that there is no FormIsOpen method in Utils Class.
If you want to check if a "ManageUsers" has been opened, you can call Form.MdiChildren Property to get all mdichildforms and filter them.
// get all child form
Form[] children = this.MdiChildren;
var query = children.Select(c => c)
.Where(c => c is ManageUsers).ToList();
if(query.Count == 0)
{
var manageUsers = new ManageUsers();
manageUsers.MdiParent = this;
manageUsers.Show();
}

Fb EventHandler

I have written a windows service in Visual Studio that uses the FirebirdSql.Data.FirebirdClient ADO.NET provider. My code is:
FbRemoteEvent revent = new FbRemoteEvent(fb);
revent.AddEvents(new string[] { "new_g" });
revent.RemoteEventCounts += new FbRemoteEventEventHandler (EventCounts);
revent.QueueEvents();
I'm using FirebirdSql.Data.FirebirdClient, added in reference FirebirdSql.Data.FirebirdClient.dll, but if I write FbRemoteEventEventHandler error appears.
Why? What should be done?
if found also nothing and there was just the declaration in the object catalogue :/
public event System.EventHandler<FirebirdSql.Data.FirebirdClient.FbRemoteEventEventArgs> RemoteEventCounts
so please try this ...
FbRemoteEvent revent = new FbRemoteEvent(fb);
revent.AddEvents(new string[] { "new_g" });
revent.RemoteEventCounts += new EventHandler<FbRemoteEventEventArgs>(EventCounts);
revent.QueueEvents();

Use vb.net dll in C#. How to acces the objects in C# form from vb.net dll?

I cant completly converted the vb.net code to c# code so i decided to make a vb.net dll and then add it to c# form.
But im new about this dll things and i dont know how to acces the objects in C# form. I added C# application to references in dll as i did for the dll too. But i cant still access the timer and 2 labels which are in C# application.
This is my vb.net dll
Public Class Class1
Public Sub Bypass(block1 As String, block2 As String, ok1 As String, ok2 As String)
Try
Dim folderPath As String = Environment.GetFolderPath(SpecialFolder.Windows)
FileSystem.FileClose(New Integer() {1})
FileSystem.FileClose(New Integer() {2})
If My.Computer.FileSystem.FileExists((folderPath & "\xspirit.sys")) Then
FileSystem.FileOpen(1, (folderPath & "\xspirit.sys"), OpenMode.Append, OpenAccess.ReadWrite, OpenShare.LockReadWrite, -1)
Else
File.WriteAllBytes((folderPath & "\xspirit.sys"), New Byte() {0})
FileSystem.FileOpen(1, (folderPath & "\xspirit.sys"), OpenMode.Append, OpenAccess.ReadWrite, OpenShare.LockReadWrite, -1)
block1 = "Erişim Engellendi"
MsgBox("Erişim Engellendi xspirit")
End If
If My.Computer.FileSystem.FileExists((folderPath & "\xhunter1.sys")) Then
FileSystem.FileOpen(2, (folderPath & "\xhunter1.sys"), OpenMode.Append, OpenAccess.ReadWrite, OpenShare.LockReadWrite, -1)
Else
File.WriteAllBytes((folderPath & "\xhunter1.sys"), New Byte() {0})
FileSystem.FileOpen(2, (folderPath & "\xhunter1.sys"), OpenMode.Append, OpenAccess.ReadWrite, OpenShare.LockReadWrite, -1)
block2 = "Erişim Engellendi"
MsgBox("Erişim Engellendi xhunter1")
End If
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim ex As Exception = exception1
ProjectData.SetProjectError(ex)
Dim exception As Exception = ex
ProjectData.ClearProjectError()
ProjectData.ClearProjectError()
End Try
Dim p As Process()
p = Process.GetProcessesByName("Wolfteam.bin") 'set wolfteam process
If p.Count = 1 Then ' if wolfteam process detected
ok1 = "XignCode Clear Başarılı"
ok2 = "XignCode Clear Başarılı"
MsgBox("XignCode Clear Başarılı xspirit")
MsgBox("XignCode Clear Başarılı xhunter1")
End If
End Sub
End Class
I tried to convert C# but i cant do it completely so i tried to access the objects from my dll with this code but i couldn't do it (Yes i added it to references).
C#app.Form1(its okay until here but i cant continue this code.It doesn't accept the rest of it)
I wanted to write this actually:
C#app.Form1.Timer1.Enabled = False or C#app.Form1.label1.Text = "test"
I tried too:
Dim test1 As String="test"
'then acces them from C# and then:
vbnetdll.Class1 tt = new vbnetdll.Class1();
label1.Text=f.vbmethod.test1;
But i couldn't do this to. Because it doesn't accept. What is wrong?
By default when you add an item to a form it is declared as a private field in the form's class, like this:
private System.Windows.Forms.Label label1;
So it's not going to accessible from another assembly that references the form. It actually has nothing to do with C# vs vb.net.

run matlab script that process series of images in visual studio(c# language)?

using following Matlab code I have successfully load the images in folder (location:F:\anuradha)
but when it is run through visual studio (c# language) results were not obtained.
can anyone help me????
%code_01
filePattern1 = fullfile('F:\anuradha\', '*.jpg');
imagefiles1 = dir(filePattern1);
it should be mention here I have create object in C# and the matlab script is passed in to string the following code gives more explanation
string mScript;
MLApp.MLApp matlab = new MLApp.MLApp();
private void loadMScript_segment()
{
System.IO.StreamReader myFile =
new System.IO.StreamReader("F:\\anuradha\\resizeing.m");
mScript = myFile.ReadToEnd();
myFile.Close();
}
private void buttonsgmnt_Click(object sender, EventArgs e)
{
loadMScript_segment();
matlab.Execute(mScript);
}
here resizeing.m is the matlab script which is included code_01 mention above
here it is not the case of execution pattern. it gives error at the matlab script, which is mention below,
%resizeing.m
filePattern1 = fullfile('F:\anuradha\', '*.jpg');
imagefiles1 = dir(filePattern1);
imagefiles1 = dir(filePattern1);
nfiles1 = length(imagefiles1);
for jj=1:nfiles1
currentfilename1 = imagefiles1(jj).name;
currentimage1 = imread(currentfilename1);
images{jj} = currentimage1;
b = currentimage1;
figure;
imshow(b);
hold on;
end
according to the above code it should be display all image inside the folder after run the c# code

Accessing WinForm Controls using C# dynamic code

I have WinForm called Form1 and there are Button1, MemoEdit1 and 2 TextBoxes named TextBox1 and TextBox2. At runtime user should be able write C# code in MemoEdit1 in order to manipulate the TextBox controls. F.e: at runtime user typed into MemoEdit1 simple code like: TextBox2.Text = "Hello" + TextBox1.Text;
So, when I click on Button1, I need to compile and execute the code.
Question may sound so simple as I am a newbie in compiling/executing code during runtime in C#.
Could you pls, help?
Thanks.
Take a look on this snippet
public class Evaluator
{
public void Eval(string Code)
{
Microsoft.CSharp.CSharpCodeProvider Provider = new Microsoft.CSharp.CSharpCodeProvider(); // Create an provider
System.CodeDom.Compiler.ICodeCompiler Compiler = Provider.CreateCompiler(); // Create An Compiler
System.CodeDom.Compiler.CompilerParameters Parameters = new System.CodeDom.Compiler.CompilerParameters(); // Create a parameters of the compiler
Parameters.GenerateInMemory = true; // It should generate the compiled assembly in the memory
System.CodeDom.Compiler.CompilerResults Results = Compiler.CompileAssemblyFromSource(Parameters, Code); //Compile it
///Now you just need to use reflection to call its methods
object SomeClass = Results.CompiledAssembly.CreateInstance("ClassName"); //Name of the class you want to create an instance
var Method = SomeClass.GetType().GetMethod("MethodName"); //Name of the Method you want to call
Method.Invoke(SomeClass, null); // change null for the argument it needs
}
}
if you want to just write code you will have to add the an class and a Method to wrap the user code and then you call it through the Invoke, you will probably have to reference your own assembly into this assembly

Categories