Is Run command in Windows executable file? - windows

Suddenly i got one thing in my mind that most of the win applications like windwos version(winver), calc and all are executable files which will be there in Windows or System32 folder.
Like that, Run Command which we are using Win+R shortcut are also executable file? is this available in anywhere in windows system folder as executable file ?
When i tried to click Open file location, it is opening Desktop. actually Where it is starting from when we click on shortcut ?

No, it's not an exe, it's a shell dialog window that you find in the dynamic link library C:\Windows\System32\shell32.dll
You can call it from VBScript like this:
dim objShell
set objShell = CreateObject("shell.application")
objShell.FileRun
From JScript like this:
var objShell = new ActiveXObject("shell.application");
objShell.FileRun();
From VB6 like this:
Private Sub fnShellFileRunVB()
Dim objShell As Shell
Set objShell = New Shell
objShell.FileRun
Set objShell = Nothing
End Sub
With modern VB.NET, this becomes:
Dim t2 As Type = Type.GetTypeFromProgID("Shell.Application")
Dim obj2 = Activator.CreateInstance(t2) ' dynamic
obj2.FileRun()
If option strict is "ON", then the way to go is this:
Dim t As Type = Type.GetTypeFromProgID("Shell.Application")
Dim obj As Object = Activator.CreateInstance(t)
t.InvokeMember("FileRun", System.Reflection.BindingFlags.InvokeMethod, Nothing, obj, Nothing)
C# Variant:
Type t = Type.GetTypeFromProgID("Shell.Application");
object obj = Activator.CreateInstance(t);
t.InvokeMember("FileRun", System.Reflection.BindingFlags.InvokeMethod, null, obj, null);
//If the C # 4.0, the Dynamic Lookup presence of, it can be:
Type t2 = Type.GetTypeFromProgID("Shell.Application");
dynamic obj2 = Activator.CreateInstance(t2);
obj2.FileRun();
But you can also call it from a batch-file, if you want to:
C:\WINDOWS\system32\rundll32.exe shell32.dll,#61
or via the Explorer command-line:
explorer.exe Shell:::{2559a1f3-21d7-11d4-bdaf-00c04f60b9f0}

Related

User-defined type not defined vb6

I am attempting to 'save' the context of a text box in vb6 to a *.ini file, so that it can be used in a later part of the program. (i.e. the user would enter something into the text box, then later in the program, a label would appear with the user-entered, saved information).
I used the following code which I copied from the source of someone else's program, however it hasn't worked:
Dim fsys As New FileSystemObject
Dim outstream As TextStream
Dim write1 As String
Dim val1 As String
val1 = Text1.Text
inisettings = App.Path & "\Variables.ini"
Set outstream = fsys.OpenTextFile(inisettings, ForWriting, False, TristateFalse)
outstream.WriteLine (val1)
Set outstream = Nothing
This is the result:
Does anyone have any way to save data for later?
FileSystemObject lives inside an external library, to use it click Project then References and tick Microsoft Scripting Runtime.
You don't actually need to do any of that, the code below uses VB's built-in functionality to write a file.
Dim hF As Integer
hF = FreeFile()
Open App.Path & "\Variables.ini" For Output As #hF
Print #hF, val1
Close #hF
You must declare TristateFalse and give it a value like 0, 1 or 2.
You can take a look at this link: https://msdn.microsoft.com/en-us/subscriptions/bxw6edd3(v=vs.84).aspx
The reason why you are getting this error is because you don't have a reference to the Microsoft Scripting Runtime library. Follow the below instructions while in your VB6 project:
From the top menu, click on Project > References.
From the list, check the item entitled "Microsoft Scripting Runtime".
Click OK.
This will resolve your immediate error however your code still has some other issues. First off, you forgot to declare the variable inisettings. I am going to assume that you will want to always overwrite the entire file each time you update the INI file so you want to use the method CreateTextFile instead of OpenTextFile.
Dim fsys As New FileSystemObject
Dim outstream As TextStream
Dim write1 As String
Dim val1 As String
Dim inisettings As String
val1 = Text1.Text
inisettings = App.Path & "\Variables.ini"
Set outstream = fsys.CreateTextFile(inisettings, True, False)
Call outstream.WriteLine(val1)
Set outstream = Nothing
Good luck!

vbscript to create shortcut from .exe from unknown directory

