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!
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:
Download Visual Studio 2013 Express for Windows Desktop (or trial version of Ultimate or whatever you feel is appropriate for you)
Open Visual Studio as an Administrator
Create a new Project. Select a "Class Library" under the "Visual Basic" templates
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
Click on Project -> ClassLibrary1 Properties
Click on "Compile" and check the box for "Register for COM interop"
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
Related
I'm trying to make a little test app to compress and extract folders, and here is my code :
Imports System.IO
Imports System.IO.Compression
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FolderBrowserDialog1.ShowDialog()
If Not FolderBrowserDialog1.SelectedPath = "" Then
Dim startPath As String = FolderBrowserDialog1.SelectedPath
Dim zipPath As String = "C:\Users\LENOVO\Desktop\result.zip"
ZipFile.CreateFromDirectory(startPath, zipPath)
End If
End Sub
End Class
And the error is in the ZipFile command :
ZipFile is not declared. it may be inaccessible due to its protection level.
I'v searched a lot, and my target framework is 4.5.2 so Zip file should be supported, and i'm using visual studio 2017.
Any help will be appreciated.
I solved the problem with adding a reference, just go to Project tab --> Add Reference --> Assemblies tab and then select System.IO.Compression.FileSystem and add it, and it should work, here is some images too :
Hope someone will find this useful.
This may not affect everyone, but this is what I had to do to solve this issue (leaving this here as a self-note as much as anything).
I ended up having to find System.IO.Compression.dll, System.IO.Compression.Filesystem.DLL, and System.IO.Compression.ZipFile.dll on my computer and move them to the BIN folder of the site I'm working on. As soon as I did so, any "not declared" errors disappeared.
Similar problem as the initial question. I followed the other solutions/comments above and ended up with having to do the following which has slight variations to the details:
Add System.IO.Compression.FileSystem to the Assembly references
Add both the below imports code lines to the beginning of the code:
Imports System.IO '(Needed for additional file system functions used)
Imports System.IO.Compression.ZipFile
Calling function without preceeding "ZipFile.":
CreateFromDirectory(FileLocation1 & "\", FileLocation1 & ".zip")
I hope this may support others facing the same issue.
BR
Fnk
I am trying to run a VBScript that opens powerpoint and runs a macro named UpdateandBreaklinks. The Script that I am running is as follows:
Dim oApp
Dim oPres
Dim oSlide
Set oApp = CreateObject("Powerpoint.Application")
oApp.visible = true
Set oPres = oApp.Presentations.Open("C:\Users\PPwithLinks.pptm")
Set oSlide = oPres.Slides(1).Duplicate
oApp.Run "PPwithLinks.pptm!UpdateandBreaklinks"
This Gets the script to Run and open Powerpoint, however the Macro is not Ran. I get an error on this line:
oApp.Run "PPwithLinks.pptm!UpdateandBreaklinks"
I have tried to adjust it to this:
oApp.Run "UpdateandBreaklinks"
But I still get the same error. The error reads as:
Application.Run: Invalid request. Sub or funtion not defined.
Code: 80048240
I have researched the problem and all I can find is that people have remedied this same issue with the code above. I am not sure what I am doing incorrectly.
I have further tested the problem and I am now at this point:
So, I created a new pptm, I created a new macro for MsgBox "Hello" within the pptm. When I call the pptm to be opened with VBS, the pptm opens and I still get the error.
BUT, testing further, if I have opened the pptm first and then call the VBS the macro does run.(It opens another instance of the program as a [Read-Only] and runs the macro.)
Also, if I just open the pptm from the VBS(delete the line that runs the macro) and then try to run the Macro manually within PowerPoint then I get the following error: "Because of your security settings, macros have been disabled."
I go into the Macro Security settings and they have not been changed, so it appears to me that when VBS Opens the pptm something is disabling the Macros and therefore erroring out the VBS code to not call and run the macro.
The script work on the local computer but not remotely. Any idea?
I'm getting VBScript runtime error "ActiveX component can't create object: XStandard.MD5".
strSource = "C:\WINNT"
Set objFSO = CreateObject(Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strSource)
Set colfiles = objFolder.Files
For each file in colfiles
Set objMD5 = CreateObject("XStandard.MD5")
Go to the DLL folder and use in command prompt > regsrv32 XMD35.dll
http://www.xstandard.com/en/documentation/xmd5/
When you want to instruct a remote computer to instantiate a given ActiveX object, you need to specify the name of the remote server when calling the CreateObject(progID, [serverName]) function.
Otherwise, the script engine attempts to instantiate the object from the local machine.
For example:
Set objMD5 = CreateObject("XStandard.MD5", nameOfYourServer)
And of course, the component must be available on the server.
More info about CreateObject at MSDN
I don't know if it has anything to do with the syntax of line 2. you are missing an open quotation. You have:
Set objFSO = CreateObject(Scripting.FileSystemObject")
You should have:
Set objFSO = CreateObject("Scripting.FileSystemObject")
I realize this is an old question, but google sent me here with the same problem. In my case it was a 32-bit vs 64-bit issue - I needed to use the 32-bit version of CScript to execute.
More details here http://blogs.msdn.com/b/helloworld/archive/2007/12/12/activex-component-can-t-create-object-when-creating-a-32-com-object-in-a-64-bit-machine.aspx
The XStandard.MD5 is a component that is NOT standard in all computers. it is something that needs to be installed. However even after installing you may still run into the same error. This may be because you are running the script in a 64bit system. for some reason the 64bit wscript won't use the xmd5.dll. however you can make a workaround by associating the scriptfiles to run on the 32bit version of wscript. by
going into your default programs. (Windows 8+ Right click the startbutton area [category view] Click Programs, then Default Programs then Associate a file type or protocol with a program) (Windows 7- click on start then click default programs)
Then click vbs then click change program, change it to
C:\Windows\SysWow64\Wscript.exe
NOTE ONLY ON 64BIT
As dmogle says in his comment, you need to make sure that the component is installed on the remote computer. Assuming that you've got access to that computer, download the dll to it and then run the command regsvr32 XMD5.dll as can be seen here.
On another note, your script looks a little funny, the twp last lines are:
For each file in colfiles
Set objMD5 = CreateObject("XStandard.MD5")
Which means that you'll be creating an instance of the MD5 object for each file. I've never used the component in question, but I'm fairly sure that you probably want to create only one instance of the object and then call the functions on that object multiple times. So I'd suggest changing the order of those lines to:
Set objMD5 = CreateObject("XStandard.MD5")
For each file in colfiles
I have an application written in VB.Net with Visual Studio 2005. The application allows the user to create and save project files. When I distribute the application, I include some demo project files, which I install in the common application data folder.
XP - C:\Documents and Settings\All Users\Application Data
Vista & 7 - C:\Program Data
I have discovered an unexpected behavior -- if any file in the common application data folder is removed, and the application is run from the start menu, then the install procedure will start and attempt to restore the missing file(s). If the MSI file no longer exists at its original location or has been changed, then the application will fail to run. I perceive that this is a "feature", but it is one I don't want. Can anyone tell me what is going on and how I can avoid it?
Some more details:
I created the setup package by using a Visual Studio deployment
project.
This behavior will not occur if I launch the EXE directly. I
expect, therefore, that the behavior has something to do with the
start menu shortcut. I've noticed that the shortcut isn't a normal
shortcut -- it doesn't have a "Target Location".
All advice is appreciated.
-TC
I have learned that this behavior involves something called "Install-on-Demand" (aka "Self Heal"). The unusual shortcuts created by the setup package are called "Advertised Shortcuts". Now that I have a name for the problem, it is easy to find information on how to fix it. Notably:
http://msdn.microsoft.com/en-us/library/aa368297.aspx
http://groups.google.com/group/microsoft.public.dotnet.distributed_apps/browse_thread/thread/401847045f104af3
http://blog.jtbworld.com/2007/11/enable-target-and-change-icon-of.html
Those pages contain a wealth of information. For the convenience of others who may stumble upon this post, I will summarize what they say:
Advertised shortcuts are special shortcuts which do some fancy things. Most notably, they reinstall damaged application before launching their target. There is some debate over whether they are good, evil, or harmless. In my opinion, they do something most users don't expect, and that makes them evil. Therefore, I'd like to disable them for my application.
Visual Studio setup projects automatically create MSI packages which generate advertised shortcuts by default. It is easy to override that default when installing the MSI package by using DISABLEADVTSHORTCUTS=1 as a command-line argument for Setup.exe. Also, with a utility like Orca, you can manually change the default by inserting DISABLEADVTSHORTCUTS=1 as a property of the MSI. However, if you want Visual Studio to automatically create MSI packages which don't create advertised shortcuts, that is harder. I did it this way:
First, I created a VBS file using the DisableAdvt code provided by Gary Chang in one of the links above (I've repeated that code below). Just create a text file, paste in the code. and save it as DisableAdvt.vbs.
Then, create a post-build event for your setup project. The exact syntax will depend on your file locations. Because my DisableAdvt.vbs is in a "Tools" subfolder of the solution folder, my post-build event looks like this:
"$(ProjectDir)..\Tools\DisableAdvt\DisableAdvt.vbs" "$(BuiltOuputPath)"
That's all I had to do. It works like a charm.
-TC
Some notes:
In Visual Studio 2005, Build events are accessed differently for setup projects than they are for other types of projects. Click on the project name in the solution explorer, then look for PostBuildEvent in the Properties pane.
Orca is a utility that can be used to manually insert the DISABLEADVTSHORTCUTS property into the MSI file. With my approach, Orca is not necessary. However, it is useful for verifying that the build event is making the expected change.
http://www.technipages.com/download-orca-msi-editor.html
In the build event, the misspelling "BuiltOuputPath" is intentional.
Here is Gary Chang's DisableAdvt.vbs code (note that I fixed a typo on line 21 -- Very important!):
Option Explicit
Const msiOpenDatabaseModeTransact = 1
Dim argNum, argCount:argCount = Wscript.Arguments.Count
Dim openMode : openMode = msiOpenDatabaseModeTransact
' Connect to Windows installer object
On Error Resume Next
Dim installer : Set installer = Nothing
Set installer = Wscript.CreateObject("WindowsInstaller.Installer") :
CheckError
' Open database
Dim databasePath:databasePath = Wscript.Arguments(0)
Dim database : Set database = installer.OpenDatabase(databasePath, openMode) : CheckError
' Process SQL statements
Dim query, view, record, message, rowData, columnCount, delim, column
query = "INSERT INTO Property(Property, Value) VALUES ('DISABLEADVTSHORTCUTS', '1')"
Set view = database.OpenView(query) : CheckError
view.Execute : CheckError
database.Commit
If Not IsEmpty(message) Then Wscript.Echo message
Wscript.Quit 0
Sub CheckError
Dim message, errRec
If Err = 0 Then Exit Sub
message = Err.Source & " " & Hex(Err) & ": " & Err.Description
If Not installer Is Nothing Then
Set errRec = installer.LastErrorRecord
If Not errRec Is Nothing Then message = message & vbLf & errRec.FormatText
End If
Fail message
End Sub
Sub Fail(message)
Wscript.Echo message
Wscript.Quit 2
End Sub
Is there any quick way to run a file(.cs) in VS 2008 with a Main method ?
Often you'd want to test some mockup code, Going Alt+f7(Project->ProjectName Properties) and changing the Startup object from a dropdown list is quite cumbersome.
Get yourself the SnippetCompiler, it's made to run snippets (not inside of VS, but close enough) and may help you.
What about instead of mockups, writing those as unit tests. You can run those quickly without changing entry points. And the tests could stick around for later changes. Instead of writing to the Console, you would use Asserts and Trace Writes.
To compile one file C# programs I have created a .bat file, on which I drag and drop a .cs file and get a .exe in .cs file directory.
SET PATH=%PATH%;C:\WINDOWS\Microsoft.NET\Framework\v3.5
cd %~d1\
cd "%~p1"
csc %1
You can use this .bat file in a Visual Studio macro to compile active .cs file and run the application.
Sub RunCS()
If Not ActiveDocument.FullName.EndsWith(".cs") Then
Return
End If
REM Path to batch file
Dim compileScript = "C:\dev\compileCS.bat"
Dim compileParams As System.Diagnostics.ProcessStartInfo
compileParams = New ProcessStartInfo(compileScript, Chr(34) & ActiveDocument.FullName & Chr(34))
Dim compiling As System.Diagnostics.Process
compiling = System.Diagnostics.Process.Start(compileParams)
compiling.WaitForExit()
Dim programFile As String
programFile = ActiveDocument.FullName.Substring(0, ActiveDocument.FullName.Length - 3) + ".exe"
Dim running As System.Diagnostics.Process
running = System.Diagnostics.Process.Start(programFile)
End Sub
This will run only programs for which all code is in one file. If you want to quickly change projects instead, you can change your solution's Startup Project to Current selection
I keep a sandbox solution around that has a console project and other tiny project types that I use frequently. Snippet Tools are nice but usually don't come with the whole Visual Studio shebang like debugging etc.
Snippy, originally by Jon Skeet (who lurks on here I believe) and further developed by Jason Haley.