Feed
Google+
Contact
XING
LinkedIn
Twitter
ICQ
meebo.com

To NET object to VBScript.NET

by Roland Schumacher alias GENiALi 14. Mai 2010 -- 884 words  -- 1 mal gelesen
From starting position

At the moment I'm working on a large project where we for a VB6 application.NET (c#) to migrate. On the whole, the logic is migrated easily, without adding great new features.

In the old application, a lot of logic in VBScripte has been paged out for calculation purposes. You made the order to be able to customize the logic without the new DLLs need to be created. I must now copy this in c#. The VBScripte do not migrate yet. Simply create the effort.

I need a.NET pass object to a VBScript, this makes the calculations and then returns the object back to c#.

At first, I thought you could just call WScript.exe with a few parameters. However, I can pass no objects as natural. After some search, then fündig was I like you a.NET can pass object to a VBScript and get back. The key word is: Microsoft Script control 1.0

What must be done now everything to solve the problems.

Create a COM visible.NET object

To a.NET to edit object in a COM environment must be made the object for COM visible. This can be done in the project setting of the classes.

COM Visible einstellen

COM Visible registrieren

With these settings, the object is registered automatically by Visual Studio com. On a production system that would need to take course setup or a batch. With regasm MyAssembly.dll , the DLL on the command line is registered, or from a batch out.

The class itself can be very easy.

using system;
using System.Collections.generic;
using System.LINQ;
using System.text;

namespace ComVisibleClass
{
	public class person
	{
		private string m_Name;
		}}private string m_Vorname;
		private int m_Alter;
		public string name
		{
			get {return m_Name;}
			}{set {m_Name = value;}
		}
		public string first name
		{
			get {return m_Vorname;}
			}{set {m_Vorname = value;}
		}
		public int age
		{
			get {return m_Alter;}
			}{{{set {m_Alter = value;}
		}
	}
}
Calling of VBScriptes

For testing I created a very simple VBScript. It is a.NET object passed and there are a few messages to the object of a MsgBox. Thus it is clear that value actually manipulated in the VBScript and also back to the.Are NET.

But still a DLL must be incorporated to run a VBScript.

Microsoft Scripting Control 1.0

Add reference to enters the COM register and is looking for the Microsoft Scripting control 1.0. Then, you can access the control in code. Simple yet the using add as in row 6 in the following code to see.

using system;
using System.IO;
using System.Runtime.InteropServices;
using System.text;
using ComVisibleClass;
using MSScriptControl;

namespace TestApp
{
	class program
	{
		main static void
		{
			StreamReader streamReader = new StreamReader ("Script.vbs", Encoding.Default);
			}}}string script = streamReader.ReadToEnd();
			streamReader.Dispose();

			RunScript(script);

			{Console.ReadLine();
		}

		private static void RunScript (string scriptCode)
		{
			try
			{
				ScriptControlClass script = new ScriptControlClass();
				}}script.Language = "VBScript";
				script.Reset();
				script.AllowUI = true;
				script.UseSafeSubset = false;

				script.AddCode(scriptCode);

				Person person = new person() {name = "Schumacher", = first name "Roland", age = 35};
				Console.WriteLine(person.)(Age);

				script.AddObject ("Person", person, true);

				Object [] objects = new object [] {};

				script.Run("CalculateStatus",_ref_objects);

				Console.WriteLine(person.){(Age);
			}
			catch (ex COMException)
			{
				Console.WriteLine(ex.)}{ToString());
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.)}ToString());
			}
		}
	}
}

In lines 14-16, located in the same directory as the test application is loaded a VBScript. The script is still discussed.

The execution of VBScriptes takes place in line 27 to 42. First, a new instance of the ScriptControlClass is created and set a few parameters. Script with .AddCode passing the code there to perform the control. Respect. If the VBScript has no sub or function the script runs in the line of 38. But as soon as the VBScript contains a function or sub still nothing happens in this line.

I create the object I want to pass to the VBScript in line 35. Note the age. This is in the initialize set to 35 . To control, I give out the on line 36.

On line 38, I pass the object to the VBScript. Here, it is also interesting. In the first parameter, I specify a name for the object. Exactly with this name it accesses to the VBScript on the person. The function CalculateStatus() is called finally in line 42.

In row 44, I give out age again. And behold, it is no longer 35 but 99. The old 99 was set in the VBScript. So, the exchange of information took place. I can a.NET object in VBScript to manipulate and the results are in.NET available.

The VBScript

The VBScript makes not much special.

Public Sub CalculateStatus()

    oPerson Dim
    set oPerson = person

    if err.number < > 0 then
        MsgBox "UPS: error" & Err.number & "creating customer:" & Err.description
    else
        MsgBox "customer name:" & oPerson.Name & "-first name:" & oPerson.Vorname & "- age:" & oPerson.Alter
        
        oPerson.Alter 99 =
        
        MsgBox "customer name:" & oPerson.Name & "-first name:" & oPerson.Vorname & "- age:" & oPerson.Alter
    end if
End Sub

The Sub CalculateStatus is defined in row 1. In line 3, I define the oPerson object and put it in the line of 4. And here you can see the person again. This is exactly the person you with script.AddObject ("Person", person, true);  has passed.

The original person is visualized shortly in line 9. Line 11 is placed the age of 99 and on line 13, the person is visualized again. Then, the VBScript is ready.

In the.NET is now 99 the age of the individual.

Result

Conclusion

What me at the beginning as unsolvable with.NET appeared is the very simple to solve. Thanks to the Internet and Google, you can find any when it out.

Now we need to solve a problem only. The VBScripte must be started in the debugger. To do this there would be yes the keyword stop in the VB. However this does not work. Here I must be looking what will be also possible for a solution.

What would be still cool

Something should be there with c# as a script.

kick it on dotnet-kicks.de

Tags:

Kommentare sind geschlossen

Statistik

Gesamte Posts: 1025
Dieses Jahr: 7
Diesen Monat: 1
Diese Woche: 0
Kommentare: 929
Total Bewertungen: 91
Ø Bewertung: 4,42
Meisten Kommentare
Google+ ... (28)
Beste Ø Bewertung
Tastaturlayout von Visual Stud ... (5)
Meiste Bewertungen
Windows 7 und die ersten Erfah ... (5)

Buttons

Stop Spam Harvesters, Join Project Honey Pot Benutzerprofil von GENiALi connection speed test network connection Bloggeramt.de Slug.ch Blog Verzeichnis und Blog Webkatalog Schumacher Roland Seitwert Valid XHTML 1.0 Transitional

writing