如何在 .exe 中调用和执行 VBScript 命令和函数? [英] How to call and execute VBScript commands and functions in an .exe?

查看:20
本文介绍了如何在 .exe 中调用和执行 VBScript 命令和函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能有点愚蠢,但我很好奇是否可能.我有一个名为 library.xxx(contains vbscript code) 的文件,其中包含预定义的函数.我还有一个文件 test.vbs(也包含 vbscript 代码,只是在这里我使用了在 library.xxx 中定义的函数).在 test.vbs 中,库是包含的",这意味着,我可以使用 library.xxx 中的函数.例如,有一个名为 ProgramFiles 的函数,如果我在 test.vbs 中调用它,我将收到 Program Files 文件夹位置.

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.

问题是,library.xxx 以这种方式可见.有一个名为 ScriptCryptor 的应用程序.有了这个应用程序,我可以打开我的 library.xxx 并制作它的 .exe,这对我来说会更好,因为它不是明文.

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.

我现在的问题是,我如何执行在 test.vbs 中调用的命令?我想我应该逐行阅读 test.vbs 文件,并以某种方式处理它.但是如何?我怎么知道我读到的行是一个函数还是一个变量?或两者?以及如何处理它们?

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.

谢谢!

推荐答案

到目前为止,最简单的方法是将library.vbs"包含到test.vbs"文件中.

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

例如:

库.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.

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

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

如果明文确实困扰着您,那么根据我所看到的,无法使 ScryptCryptor 的可执行文件可用于您的 vbscript.

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.

相反,您可以创建一个 COM 库 DLL 用作 test.vbs 文件中的对象.

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

这样做的缺点是需要学习一门新语言.Visual Studio Visual Basic 肯定与 Windows Shell Script Visual Basic 不同,但它可以满足您的需求.

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.

采取的步骤:

  1. 下载适用于 Windows 桌面的 Visual Studio 2013 Express(或 Ultimate 的试用版或您认为适合您的任何版本)
  2. 以管理员身份打开 Visual Studio
  3. 创建一个新项目.在Visual Basic"模板下选择一个类库"
  4. 复制并粘贴下面的代码

  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

公共类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

结束类

点击项目 -> ClassLibrary1 属性

Click on Project -> ClassLibrary1 Properties

您现在有一个可供您的 test.vbs 使用的库:

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

如果你像我一样需要将 test.vbs 称为 C:\Windows\SysWOW64\cscript.exe test.vbs

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

有关在 Visual Basic 中创建 COM 类的更多信息,请参见此处:演练:创建使用 Visual Basic 的 COM 对象

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

这篇关于如何在 .exe 中调用和执行 VBScript 命令和函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