Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Interop: Using .NET from VBScript

 
Logged in as: Guest
arrSession:exec spGetSession 2,7,26652
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [General Forum] >> Other Programming/Scripting Languages >> Interop: Using .NET from VBScript
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Interop: Using .NET from VBScript - 10/3/2005 12:28:11 AM   
  ehvbs

 

Posts: 2077
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
I knew that at least some parts of .NET can be used from VBScript, but I thought it
would be very complicated, so I postponed research/work on this topic to the future.
But today a link from Michael Harris sent me to
http://blog.opennetcf.org/afeinman/PermaLink,guid,aa53e23d-b8e5-4015-b00a-0c8ea9bc6dfe.aspx
where I saw that using .NET components could be as easy as:

      
Therefore I would like to ask
  • Do you use .NET from VBScript?
  • Where can I get more information?
 
 
Post #: 1
 
 RE: Interop: Using .NET from VBScript - 10/3/2005 1:49:10 AM   
  Snipah


Posts: 1343
Score: 6
Joined: 11/1/2004
From: Netherlands
Status: offline
EHvbs:

I found the following article on DevX (click to read the full article): VBScript: Microsoft's Orpaned Language
quote:


"Microsoft is fully committed to evolving implementations of both JScript and VBScript to a level where each language is functionally equivalent. So don't feel that you need to commit to one just to ensure that you are investing in a language that has a future."

If that statement is true, Microsoft is keeping the future of VBScript well-hidden. The most telling stroke against VBScript is that with two cumulative releases of the .NET framework and Visual Studio now behind us, there's still no sign of a VBScript.NET. There's a JScript.NET—has been since the early beta releases—but no VBScript.NET.

Does VBScript Have a Future?
I once believed that VBScript would rapidly become the script lingua franca of the Internet. After all, VBA, another cut-down version of Visual Basic, found its way not only into the Microsoft Office suite as the primary macro language, but also into a host of other commercial products. Because VBA and VBScript are fundamentally similar and therefore attractive to the same large universe of developers, one might rationally assume that VBScript would make an equally successful transition into Web products as VBA did in desktop products. But that didn't happen. If functional equivalence with JScript were a goal, Microsoft could have created a VBScript.NET. At one point, they had a third-party company create a proof-of-concept VBScript.NET, according to David Simmons of SmallScript Corp.—but Microsoft has apparently made a unilateral decision not to bring the language along into the .NET family. Unless rectified quickly, that omission represents a hard stop for VBScript, whereas VBA's future looks a bit more stable for the short term, if not for the long term.
From all appearances, VBScript is on a path to extinction. And with all due respect to VBScript, that's probably a good thing. In spite of the broad industry support for JavaScript, Microsoft probably could have garnered equally wide support for VBScript if it had ported the language to other platforms early in its lifecycle. However, as that didn't happen, there's little reason now for Microsoft (or its customers) to maintain the fiction that VBScript has any serious future as a Web language. I interpret VBScript's absence from .NET as Microsoft's tacit agreement that VBScript has no future. In my opinion, the lack of client-side support and the absence of VBScript.NET should serve as warning signs to developers or organizations using or planning to use VBScript.
Of course, VBScript aficionados are perfectly free to ignore these warning signs and continue to use this familiar language; but unless Microsoft has some sudden radical VBScript epiphany, those who do should be prepared for heartache and frustration down the road. By the way, if you want to buy the domain name VBScript.NET, it's for sale.




//edit: MSDN Blogs says the following:

quote:


 Back then I spent a lot of time working with the VB .NET language designers to see if these ideas from VBScript would work in VB .NET, thereby giving VB .NET a better backwards compatibility story with VBScript.



Regrettably, by and large, they don't.  (I may go into details in future blogs as to some of the crazy ideas we had for making VBScript work in the .NET world.) Based on that experience, I absolutely 100% disagree that fracturing VB into a FOURTH variant -- VB6, VBScript, VB.NET and the proposed VBScript.NET -- is a good way to achieve the laudable goal of lessening the VB learning curve.  We can improve VB without fracturing it into yet another variant. 


< Message edited by Snipah -- 10/3/2005 1:59:14 AM >


_____________________________

For more information, please see the "Read me First" topic.

http://www.visualbasicscript.com

