From my software I'd like to be able to access several Windows dialogs direcly. Basically shortcuts to some dialogs that I use from time to time and that are otherwise difficult to access.
One example would be the "Merge or delete network locations" dialog (screenshot).
Using Process Explorer I found out the following information about that dialog:
C:\Windows\System32\netprof.dll
C:\Windows\system32\DllHost.exe /Processid:{44C39C96-0167-478F-B68D-783294A2545D}
Unfortunately I can't figure out what to do with it.
For other dialogs/locations there are ways like this:
Trash:
%windir%\explorer.exe /n,::{645FF040-5081-101B-9F08-00AA002F954E}
Keyboard:
%systemroot%\system32\control.exe /name Microsoft.RegionalAndLanguageOptions /page /p:"keyboard"
But I can't figure out a general way to do this.
The programming language doesn't matter in this case. You're welcome to post a solution in any language or by the command prompt. Please use the mentioned dialog as an example, but I hope to find out a general solution. Thanks.
Not every dialog can be programmatically accessed as you suggest. Some dialogs are custom to the application that launches them, so you won't be able to easily access them.
I wasn't able to find a reference to the "Merge or delete network locations" dialog that you were primarily interested in.
For the ones that can be accessed, there are basically two types.
Dialogs that can be invoked from the regular Windows command line. As mentioned by kenny in the comments, this serverfault question captures a lot of the known commands.
Another approach would be to launch the dialog through powershell. Powershell can access and launch the .NET framework dialogs. This TechNet Scripting Guys article provides an example of launching the open file dialog through powershell.
The article is well worth reading, but this is the snippet that does the magic.
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
} #end function Get-FileName
# *** Entry Point to Script ***
Get-FileName -initialDirectory "c:\fso"
And just in case you wanted something besides the open file dialog, you would change the following line to the dialog you need. $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
Related
There are various questions and answers on SO explaining how to create a shortcut to the printer queue of a printer using powershell. They all use a shortcut destination like this: C:\Windows\System32\rundll32.exe printui.dll,PrintUIEntry /n Printername.
However, the shortcuts created like this do not have the right-click options like Scan which are visible when right-clicking the printer on the printer & devices page in the control panel. Right-clicking the printer on the Devices and Printers page reveals a Create Shortcut option, which creates a shortcut which has the same right-click options as the original item. (Dragging the printer to the desired destination folder works too)
The properties pages of the shortcuts created a) with powershell b) with the Create shortcut option also look quite different:
Thus, my question is: How do I create a shortcut that is equivalent to the shortcut created when using the right-click option Create Shortcut on a printer on the devices and printers page with powershell?
The correct shortcut does not point to rundll32.exe, it points to the printer in the shell namespace. This target is an item id list, not a filesystem path.
I don't know the native way to do this in Powershell. With P/invoke it would be SHParseDisplayName + IShellLink::SetIDList.
You would probably want to go the reverse way first; on a correct link, get its id list and call SHGetNameFromIDList(...,SIGDN_DESKTOPABSOLUTEPARSING,...). The returned string would look something like ::{GUIDHERE}\::{ANOTHERGUID}\MyPrinterName.
Thanks to Anders for bringing me onto the right track.
You can achieve it like this:
Get a list of all devices and printers:
$shell = New-Object -ComObject Shell.Application
$devices_and_printers = $shell.namespace("::{26EE0668-A00A-44D7-9371-BEB064C98683}\2\::{A8A91A66-3A7D-4424-8D24-04E180695C7A}")
$devices_and_printers.items() | select name,path
Create shortcut:
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:userprofile\Desktop\My Printer.lnk")
$Shortcut.TargetPath = ($devices_and_printers.items() | where { $_.name -eq "My Printer Name" }).Path
$Shortcut.Save()
I would like to have an executable file run at startup, Windows 7.
I was wondering how I may do so directly at the command line or by way of batch file.
Windows provides powershell to automatize this kind of tasks using "command prompt" or scripts.
In the case you are proposing, you can change the registry values that controls startup programs by using the new-item-property cmdlet like this
PS> New-ItemProperty HKM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\ -Name <pretty beautiful name for your app> -Value <executable for your app>
You can also use PowerShell to elliminate or to change keys. But, you know
With big power comes great responsability
On a windows comp
Hit the win + r
A small dialog box appears on bottom left. In the box type
shell:startup
You are automatically redirected to the relevant folder. Just copy past ur program into this folder.
If u have a more malicious intent in mind simply use the copy command to copy the file from its current destination(A USB perhaps) to the location gotten from the aforementioned method.
However there is a slight catch. This only works if ur the admin of the computer. So you would theoretically have to include code to hand your program admin access before copying the file
[Tutorial on how to do it here1
Is there some way I can programmatically (in VBS) OR by using CMD/RUN open the 'Open' dialog that contains the places bar and a browser but without opening say notepad or MSpaint?
http://i.technet.microsoft.com/dynimg/IC354177.jpg
I'd like to use this on the desktop itself, it would be really cool if there was a DLL I can just use instead of having a VBS file but if not i'm sure its possible in VBS.
I'm busy searching where the actual open dialog box comes from, it should come from some DLL file somewhere.
I might even consider stopping the windows shell from opening all together and just using this open window as the shell on some computers.
Regards, Rocklore
What version of Windows are you on?
"UserAccounts.CommonDialog" was the way to do this in XP. But it no longer exists in Windows 7. You may be able to use some of the flags available for the BrowseForFolder() method to make it look like a file open dialog. See this page for an example.
XP Edit:
Here's an XP example using UserAccounts.CommonDialog.
With CreateObject("UserAccounts.CommonDialog")
.InitialDir = CreateObject("WScript.Shell").SpecialFolders("Desktop")
.Filter = "All Files|*.*"
' Show the dialog. If [Open] is clicked, save the name of the selected file...
If .ShowOpen Then strFile = .FileName
End With
Is there any method in JScript to get the handle of the main window of a process by providing the process name? The Process.MainWindowHandle property works only in JScript .NET. Is anything similar available in classic JScript?
I am not sure if this works, just try to loop window.parent until its undefined.
something like -
var mainWindow = window;
while( mainWindow.parent ) {
mainWindow = mainWindow.parent;
}
you also have something like window.top which always returns you the topmost window. But not sure if this is supported by all browsers.
JScript and Windows Script Host don't have this functionality, and neither does WMI.
If PowerShell is an option for you, then you can use the Process.MainWindowHandle property you mentioned:
(Get-Process notepad).MainWindowHandle
Otherwise, you'll need to find or write an utility (COM object, command-line tool etc) that would provide this functionality, and call this tool from your script.
Edit: So you need to close the window — that's a UI automation task.
Windows Script Host provides very limited UI automation functionality. If you know the window title, you could try using the AppActivate to and SendKeys methods to activate that window and send the Alt+F4 shortcut to it. You can find an example this answer. (The code is in VBScript, but it should give you the idea.) However, this approach isn't reliable.
If you really really don't want to kill the process, the easiest solution is to use some third-party UI automation tool. For example, you could try the free AutoIt tool — I think it should be able to accomplish what you need.
Edit 2: Have you tried recording the closing of the window? You should get a script like this:
Sys.Process("notepad").Window("Notepad", "Untitled - Notepad").Close();
Isn't this what you need?
For a native win32 application, there is no such thing as a "main window". A process can have no windows at all, or several top level "main" windows.
Well once i had to write a add-in for Outlook. My boss wants a splash-screen to appear when Outlook loads. But Outlook window goes over the splash. After a lot of search i found FindWindow http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k%28FINDWINDOW%29%3bk%28TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22%29%3bk%28DevLang-CSHARP%29&rd=true this is help for it . This function finds window based on window caption and window class name. I p-invoked it and used it from C#. If you can use this function through JScript I think it could do the job for you. (I used Spy++ for finding lpClassName parameter)
Is there a way to bring up the Windows XP shutdown dialog (the one with the three buttons – Suspend, Shutdown, Restart and Hibernate when Shift is pressed) using any language (preferred: C/C++, VB, Haskell, batches)?
I think I can load the msgina.dll in my C++ program, but I dunno what to do next – what function in the dll is used to show the dialog?
Assuming that your language has a way to do basic file I/O and invoke shortcuts on Windows, try this tip from here:
Create a new txt file somewhere on your system, open it and put in this one line:
(new ActiveXObject("Shell.Application")).ShutdownWindows();
Save and close the file. Change the extension to *.js.
You can make a shortcut to that file to make it easy to shut down your system.
I find PowerShell to be more elegant than other Windows scripting options.
(New-Object -Com Shell.Application).ShutdownWindows()