简体   繁体   中英

Send Command to CMD.exe using Batch file

Hi I'm trying to send the command "shutdown" to an already open command prompt called Administrator: Transaction Server using a BAT file but when i run the BAT file nothing happens.

I followed this question - How to execute a command in already open CMD window using VBScirpt

Does anyone know why?

Here's my code:

:: s
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:115 /lparam:1
:: h
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:104 /lparam:1
:: u
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:117 /lparam:1
:: t
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:116 /lparam:1
:: d
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:100 /lparam:1
:: o
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:111 /lparam:1
:: w
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:119 /lparam:1
:: n
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:110 /lparam:1
:: ENTER
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:13 /lparam:1

:: ENTER
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:13 /lparam:1

:: wait for some time (~3 sec) till the program shuts down, if necessary
PING -n 4 localhost 1>NUL

pause
...

I don't know which sendmessage you are using. I downloaded this
SendMessage.exe: Access to advanced Windows features from Dostips.com author:aacini

And with this batch I managed to run shutdown in another cmd window titled Test-Msg :

@ECHO OFF

Set /A WM_CHAR=0x0102
Set Msg=%WM_CHAR%

Set Win="Test-Msg"
::          s   h   u   t   d   o   w   n   ret
For %%A in (115 104 117 116 100 111 119 110 13) Do (
echo SendMessage.exe %Win% %Msg% %%A 1
     SendMessage.exe %Win% %Msg% %%A 1
)
Pause
SendMessage.exe %Win% %Msg% 13 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM