我的一个C#应用程序出现了一个非常奇怪的问题:
其一般用途是按照[user32.dll] SendMessage
发送命令。这在普通机器上或从批处理文件或命令行窗口启动时都不会出现问题。
如果我从Telnet、SSH或PHP的shell_exec()
执行应用程序,则应用程序无法找到目标窗口句柄,即使它可以找到目标进程。
我写了一段测试代码来向你展示我的问题:
public static int Main(string[] argv)
{
foreach (Process p in Process.GetProcesses())
Console.Write(p.MainWindowHandle.ToInt32().ToString("x8") + " - " + p.ProcessName + "n");
Console.ReadKey(true);
return 0;
}
以"正常"方式执行时,结果如下:
00000000 - BTTray
00000000 - SkypeC2CAutoUpdateSvc
00000000 - svchost
00000000 - DLMSession
00000000 - conhost
00000000 - chrome
00000000 - GoogleCrashHandler64
000b0a58 - devenv
00000000 - FreeSSHDService
00000000 - GoogleCrashHandler
00000000 - mDNSResponder
00000000 - conhost
00000000 - AcSvc
00000000 - taskeng
00000000 - lsm
00000000 - chrome
00000000 - UMVPFSrv
00000000 - lsass
00000000 - IntelliTrace
00000000 - PresentationFontCache
00000000 - conhost
00000000 - smss
00000000 - AppleMobileDeviceService
00000000 - services
00000000 - audiodg
00000000 - RCIMGDIR
00000000 - password_manager
00000000 - chrome
00000000 - tvt_reg_monitor_svc
00000000 - netsession_win
00030abe - devenv
00000000 - chrome
00000000 - WmiPrvSE
00000000 - PrintIsolationHost
00000000 - IpOverUsbSvc
00000000 - msvsmon
00000000 - WLIDSVC
00000000 - UNS
0003008a - tpnumlkd
00000000 - chrome
00000000 - mitsijm
00000000 - cmd
00000000 - svchost
00000000 - httpd
00000000 - QDLService2kLenovo
00000000 - spoolsv
00000000 - winlogon
00000000 - TCPSVCS
00000000 - WUDFHost
00000000 - SynTPHelper
00000000 - vmnat
00000000 - conhost
00000000 - chrome
00000000 - NisSrv
00000000 - svchost
00000000 - netsession_win
00000000 - mysqld
00000000 - RegSrvc
00000000 - vmnetdhcp
00000000 - chrome
00000000 - cmd
00000000 - SynTPLpr
00000000 - iPodService
00000000 - rundll32
00000000 - msseces
00000000 - csrss
00000000 - svchost
00000000 - TrustedInstaller
00000000 - nvvsvc
000101aa - SynTPEnh
00000000 - wininit
00000000 - nvxdsync
00000000 - btwdins
00000000 - MFUSNM~1
00000000 - chrome
00000000 - EvtEng
00000000 - dgnsvc
00000000 - taskeng
00000000 - vmware-tray
00000000 - svchost
00000000 - PrivacyIconClient
00000000 - MSBuild
00000000 - AcDeskBandHlpr
00000000 - TpScrex
00200738 - notepad++
00000000 - PnkBstrA
00000000 - chrome
00000000 - cssauth
00000000 - chrome
00000000 - SUService
00000000 - svchost
00000000 - chrome
00000000 - AdAppMgrSvc
00000000 - chrome
00000000 - LMS
00000000 - WmiPrvSE
00000000 - IntelliTrace
00000000 - svchost
00000000 - csrss
00000000 - DTLite
00000000 - TPONSCR
00000000 - iTunesHelper
00000000 - agent
00000000 - chrome
00000000 - sqlwriter
00000000 - DZSVC64
00000000 - conhost
00000000 - rundll32
00000000 - sshd
00000000 - acrotray
00000000 - chrome
00000000 - SvcGuiHlpr
00000000 - telnet
00000000 - tpnumlk
00000000 - loggerservice
00000000 - wlanext
00000000 - svchost
00000000 - explorer
00000000 - TpShocks
00000000 - micmute
00000000 - mounter
00000000 - AcPrfMgrSvc
00000000 - TPOSDSVC
00000000 - BlackGlassEnhanced
00000000 - svchost
002e03f6 - chrome
00000000 - TPHKSVC
00000000 - SpotifyWebHelper
00000000 - unsecapp
00000000 - ibmpmsvc
00000000 - cammute
000303d8 - clover
00000000 - nvvsvc
00000000 - svchost
00000000 - ISUSPM
00000000 - password_manager
00000000 - wodCmdTerm
00000000 - TeamViewer_Service
00000000 - FLxHCIm
00000000 - vmware-usbarbitrator64
00000000 - rundll32
00160f3c - wmplayer
00000000 - conhost
0002001c - explorer
00000000 - dwm
00000000 - CISVC
00000000 - svchost
00000000 - svchost
00091068 - cmd
00000000 - test implementation.vshost
00000000 - svchost
00000000 - chrome
00000000 - taskhost
00000000 - svchost
00000000 - armsvc
00000000 - OSPPSVC
00000000 - conhost
00000000 - MCPLaunch
00000000 - svchost
00030ff6 - WMP
00000000 - vmware-authd
00000000 - conhost
00000000 - conhost
00000000 - WUDFHost
00000000 - chrome
00000000 - SkypeC2CPNRSvc
00000000 - iviRegMgr
00000000 - vmware-hostd
00000000 - chrome
00000000 - wuauclt
00000000 - httpd
00000000 - taskhost
00000000 - cygrunsrv
00000000 - UnsignedThemesSvc
00000000 - MsMpEng
00000000 - WLIDSVCM
00000000 - System
00000000 - DLG
00000000 - SearchIndexer
00000000 - Idle
正如您所看到的,有许多进程没有窗口句柄,但像VisualStudio(devnev
)这样的进程有一个
如果我通过Telnet、SSH等运行相同的命令,我会得到以下结果:
00000000 - BTTray
00000000 - SkypeC2CAutoUpdateSvc
00000000 - svchost
00000000 - DLMSession
00000000 - conhost
00000000 - chrome
00000000 - GoogleCrashHandler64
00000000 - devenv
00000000 - FreeSSHDService
00000000 - GoogleCrashHandler
00000000 - mDNSResponder
00000000 - conhost
00000000 - AcSvc
00000000 - taskeng
00000000 - lsm
00000000 - chrome
00000000 - UMVPFSrv
00000000 - lsass
00000000 - IntelliTrace
00000000 - PresentationFontCache
00000000 - conhost
00000000 - smss
00000000 - AppleMobileDeviceService
00000000 - services
00000000 - audiodg
00000000 - RCIMGDIR
00000000 - password_manager
00000000 - chrome
00000000 - tvt_reg_monitor_svc
00000000 - netsession_win
00000000 - devenv
00000000 - chrome
00000000 - WmiPrvSE
00000000 - PrintIsolationHost
00000000 - IpOverUsbSvc
00000000 - msvsmon
00000000 - WLIDSVC
00000000 - UNS
00000000 - tpnumlkd
00000000 - chrome
00000000 - mitsijm
00000000 - cmd
00000000 - svchost
00000000 - httpd
00000000 - QDLService2kLenovo
00000000 - spoolsv
00000000 - winlogon
00000000 - TCPSVCS
00000000 - WUDFHost
00000000 - SynTPHelper
00000000 - vmnat
00000000 - conhost
00000000 - chrome
00000000 - NisSrv
00000000 - svchost
00000000 - netsession_win
00000000 - mysqld
00000000 - RegSrvc
00000000 - vmnetdhcp
00000000 - chrome
00020062 - cmd
00000000 - SynTPLpr
00000000 - iPodService
00000000 - rundll32
00000000 - msseces
00000000 - csrss
00000000 - svchost
00000000 - TrustedInstaller
00000000 - nvvsvc
00000000 - SynTPEnh
00000000 - wininit
00000000 - nvxdsync
00000000 - btwdins
00000000 - MFUSNM~1
00000000 - chrome
00000000 - EvtEng
00000000 - dgnsvc
00000000 - taskeng
00000000 - vmware-tray
00000000 - svchost
00000000 - PrivacyIconClient
00000000 - MSBuild
00000000 - AcDeskBandHlpr
00000000 - TpScrex
00000000 - notepad++
00000000 - PnkBstrA
00000000 - chrome
00000000 - cssauth
00000000 - chrome
00000000 - SUService
00000000 - svchost
00000000 - chrome
00000000 - AdAppMgrSvc
00000000 - chrome
00000000 - LMS
00000000 - WmiPrvSE
00000000 - IntelliTrace
00000000 - svchost
00000000 - csrss
00000000 - DTLite
00000000 - TPONSCR
00000000 - iTunesHelper
00000000 - agent
00000000 - chrome
00000000 - sqlwriter
00000000 - DZSVC64
00000000 - conhost
00000000 - rundll32
00000000 - sshd
00000000 - acrotray
00000000 - chrome
00000000 - SvcGuiHlpr
00000000 - telnet
00000000 - tpnumlk
00000000 - loggerservice
00000000 - wlanext
00000000 - svchost
00000000 - explorer
00000000 - TpShocks
00000000 - micmute
00000000 - mounter
00000000 - AcPrfMgrSvc
00000000 - TPOSDSVC
00000000 - BlackGlassEnhanced
00000000 - svchost
00000000 - chrome
00000000 - TPHKSVC
00000000 - SpotifyWebHelper
00000000 - unsecapp
00000000 - ibmpmsvc
00000000 - cammute
00000000 - clover
00000000 - nvvsvc
00000000 - svchost
00000000 - ISUSPM
00000000 - password_manager
00000000 - wodCmdTerm
00000000 - TeamViewer_Service
00000000 - FLxHCIm
00000000 - vmware-usbarbitrator64
00000000 - rundll32
00000000 - wmplayer
00000000 - conhost
00000000 - explorer
00000000 - dwm
00000000 - CISVC
00000000 - svchost
00000000 - svchost
00000000 - cmd
00000000 - test implementation.vshost
00000000 - svchost
00000000 - chrome
00000000 - taskhost
00000000 - svchost
00000000 - armsvc
00000000 - OSPPSVC
00000000 - conhost
00000000 - MCPLaunch
00000000 - svchost
00000000 - WMP
00000000 - vmware-authd
00000000 - conhost
00000000 - conhost
00000000 - WUDFHost
00000000 - chrome
00000000 - SkypeC2CPNRSvc
00000000 - iviRegMgr
00000000 - vmware-hostd
00000000 - chrome
00000000 - wuauclt
00000000 - httpd
00000000 - taskhost
00000000 - cygrunsrv
00000000 - UnsignedThemesSvc
00000000 - MsMpEng
00000000 - WLIDSVCM
00000000 - System
00000000 - DLG
00000000 - SearchIndexer
00000000 - Idle
正如您所看到的,除了cmd
之外,每个进程都没有窗口句柄
我的问题是:我该如何解决这个问题?还是Windows以某种方式阻止了通过Telnet访问窗口?
非常感谢
Telnet在与用户会话不同的Windows会话中运行。通常,所有服务都在会话0中运行,所有用户应用程序都在会话1+中运行。当你运行应用程序时,你只能看到那些与你的应用程序在同一会话中运行的窗口名称。阅读更多关于烦人的Windows会话机制的信息(试着用谷歌搜索)。