I work in the IT office of a County Government Agency and we are tasked with, among other things, imaging and setting up computers for the employees. One of our application providers has given us an installer for their application that, upon installation, creates randomized folder names within the parent folder. I am looking for a VBScript that will create a shortcut of a .exe from a directory with unknown sub-folder names and place it in the Public Desktop folder. I have also discovered that the vendor has included two instances of the same application in two different sub-folders. I am only interested in using the path from the first .exe located. I found a script online, (unfortunately, I do not remember where I found it. So, I am unable to give credit to the individual who wrote it), that creates a shortcut if the path is known. I have edited the script by adding some variables and including more icon settings for the shortcut. I am extremely new to scripting, so, I am unable to modify this script to find the path to the .exe and then use the path to create the shortcut. The first .exe is located three sub-folders deep and all three folders have randomized names. Any help is greatly appreciated.
' This script creates a shortcut of MyApp and places it in the Public Desktop folder for all users
Option Explicit
Dim objWSH, objFSO, link, desktopPath, AppPath, IconPath, DirPath
DirPath = "C:\Program Files\MyApp Folder\Randomized1\Randomized2\Randomized3"
IconPath = "C:\Program Files\MyApp Folder\Randomized1\Randomized2\Randomized3\ApplicationIcon.ico"
AppPath = "C:\Program Files\MyApp Folder\Randomized1\Randomized2\Randomized3\MyApp.exe"
Set objWSH = WScript.CreateObject("WScript.Shell")
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
desktopPath = "C:\Users\Public\Desktop"
' If file exists define where the shortcut should point to
If objFSO.FileExists(AppPath) Then
set link = objWSH.CreateShortcut(desktopPath & "\MyApp.lnk")
' Define icon settings
link.TargetPath = AppPath
link.IconLocation = IconPath
link.Description = "MyApp"
link.WindowStyle = 2
link.WorkingDirectory = DirPath
link.Save
Else
WScript.Echo "Program file does not exist"
End if
You should be able to modify this to find the files that you want.
Get all files from a directory and it's sub-directories
GetFileList returns an 1 dimensional array of FileInformation.
Function getFileList(localRoot, fld, ftpArray)
Dim fso, f, baseFolder, subFolder, ftpFile, i
Set fso = CreateObject("Scripting.Filesystemobject")
If IsNull(fld) Then
Set baseFolder = fso.GetFolder(localRoot)
Else
Set baseFolder = fld
End If
For Each f In baseFolder.Files
If IsNull(ftpArray) Then
ReDim ftpArray(0)
Else
i = UBound(ftpArray) + 1
ReDim Preserve ftpArray(i)
End If
Set ftpFile = New FileInformation
ftpFile.setValues localRoot, fso, f
Set ftpArray(i) = ftpFile
Next
For Each subFolder In baseFolder.SubFolders
getFileList localRoot, subFolder, ftpArray
Next
getFileList = ftpArray
End Function
Class FileInformation
Public FilePath
Public FolderPath
Public FileExtension
Public Sub setValues(localRoot, fso, f)
FilePath = f.Path
FolderPath = f.ParentFolder.Path
FileExtension = fso.GetExtensionName(FilePath)
End Sub
End Class
This will search all the FileInformation collected.
File Path: f.FilePath
Folder Path: f.FolderPath
File Extension: f.FileExtension
Const localRootFolder = "C:\Program Files\MyApp Folder"
Dim filelist, f
filelist = getFileList(localRoot, Null, Null)
For Each f In filelist
Next
After more research and testing, I decided to use Command Line to find the path. After getting the syntax right, it works flawlessly. I have included my final script below. I hope this can help someone else. I went here, Running command line silently with VbScript and getting output?, to help me with using Command Line in VBScript. I then found this, https://blogs.technet.microsoft.com/heyscriptingguy/2007/11/08/hey-scripting-guy-how-can-i-remove-a-value-from-the-path-environment-variable/, which shows how to use the Replace function so I could remove MyApp.exe from the path. I then added that to my original script and it worked.
' This script creates a shortcut of the MyApp application and places it in the Public Desktop folder for all users.
Option Explicit
Dim objExec, output, objDir, objWSH, objFSO, link, DesktopPath, AppPath, IconPath, DirPath
Set objWSH = WScript.CreateObject("WScript.Shell")
Set objExec = objWSH.Exec("Where /R ""C:\Program Files\MyApp"" ""MyApp.exe"" ")
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
output = objExec.StdOut.ReadLine
DirPath = Replace(output, "\MyApp.exe", "")
IconPath = (DirPath & "\MyAppIcon.ico")
AppPath = (DirPath & "\MyApp.exe")
DesktopPath = "C:\Users\Public\Desktop"
' If file exists define where the shortcut should point to
If objFSO.FileExists(AppPath) Then
set link = objWSH.CreateShortcut(DesktopPath & "\MyApp.lnk")
' Define icon settings
link.TargetPath = AppPath
link.IconLocation = IconPath
link.Description = "MyApp"
link.WindowStyle = 3
link.WorkingDirectory = DirPath
link.Save
Else
WScript.Echo "Program file does not exist"
End If

