[ZZ]Debug VBScript with Visual Studio

來源:互聯網
上載者:User

 

http://blog.theopensourceu.com/2009/05/debug-vbscript-with-visual-studio/

 

For a project that I work on at at my primary employer, we use VBScript inside
of DTS
packages. Until this project, I’ve not had too much experience with VBScript. What
I’ve found most frustrating about the language is the inability to step though
the code… until a recent discovery. I’ve found that if I save the target code to
my local machine as a VBS file and execute it with WScript.exe, I can attach a
debugger to it.

I’ve discovered this by poking around and I don’t know too much about
WScript.exe. I’m sure if you Google it, you can find more about it.

For this to work, you’ll need a copy of Visual
Studio installed. I am not sure if any of the express editions will work.
Drop a line in the comments if you find it will. As a quick walk though:

  1. Save your code as a VBS file. Don’t forget to call Main (or your main)
    function as wscript won’t automatically call main.
  2. Open a command prompt.
  3. Navigate to the location of the file.
  4. Type: WScript.exe FileName.vbs //D //X
  5. You should be prompted to select a debugger, select a new instance of either
    Visual
    Studio 2003, 2005 or 2008.
  6. Visual
    studio will open up and break at the very first line.
  7. Step though the code.

For an example, save the following as “Example.vbs” on your desktop.

 

MsgBox "Starting Script!"Dim i : i = 0Const C_Max = 100Dim sPrimeNumberList : sPrimeNumberList = "Prime Numbers: " & vbCrLfFor i = 0 to C_MaxIf IsNumberPrimeNumber(i) Then     sPrimeNumberList = sPrimeNumberList & cStr(i) & ", "EndIfNextMsgBox sPrimeNumberListMsgBox "Script Completed."PublicFunction IsNumberPrimeNumber(ByVal iNumber)Dim bIsPrime : bIsPrime = TrueFor j = 2 To iNumber\2  bIsPrime = iNumber Mod j > 0IfNot bIsPrime ThenExitForNextIsNumberPrimeNumber = bIsPrimeEndFunction

 

 

Open a command prompt and navigate to your desktop:

Command Prompt for DebuggingVBScript from Visual
Studio

Once there, type in the following:

 

1  wscript Example.vbs //D //X

 

You should then be presented with a dialog similar to the following:

Visual
StudioDebuggingVBScript

Once you make a selection, you should get something like the following:

DebuggingVBScript from Visual
Studio

From here, I’ll assume you are familiar enough with Visual
Studio to play with the debugger and learn how it works. When debuggingVBScript, I’ve
found that you don’t have all the options that you might be used to such as Step
In To, but for me simply having this much is a life saver.

To get around the step-in-to thing, I simply use Run
to Cursor
. It works like a charm.

Update: I just found that (at least on this computer) and in
Visual
Studio 2003, I have all my standard code-stepping buttons available. I’m not
sure why on my work computer I only have Step-Over but either way, this is still
a life saver.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.