(in reply to ehvbs)
 
 
Post #: 2
 
 RE: Interop: Using .NET from VBScript - 10/3/2005 4:14:58 AM   
  ehvbs

 

Posts: 2077
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Snipah,

thanks for your quick and inspiring response. But I'm more interested in technical
(not 'political') information. Some (a lot of) my tasks/problems are to be solved by
using VBScript - with no regards to Microsoft's decisions about the future of VBScript.

But if I want to solve my problems in a good (if not the best) way, I have to keep up
with new technologies like .NET. If you care to read my posting to the "sort list" topic,
I think you will agree, that "using .NET components from VBScript" deserves further
studies.

And that is my problem at hand: How do I get reliable information / sample code?
Has anybody out there some experience / links / tutorials /articles to share?

(in reply to Snipah)
 
 
Post #: 3
 
 RE: Interop: Using .NET from VBScript - 10/3/2005 7:46:16 AM   
  Snipah


Posts: 1343
Score: 6
Joined: 11/1/2004
From: Netherlands
Status: offline
I totally agree with you on this subject, thus i did some dig work for future readers...


External Link: www [dot] pinvoke [dot] net - Do Interop the Wiki way (a new page about .NET, wikipedia style)

quote:

ORIGINAL Marvels of COM .NET interop @ Compact Framework Musings. Alex Feinman's web log
[...]
From the System.Collections namespace we have:
  • Queue
  • Stack
  • ArrayList
  • SortedList
  • Hashtable

I'd say it makes a rather nice addition to the existing VB script (and javascript as well) toolset. But wait, there is more...
System.IO namespace is represented by two useful classes:
  • StringWriter
  • MemoryStream

Finally, there are System.Text.StringBuilder and System.Random classes.
Why would I be interested in StringWriter (or StringBuilder)? Because frankly, VBScript memory allocator sucks. Anyone who ever tried to build a large string concatenating small ones knows that after 65536 characters performance drops drastically. Concatenating first 65536 characters one by one takes about 1.5 sec on my 3.5 GHz machine. Appending the next 65536 takes about 4 sec. The following 65536 characters take 33 sec. And you don't want to know what happens after that. Well, in case you do, appending together the string representations of numbers 1 through 100000 takes over 2 minutes.
Of course I decided to check if I could do it faster with StringWriter. But there was a catch. If you check the lsit of methods of the StringWriter class in the Visual Studio object browser, or rather the TextWriter class, from which StringWriter derives all of the Write methods, you would notice that Write() has 17 overloads, and WriteLine() has 18. Obviously you would want to use a particlular overload. How do you choose one. As it happens, the .NET framework deals with this problem in a straightforward, if inelegant way. If you have 18 overloaded methods called Write, they are exposed as Write, Write_2, Write_3 ... Write_18. How do you find out which one is which? If you look into the class browser (or Ildasm), the order of the overloads is reverse to what you see. E.g. for StringWriter we have:
  • System.IO.TextWriter.Write(string, params object[]) // Invoke as Write_17
  • System.IO.TextWriter.Write(string, object, obejct, object) // Invoke as Write_16
  • System.IO.TextWriter.Write(string, object,object) // Invoke as Write_15
  • System.IO.TextWriter.Write(string, object) // Invoke as Write_14
  • System.IO.TextWriter.Write(object) // Invoke as Write_13
  • System.IO.TextWriter.Write(string) // Invoke as Write_12
  • System.IO.TextWriter.Write(decimal) // Invoke as Write_11
  • System.IO.TextWriter.Write(double) // Invoke as Write_10
  • System.IO.TextWriter.Write(float) // Invoke as Write_9
  • System.IO.TextWriter.Write(ulong) // Invoke as Write_8
  • System.IO.TextWriter.Write(long) // Invoke as Write_7
  • System.IO.TextWriter.Write(uint) // Invoke as Write_6
  • System.IO.TextWriter.Write(int) // Invoke as Write_5
  • System.IO.TextWriter.Write(bool) // Invoke as Write_4
  • System.IO.TextWriter.Write(char[], int, int) // Invoke as Write_3
  • System.IO.TextWriter.Write(char[]) // Invoke as Write_2
  • System.IO.TextWriter.Write(char) // Invoke as Write

