plc.myforum.ro Forum Index plc.myforum.ro
PLCs users www.plc.myforum.ro!
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to call a .NET DLL from a VBScript

 
Post new topic   Reply to topic    plc.myforum.ro Forum Index -> Simatic Automation Systems
View previous topic :: View next topic  
Author Message
kosta
Member
Member


Joined: 09 Aug 2015
Posts: 161
Location: Salonic

PostPosted: 14/02/2017 20:44    Post subject: How to call a .NET DLL from a VBScript Reply with quote

How to call a .NET DLL from a VBScript
Raymund Macaalay,
Step 1: Create Your DLL. Start up Visual Studio you can develop either in C#, VB or any language you want, this sample will be C#. You need to create a Class Library Project.
Code:
using System;
using System.Runtime.InteropServices;
namespace MyDLL
{
 [ComVisible(true)]
 public class Operations
 {
 [ComVisible(true)]
 public string getValue1(string sParameter)
 {
 switch (sParameter)
 {
 case "a":
 return "A was chosen";

 case "b":
 return "B was chosen";

 case "c":
 return "C was chosen";

 default:
 return "Other";
 }
 }
 public string getValue2()
 {
 return "From VBS String Function";
 }
 }
}


n the code above, apply the ComVisibleAttribute Class controls accessibility of an individual managed type or member, or of all types within an assembly, to COM. You can apply this attribute to classes, structures, interfaces, delegates, enumerations, fields, methods, assemblies or properties. By default, it is set to true but in case you want to hide the individual type, you can just set it to false (I just showed it for reference purposes, as you notice the get<code>Value2 does not have that attribute, but still it is visible)

Build the project.

Now after creating this Class Library Project, you have to configure it so that when you compile it will register on the systems assembly. There are two ways of doing that; one is to a command prompt. If you have .NET Framework 2.0 installed, regasm would be in the following path at:
Code:
C:\windows\microsoft.net\Framework\v2.0.50727\regasm.exe


And use the following for registering and unregistering an assembly:
How to call a .NET DLL from a VBScript How to call a .NET DLL from a VBScript

The one on top registers it and the one at the bottom unregisters it. Now what does that the /codebase option stand for? You need to use the /codebase switch when you don’t have your assembly in GAC because that will add an absolute path for your assembly in registry so that COM client can find it.

Another option is Directly to Visual Studio, this is the easy way and it’s as easy as ticking a check box in the project properties.
How to call a .NET DLL from a VBScript How to call a .NET DLL from a VBScript

Now you have your DLL ready.

Step 2: Fabricate your VBScript and here is my sample:
Code:
dim myObj
Dim myClass
Set myObj = CreateObject("MyDLL.Operations")
MsgBox myObj.getValue1("a")
MsgBox myObj.getValue2()

Now run your VBScript and that’s it.

https://www.codeproject.com/articles/79314/how-to-call-a-net-dll-from-a-vbscript

example
In V13SP1 following code to set the text of RichTextBox (.NetControl)

Code:
SetPropertyByConstant AccessPath "RichTextBox_1" "Text" "This is the new text"
Back to top
View user's profile Send private message
pointgrey
Member
Member


Joined: 13 Jul 2014
Posts: 140

PostPosted: 14/02/2017 21:43    Post subject: Reply with quote

thank you. your post is really original and helpful.
Back to top
View user's profile Send private message
trak
Member junior
Member junior


Joined: 24 Apr 2015
Posts: 62

PostPosted: 19/02/2017 09:50    Post subject: Reply with quote

thanks kosta.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    plc.myforum.ro Forum Index -> Simatic Automation Systems All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Fa-ti si tu propriul forum GRATUIT, pe www.myforum.ro