0

I would like to extend the functionality of vbscript with the code I have written in c#. I have written some classes to automate the SAP GUI and would like to use these classes in all the vbscript files I have.

I have hundreds of vbscript files and know it will take years to convert all them to C#. So I think it will be faster to expose my c# classes to vbscript.

Do you know how to do this or know any references online I can study?

1
  • Use the [ComVisible] attribute to expose C# code to vbscript. Commented Jan 12, 2013 at 15:43

2 Answers 2

1

I don't know whether you're running your VBScript from the command-line or from within something like Office.

If the former, you could create one or more command-line apps that you can call from any scripting language and into which you pass parameters & action specifiers just like any other command-line tool. (Also consider moving to PowerShell in this case - it exponentially better than VBScript for command-line scripting & has great integration with .NET).

If the latter, you'll likely need to register your C# classes using RegAsm and then create instances of your C# types as per any other COM type. See this post for more details: How do I call .NET code (C#/vb.net) from vbScript?

Sign up to request clarification or add additional context in comments.

Comments

1

VB script runs on the client inside the browser run-time.

The only C# solution I am aware of to run inside the browser, is silverlight. It is still just c# though.

You can access c# code from scripting languages like VB- of java-script, by decorating them with the [ScriptableMember] attribute, like so:

/// <summary> /// Members that can be called from javascript. (or vbscript) /// </summary> public sealed class LINEARVIEWER_SL_SCRIPTS { [ScriptableMember] public void ChangeNetwork(string pNetworkFilterId, string pNetworkFilter) { MainViewModel MainVM = (MainViewModel)((MainPage)Application.Current.RootVisual).DataContext; long SectionID; if (long.TryParse(pNetworkFilterId, out SectionID) == false) { throw new FormatException("'" + pNetworkFilterId + "' not a valid section / network ID."); } MainVM.RoadFilterViewModel.SelectSectionAsync(SectionID, /* completed handler = */ null); } } 

You have to register these classes when the silverligh (c#) application starts up, like so:

 private void Application_Startup(object sender, StartupEventArgs e) { ... HtmlPage.RegisterScriptableObject("LINEARVIEWER_SL_SCRIPTS", new LINEARVIEWER_SL_SCRIPTS()); } 

From the java (or vb) script, you can then simply call those methods like so:

 function DoAddToLIV(pNetworkFilterId, pNetworkFilter) { ... gObjLIV.Content.LINEARVIEWER_SL_SCRIPTS.ChangeNetwork(pNetworkFilterId, pNetworkFilter); ... } 

where gObjLIB.Content is the id of the silverlight object inside the html page.

 var gObjLIV = null; function onSilverlightPluginLoaded(sender, args) { gObjLIV = sender.getHost(); } 

You can hook that function to the silverlight object in the html of ASPX page, using this parameter:

<param name="onLoad" value="onSilverlightPluginLoaded" /> 

Let me know if I missed anything or if you need more examples. I don't mind.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.