Changing title of an application when launching from command prompt
Asked Answered
A

2

8

I am a business user of an application that has two separate environments: test and production. It is important that I know which environment I'm using at all times, but the application gives no indication. Window title, layout, and all features are identical, and there is no function in the program to identify the environment, so it's my responsibility to remember which .exe I'm currently using.

I had the thought that I could modify the shortcut or use a command prompt to open the window such that the title clearly says "TEST" or "PRODUCTION".

I attempted the below, but, while it launches the application as expected, there is no change to the window title. (I suspect this only works when launching command prompts)

start "different title" fake.exe

Is there a way to accomplish this? Any ideas would be very much appreciated.

Argument answered 18/8, 2016 at 15:22 Comment(1)
Note that your symptom implies that whatever application fake.exe represents explicitly sets the console window title itself. Absent that, the title passed to cmd.exe's internal start command as the first, of necessity double-quoted argument, does take effect.Amplitude
G
8

You need to make a program to do this.

You need to call the Windows' API. This is how to make a title bar changing program.

Create a file using notepad and call it SetText.bas. Store it on your desktop.

Paste this into it.

Imports System
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module MyApplication  

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long

Sub Main()
    On Error Resume Next
    Dim CmdLine As String
    Dim Ret as Long
    Dim A() as String
    Dim hwindows as long

    CmdLine = Command()
    If Left(CmdLine, 2) = "/?" Then
        MsgBox("Usage:" & vbCrLf & vbCrLf & "ChangeTitleBar Oldname NewName")
    Else
        A = Split(CmdLine, Chr(34), -1, vbBinaryCompare)
        hwindows = FindWindow(vbNullString, A(1))
        Ret = SetWindowText(hwindows, A(3))

    End If
End Sub
End Module

Then type in a command prompt window.

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%userprofile%\desktop\SetText.exe" "%userprofile%\desktop\settext.bas" /verbose

A program has been created on your desktop called settext.exe. To use

"%userprofile%\desktop\settext" "Untitled - Notepad" "A Renamed Notepad"
Ginaginder answered 19/8, 2016 at 7:28 Comment(3)
Notes on usage: 1) the final command must be run after the program is open. 2) this is a one-time change of title. If the program changes its own title, the title you set will be irrelevant. (Using the example of notepad, if you renamed it then started typing, it would become *Untitled - Notepad.Horus
Awesome! Not the code, but the way to help someone who may not have any programming experience or know Visual Studio! Very cool!Doorkeeper
@Ginaginder : thank you for an awesome answer but unfortunately it does not work with dynamic changed windows title like in notepad++, windows name could be **new 12 - Notepadd++ is there any solution if I just want to set title as MyNotepad++ without changing it dynamically?Roseola
H
0

Thanks for the above. It was very handy and I've been using it for 6 months but I had a need to perform this with a process ID instead of just the current window name. As such I've written an alternative version of this (code below).

It can be run from CMD with the following arguments: settext.exe /WindowToRename "Old name" "New Name" or settext.exe /pid ProcessIDHere "New Name"

Imports System
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports System.Diagnostics
Imports System.Collections.Generic

Public Module MyApplication

    Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String) As Boolean

    Sub Main()
        On Error Resume Next
        Dim CmdLine As String
        Dim Args() As String

        CmdLine = Command()
        Args = ParseCommandLine(CmdLine)

        If Args.Length >= 3 Then
            If Args(0).ToLower() = "/pid" Then
                Dim targetPid As Integer
                If Integer.TryParse(Args(1), targetPid) Then
                    ChangeTitleByPid(targetPid, Args(2))
                Else
                    Console.WriteLine("Invalid process ID.")
                End If
            ElseIf Args(0).ToLower() = "/windowtorename" Then
                Dim oldName As String = Args(1)
                Dim newName As String = Args(2)
                ChangeTitleByOldName(oldName, newName)
            Else
                ShowUsage()
            End If
        Else
            ShowUsage()
        End If
    End Sub

    Private Sub ShowUsage()
        MsgBox("Usage:" & vbCrLf & vbCrLf & "settext.exe /WindowToRename ""Old name"" ""New Name""" & vbCrLf & vbCrLf & "settext.exe /PID ProcessID ""New Name""")
        Console.WriteLine("Usage:")
        Console.WriteLine("To change the title by process ID: ChangeTitle /pid [Process ID] [New Title]")
        Console.WriteLine("To change the title by old name: ChangeTitle /oldname [Old Name] [New Name]")
    End Sub

    Private Sub ChangeTitleByPid(pid As Integer, newTitle As String)
        Dim processes() As Process = Process.GetProcesses()
        For Each proc As Process In processes
            If proc.Id = pid Then
                SetWindowText(proc.MainWindowHandle, newTitle)
                Exit For
            End If
        Next
    End Sub

    Private Sub ChangeTitleByOldName(oldName As String, newTitle As String)
        Dim processes() As Process = Process.GetProcesses()
        For Each proc As Process In processes
            If proc.MainWindowTitle = oldName Then
                SetWindowText(proc.MainWindowHandle, newTitle)
                Exit For
            End If
        Next
    End Sub

    Private Function ParseCommandLine(commandLine As String) As String()
        Dim args As New List(Of String)
        Dim inQuotes = False
        Dim currentArg As String = ""
        For Each c As Char In commandLine
            If c = """"c Then
                inQuotes = Not inQuotes
            ElseIf c = " "c AndAlso Not inQuotes Then
                args.Add(currentArg)
                currentArg = ""
            Else
                currentArg &= c
            End If
        Next
        args.Add(currentArg)
        Return args.ToArray()
    End Function
End Module
Heighten answered 15/10, 2023 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.