1

I need to debug several VBS files that make up a HTA application.

I've searched the web and couldn't find the exact QA. This one about How to run multiple batch files with single External Tool in Visual Studio and How to debug IE9 HTA? which a silver bullet hasn't been found.

This is where I'm stuck, I debug main.vbs and it references a class cutils which is in the file utils.vbs. And when I step over this line to instantiate cutils I get an error:

Microsoft VBScript runtime error: Variable is undefined: 'cutil'

enter image description here

So I tried a few External Tool Arguments like comma separated files:

//nologo //X  "$(ItemPath), $(ItemDir)utils.vbs"

But that doesn't work. Is that possible, to specify multiple files?

Otherwise apart from copying all the classes into one file how can I debug the VBS files in Visual Studio? I've tried to attach VS debugger to mshta.exe however it does NOT get into debug mode, even with the standard debugger keyword doesn't halt code control:

function clickButton()
    debugger 

So I am after a way to debug multiple VBA files with cscript.exe or even better attach to a mshta process to step through the code with debugger attached.


Update 1:

Following this answer https://stackoverflow.com/a/316491/495455 I tried creating a wsf file with all the references and using cscript.exe to debug:

<job id="IncludeExample">
   <script language="VBScript" src="main.vbs"/>
   <script language="VBScript" src="utils.vbs"/>
</job>

This starts off on main.vbs however it still fails to find the cutils class in utils.vbs:

enter image description here


Update 2:

So I renamed the .hta file extension to .html and that allowed me to open IE's Developer tools. For some reason pressing F12 in a HTA application doesn't allow you to use the Dev Tools.

When I do this, debugging is painful, the debugger can seem to find utils.vbs however, when I call the activate function in main.vbs it says it can't be found. Or more specifically main is not defined.

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • In update 1, are you running the debugger using the `wsf` file? I.e. `cscript.exe //nologo "test.wsf"`. – user692942 Feb 21 '17 at 10:12
  • In update 1, I had the vbsDebugging.wsf file open and clicked Tools menu > cscript to debug it, VS then opened the "VBScript - script block" as per screenshot – Jeremy Thompson Feb 21 '17 at 10:13
  • 1
    Have you tried just running `cscript.exe //nologo "test.wsf"` from a command prompt outside of the debugger does it work or still error? – user692942 Feb 21 '17 at 10:15
  • Sorry not at work for another 12hrs to check right now. I will try since VBScript is an interpreted language, not a compiled one so to rule out an issue with the debugger. Any other ideas mate? – Jeremy Thompson Feb 21 '17 at 10:22
  • 1
    I'd make sure that you define the "includes" in order they are used, so for example at the moment you define `main.vbs` before `utils.vbs` but you try and call `cutil` class in `main.vbs`. Reorder `utils.vbs` so it is defined before `main.vbs` in your list of scripts and try again. I don't think the debugger is the problem, I'd wager that the script will still fail when called outside the debugger. – user692942 Feb 21 '17 at 10:32
  • Ahhh, of course, that may be what 'script block' alludes to. Will also check the hex code 0x800a01f4 in err.exe and see if the vbs files properties has that "Blocked" checkbox ticked – Jeremy Thompson Feb 21 '17 at 10:35
  • Just make sure `utils.vbs` doesn't reference something in `main.vbs` or you will end up with a circular dependency. – user692942 Feb 21 '17 at 10:37
  • 1
    @Lankymart can you put your comment as an answer *define the "includes" in order they are used in the WSF file* - this did the trick, thanks. – Jeremy Thompson Feb 21 '17 at 23:40

1 Answers1

2

As mentioned in the comments, the likely cause is the order of the scripts in the WSF file. Try arranging them so utils.vbs comes before main.vbs as it appears to have a dependency on the cutil class in utils.vbs.

<job id="IncludeExample">
   <script language="VBScript" src="utils.vbs"/>
   <script language="VBScript" src="main.vbs"/>
</job>
Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175