This means that if we want to write a string into a stringwriter, we would call:
s = “Bob“
Set wrt = CreateObject("System.IO.StringWriter")
wrt.Write_12 s

To test the performance of a StringWriter as compared to concatenating a VB string I ran the following script:
'stringtest.vbs
WScript.Echo Now
s = ""
for i = 1 to 100000
s = s & CStr(i)
next
WScript.Echo Now


set wrt = CreateObject("System.IO.StringWriter")
for i = 1 to 100000
s = CStr(i)
wrt.Write_12 s
next
s = wrt.GetStringBuilder().ToString()
WScript.Echo Now

And here are the results:
8/28/2005 1:02:14 AM
8/28/2005 1:04:07 AM
8/28/2005 1:04:08 AM
As you can see, concatenating a VB string took nearly 2 minutes, while .NET string was only a second.
But wait, there is more...
Let's take a look at the StringBuilder class. The most important feature of it (at least in the context of what we are doing here) is formatting. There are 5 AppendFormat methods:

AppendFormat
( System.IFormatProvider provider , System.String format , params object[] args )
AppendFormat ( System.String
format , params object[] args )
AppendFormat ( System.String
format , System.Object arg0 , System.Object arg1 , System.Object arg2 )
AppendFormat ( System.String format , System.Object arg0 , System.Object arg1 )
AppendFormat ( System.String
format , System.Object arg0 )

Let's see if we could easily use one of them:
'sbtest.vbs
set sb = CreateObject("System.Text.StringBuilder")
sb.AppendFormat_5 Nothing, "{0} is a {1} number" & vbCrLf, Array(1, "loneliest")
sb.AppendFormat_5 Nothing, "{0} is a {1} number" & vbCrLf, Array(2, "happiest")
WScript.Echo sb.ToString()

Prints:
1 is a loneliest number
2 is a happiest number


We have formatting! In VBScript.
[...]


_____________________________

For more information, please see the "Read me First" topic.

http://www.visualbasicscript.com

(in reply to ehvbs)
 
 
Post #: 4
 
 RE: Interop: Using .NET from VBScript - 12/28/2006 12:35:08 AM   
  TNO


Posts: 1064
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
I'd like to bump this topic to get some newer perspectives on this subject (Its been a year...). I've been searching for a way to access DirectX through scripting for months now without any good results. The closest I came was with DirectAnimation, but with IE7 this method has gone the way of thre Dinosaur (Microsoft chose to disable this component instead of patch it.). I was hoping a new method would be available through the .NET approach, but as we've seen thus far, documentation is hard to come by to say the least.

P.S. To comment on the StringBuilder topic (probably pointless to do so by now), manipulating arrays is MUCH faster than concatenating strings. With the introduction of the JS.Array object we could simply use Push() and then Join to make the final string. Its a linear growth, not exponential like normal concatenation.

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to Snipah)
 
 
Post #: 5
 
 RE: Interop: Using .NET from VBScript - 1/2/2007 4:03:36 AM   
  DiGiTAL.SkReAM


Posts: 1157
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
Personally, I do concatenation by using Dictionary objects.

      

I have found that this is, performance-wise, on-par with using pure arrays, with the additional bonus of not having to use any external code/libraries, etc.
But regardless of whether you use Dictionaries or Arrays, you will still see about a 250x improvement over straight string concatenation.



_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to TNO)
 
 
Post #: 6
 
 RE: Interop: Using .NET from VBScript - 1/4/2007 4:08:26 PM   
  TNO


Posts: 1064
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
Thats a little too risky of a  method IMO. The Dictionary Object is an associative array, and as such shouldn;t be treated in such a way since you can't be 100% certain that the order you add the items is the order they are actually in when you Join() it. The ECMA specifiation on Associative arrays is vague:

quote:

Get name of the next property of [the object] that doesn't have the DontEnum attribute. If there is no such property, go to [the end]


That means that the order in which properties (i.e. items) are traversed is implementation dependent. Thus far as I've seen, the current engine/dll appears to traverse items in the order in which they were inserted, but newer or different OSes could just as easily use a seemingly random order that would reflect their respective hashtable implementations. Unlikely I know, but the possibility is there

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 7
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [General Forum] >> Other Programming/Scripting Languages >> Interop: Using .NET from VBScript Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts