kampi
kampi

Reputation: 2484

How to call and execute VBScript commands and functions in an .exe?

This question may be a little stupid, but i'm curious if it is possible or not. I have a file named library.xxx(contains vbscript code), which contains predefined functions. And i have an other file test.vbs(also contains vbscript code, just here i use the functions, which are defined in library.xxx). In test.vbs the library is "included", which means, i can use the functions from library.xxx. For example, there is a function called ProgramFiles, and if i call it in test.vbs, i will receive the Program Files folder location.

The problem is, that library.xxx is visible in this way. There is an application called ScriptCryptor. With this application, i can open my library.xxx and make an .exe of it, which would be better for me, since it is not clear text.

My problem is now, how could i execute the command which are called in test.vbs? I think i should read line by line the test.vbs file, and process it somehow. But how? How do i know if the line i read is a function or just a variable? Or both? And how to process them?

Is there some way to do that?

Hopefully it is understandable what i want.

Thanks!

Upvotes: 0

Views: 3305

Answers (1)

Sean W.
Sean W.

Reputation: 863

By far the easiest way to accomplish this is to include "library.vbs" in to your "test.vbs" file.

For example:

Library.vbs:

Function ProgramFiles()

    ProgramFiles = "C:\Foo"

End Function

test.vbs:

sub includeFile (fSpec)
    dim fileSys, file, fileData
    set fileSys = createObject ("Scripting.FileSystemObject")
    set file = fileSys.openTextFile (fSpec)
    fileData = file.readAll ()
    file.close
    executeGlobal fileData
    set file = nothing
    set fileSys = nothing
end sub

includeFile "library3.vbs"

wscript.echo ProgramFiles

Your question seems to indicate that you may already be doing this so if you are then I apologize.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

If clear text truly is bothering you then from what I have seen there is no way to make the executable from ScryptCryptor to be made available to your vbscript.

Instead you could create a COM Library DLL to be used as an object in your test.vbs file.

The downside to this is that it will be necessary to learn a new language. Visual Studio Visual Basic certainly is different from Windows Shell Script Visual Basic but it would work for what you want.

Steps to take:

  1. Download Visual Studio 2013 Express for Windows Desktop (or trial version of Ultimate or whatever you feel is appropriate for you)
  2. Open Visual Studio as an Administrator
  3. Create a new Project. Select a "Class Library" under the "Visual Basic" templates
  4. Copy and paste the code below

    <ComClass(OurLibrary.ClassId, OurLibrary.InterfaceId, OurLibrary.EventsId)> Public Class OurLibrary

    Private userNameValue As String
    
    Public Const ClassId As String = "40491A82-D53A-46A6-B7E0-1CDF78A33AB6"
    Public Const InterfaceId As String = "B49C996C-B039-471D-BF17-0DDA5B3CF517"
    Public Const EventsId As String = "6245E3DD-DEB5-4B75-AC03-F4430BC18FDE"
    
    Public Sub New()
    
        MyBase.New()
    
    End Sub
    
    Public Sub mycopy(mySource As String, myDest As String)
    
        My.Computer.FileSystem.CopyFile(mySource, myDest, True)
    
    End Sub
    

    End Class

  5. Click on Project -> ClassLibrary1 Properties

  6. Click on "Compile" and check the box for "Register for COM interop"
  7. Click on Build -> Build Solution

You now have a library that your test.vbs can use:

Set myLib = CreateObject("ClassLibrary1.OurLibrary")

mySource = "C:\mytextfile1.txt"
myDest = "C:\mytextfile2.txt"

myLib.mycopy mySource, myDest

If your like me test.vbs needed to be called as C:\Windows\SysWOW64\cscript.exe test.vbs

For more information about creating COM classes in Visual Basic see here: Walkthrough: Creating COM Objects with Visual Basic

Upvotes: 1

Related Questions