vb script not working on windows 8

I have a script that I'm using to add watermarks to pdf and worked fine with windows vista and xp
With windows script I'm getting this error :
80070005
This is the script I'm using :
Option Explicit
Const Watermark = "watermark.pdf"
Const Watermark2 = "AAAWatermark.pdf"
Dim objArgs, fname, tfname, fso, pdf
Set objArgs = WScript.Arguments
fname = objArgs(0)
Set fso = CreateObject("Scripting.FileSystemObject")
tfname = fso.GetTempName
Set pdf = WScript.CreateObject("pdfforge.pdf.pdf")
pdf.StampPDFFileWithPDFFile fname, tfname, Watermark, 1, 9999, false, 1, 10
If fso.FileExists(tfname) Then
fso.DeleteFile(fname)
fso.MoveFile tfname, fname
Else
MsgBox "There was an error adding the Watermark!", vbCritical, AppTitle
End If
Set pdf = Nothing
Set fso = Nothing
Set objArgs = Nothing
Any ideal please?
Thank you
Although I'm a little rough with my Francais, it would appear that you do not have rights to save temporary files in that directory mentioned in the error box or the directory does not exist? You could right click the folder and go to le security tab and add the everyone object and assign write access (or something more secure if you have some other group, etc)
(Edit: The original post had a screenshot in French for added context here)

Vba - returning Shell explorer current path and then close it

First of all, I must say that I'm running a code in a machine that has very few references installed and no Office at all.
I need to open an "explorer.exe" instance with the Shell command, browse through folders, enter in a selected (or newly created) one and finally click on "Ok" on a MsgBox (which pops up simultaneously) to close the Shell and return the selected folder path to a variable.
I have no idea how to achieve that. When I use CurDir, I end up getting a folder which is not the selected one.
Code used (which didn't work):
Sub BrowseForFolder()
ActualDir = "D:\"
Call Shell("C:\Windows\explorer", ActualDir, 1)
If Msgbox("Browse into folder or create a new one and then browse into it, then click ok", vbOkOnly, "Browse") = vbOk Then
' here should be the command to return the path. The following doesn't work since it returns always "D:\"
ActualDir = CurDir
End If
' Here I have to close the Shell - I have no idea what to write to do it
End Sub
Below the code suggested by LS_dev which worked totally:
Private Sub SelectFolder_Click()
Dim objShell As Shell
Had to add the shell references
Dim ssfWINDOWS As Long
Dim objFolder As Folder2
for some reason "Folder" doesn't work
ssfWINDOWS = 36
Set objShell = New Shell
Set objFolder = objShell.BrowseForFolder(0, "Select Folder", 0, "D:\")
If (Not objFolder Is Nothing) Then
FolderName = objFolder.Self.path
End If
Set objShell = Nothing
End Sub

get the target path from a nethood link

Let's say I have a nethood link to a folder with the name "BLABLA" and the target path is "\\servername\temp"
how do I get the string for the target path?
I tried:
Set oShell = CreateObject("WScript.Shell")
Const NET_HOOD = &H13&
Set oShApp = CreateObject("Shell.Application")
sNetHood = oShApp.NameSpace(NET_HOOD).Self.Path
Set oShortCut = oShell.CreateShortcut(sNetHood & "\" & "BLABLA" & ".lnk")
MsgBox "> " & oShortCut.TargetPath
It does everything, even creates a oShortCut object without any errors.
But, it does not return
oShortCut.TargetPath
what am I doing wrong?
I'd like it to return this: "\\servername\temp\BLABLA"
Thanks in advance for any advice!
I've created the shortcut under win 7 with right click in Computer view of the explorer and then > Add a network location > Next ... etc. It creates a Folder representing a shortcut in NetHood to the path on the server ... it's like a mapped share but not really it.
thx for the input ... after years of reading myself into the matter and then checking google once more i found a c# code which i wrote into vbs and then simplified only to see that all i had to change in the end was to add this:
.GetLink
so the solution to my problem is:
Const NET_HOOD = &H13&
Set oShell = CreateObject("Shell.Application")
Set oFolder = oShell.NameSpace(NET_HOOD)
For Each oFile In oFolder.Items
MsgBox oFile.GetLink.Path
Next

Resources