求助,WINDOWS进程搜索重定向向问题,高分请教

Windows进程内标准输出重定向_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Windows进程内标准输出重定向
上传于|0|0|文档简介
&&编程实现使用iostream输出调试信息。
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
你可能喜欢2001年4月 VC/MFC大版内专家分月排行榜第三
2004年3月 其他开发语言大版内专家分月排行榜第二2004年1月 其他开发语言大版内专家分月排行榜第二2003年9月 其他开发语言大版内专家分月排行榜第二
本帖子已过去太久远了,不再提供回复功能。求助!WINDOWS进程重定向问题,高分请教! -DotNet- TryCatch
>> Content
求助!WINDOWS进程重定向问题,高分请教!
这个程序中创建了作业进程,并将进程重定向到文件,但是却没有输出到文件,请问这是为什么?请高手帮帮忙......非常感谢!!!#include&&iostream&#define&_WIN32_WINNT&0x0500#include&&windows.h&#include&&winbase.h&#include&&string&#include&&fstream&using&namespace&struct&ProcessRunResult{long&TimeUlong&MemoryUstring&string&E};class&OnlineJudgeException{string&s_public:void&set(string&str){s_exc=}string&get(){return&s_}OnlineJudgeException(){}OnlineJudgeException(string&str){s_exc=}~OnlineJudgeException(){}};ProcessRunResult&StartJudgeProcess(string&program,string&file_in,string&file_out,string&user_answer,long&TimeLimit,long&MemoryLimit){HANDLE&hJob=CreateJobObject(NULL,NULL);//创建作业ProcessRunResult&//进程返回信息BOOL&b_//成功/失败标志try{JOBOBJECT_EXTENDED_LIMIT_INFORMATION&JobExtendL//设置作业进程的扩展限制JobExtendLimit.BasicLimitInformation.LimitFlags=JOB_OBJECT_LIMIT_PRIORITY_CLASS&|//启用进程优先级设定JOB_OBJECT_LIMIT_JOB_MEMORY|//启用作业进程内存限制JOB_OBJECT_LIMIT_PROCESS_MEMORY&|//启用进程内存限制JOB_OBJECT_LIMIT_JOB_TIME&|//启用作业进程时间限制JOB_OBJECT_LIMIT_PROCESS_TIME|//启用进程时间限制JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION;//使系统为与作业相关的每个进程关闭“未处理的异常情况”对话框JobExtendLimit.BasicLimitInformation.PriorityClass=NORMAL_PRIORITY_CLASS;//进程优先级为正常JobExtendLimit.BasicLimitInformation.PerJobUserTimeLimit.QuadPart=(TimeLimit+1)*;//作业进程时间限制sJobExtendLimit.BasicLimitInformation.PerProcessUserTimeLimit.QuadPart=TimeLimit*;//子进程时间限制sJobExtendLimit.JobMemoryLimit=(MemoryLimit+1)*;//作业内存限制MBJobExtendLimit.ProcessMemoryLimit=MemoryLimit*;//每个进程内存限制MBb_set=SetInformationJobObject(hJob,JobObjectExtendedLimitInformation,&JobExtendLimit,sizeof(JobExtendLimit));//设置作业扩展限制if(!b_set)throw&OnlineJudgeException("作业扩展限制设置失败!");//设置失败抛出异常JOBOBJECT_BASIC_UI_RESTRICTIONS&JobUIR;//设置基本UI限制JobUIR.UIRestrictionsClass=JOB_OBJECT_UILIMIT_EXITWINDOWS&|//用于防止进程通过ExitWindowsEx函数退出、关闭、重新引导或关闭系统电源JOB_OBJECT_UILIMIT_SYSTEMPARAMETERS&|//防止进程通过SystemParametersInfor函数来改变系统参数JOB_OBJECT_UILIMIT_DISPLAYSETTINGS&|//防止进程通过ChangeDisplaySettings函数来改变显示设置JOB_OBJECT_UILIMIT_DESKTOP&|//防止进程使用CreateDesktop或SwitchDesktop函数创建或转换桌面JOB_OBJECT_UILIMIT_HANDLES;//防止作业中的进程使用同一作业外部的进程创建的USER对象(如HWND)b_set=SetInformationJobObject(hJob,JobObjectBasicUIRestrictions,&JobUIR,sizeof(JobUIR));//设置基本UI限制if(!b_set)throw&OnlineJudgeException("作业基本UI限制设置失败!");//设置失败抛出异常&&&&&&&&//创建一个进程,此进程创建时挂起以便进行设置限制.STARTUPINFO&si&=&{sizeof(si)};si.dwFlags|=STARTF_USESTDHANDLES;//设置重定向输入输出标志si.lpReserved=NULL;si.lpDesktop=NULL;si.lpTitle=NULL;si.lpReserved2=NULL;HANDLE&in,user_user_out=CreateFile(user_answer.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);in=CreateFile(file_in.c_str(),GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);si.hStdInput=si.hStdOutput=user_si.hStdError=user_PROCESS_INFORMATION&//创建一个进程b_set=CreateProcess(NULL,(LPSTR)program.c_str(),NULL,&NULL,&TRUE,//TRUE启用重定向输入输出CREATE_SUSPENDED,&NULL,&NULL,&&si,&&pi);if(!b_set)throw&OnlineJudgeException("创建进程失败!");//创建进程失败抛出异常//将进程放入作业中b_set=AssignProcessToJobObject(hJob,pi.hProcess);if(!b_set){ExitProcess(GetLastError());throw&OnlineJudgeException("将进程放入作业失败!");}ResumeThread(pi.hThread);//启动进程CloseHandle(pi.hThread);//关闭进程线程句柄CloseHandle(pi.hProcess);//关闭进程句柄}catch(OnlineJudgeException&exc){result.Exc=exc.get();return&}catch(...){result.Exc="未知错误!";return&}TerminateJobObject(hJob,0);CloseHandle(hJob);//关闭作业句柄return&}int&main(int&argc,&char*&argv[]){string&strcmd,strin,strout,struser_ProcessRunResult&strin="e:\\in.txt";strout="e:\\out.txt";struser_out="e:\\user_out.txt";strcmd="e:\\hello.exe";result=StartJudgeProcess(strcmd,strin,strout,struser_out,5,32);return&0;}
------Solutions------
b_set=CreateProcess(NULL//这里,(LPSTR)program.c_str(),NULL,&NULL,&TRUE,//TRUE启用重定向输入输出CREATE_SUSPENDED,&NULL,&NULL,&&si,&&pi);这里有错,启动一个NULL???
------Solutions------
不是,程序名在(LPSTR)program.c_str()中给出了,如果第一个参数为NULL,则从第二个参数中寻找程序名
------Solutions------
怎么样才能联系你,我的邮箱是.cn2010年2月 Java大版内专家分月排行榜第二
2011年7月 Java大版内专家分月排行榜第三2010年1月 Java大版内专家分月排行榜第三2009年12月 Java大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。查杀不死进程-强制挂掉它!
查杀不死进程-强制挂掉它!
ntsd  ntsd从Windows
2000开始就是系统自带的进程调试工具,在system32目录下。NTSD的功能非常的强大,用法也比较复杂,但如果只用来结束一些进程,那就比较简单了。在Windows中只有System、SMSS.EXE和CSRSS.EXE不能杀。前两个是纯内核态的,最后那个是Win32子系统,ntsd本身需要它。lsass.exe也不要杀掉,它是负责本地账户安全的。被调试器附着的进程会随调试器一起退出,所以可以用来在命令行下终止进程。
  打开cmd 后输入以下命令就可以结束进程:
  方法一:利用进程的PID结束进程
  命令格式:ntsd -c q -p pid
  命令范例: ntsd -c q -p 1332 (结束explorer.exe进程)
  范例详解:explorer.exe的pid为1332,但是如何获取进程的pid呢?在CMD下输入TASKLIST就可以获取当前任务管理器所有进程的PID。或者打开任务管理器,在菜单栏,选择“查看”—“选择列”,在打开的选择项窗口中将“PID(进程标识符)”项选择钩上,这样任务管理器的进程中就会多出PID一项了。(PID的分配并不固定,是在进程启动是由系统随机分配的,所以进程每次启动的进程一般都不会一样。)
  可使用以下命令:
  =================================================
  @echo off
  mode con cols=30 lines=5
  color 1e
  set /p t=请输入进程名:
  tasklist /fo csv&2.txt
  find "%t%" 2.txt&1.txt
  for /f "delims=, tokens=2" %%i in (1.txt) do set a=%%i
  ntsd -c q -p %a%
  echo PID NAME
  echo ============
  echo %a% %T%
  del 1.txt
  del 2.txt
  pause &nul
  =================================================
  方法二:利用进程名结束进程
  命令格式:ntsd -c q -pn ***.exe (***.exe 为进程名,exe不能省)
  命令范例:ntsd -c q -pn explorer.exe
  另外的能结束进程的DOS命令还有taskkill和tskill命令:
  命令格式: taskkill /pid 1234 /f ( 也可以达到同样的效果。)
  如果上面这些还不能满足您的求知欲,下面还有:
  ntsd详解
  有一些高等级的进程,tskill和taskkill或许无法结束,那么我们还有一个更强大的工具,那就是系统debug级的ntsd.准确的说,ntsd是一个系统调试工具,只提供给系统开发级的管理员使用,但是对我们杀掉进程还是很爽的.基本上除了WINDOWS系统自己的管理进程,ntsd都可以杀掉。NTSD
调试程序在启动时要求用户指定一个要连接的进程。使用 TLIST 或 PVIEWER,您可以获得某个现有进程的进程 ID,然后键入
NTSD -p pid 来调试这个进程。NTSD 命令行使用如下的句法:
  NTSD [options] imagefile
  其中,imagefile 是要调试的映像名称。
  用法usage: ntsd [-?] [-2] [-d] [-g] [-G] [-myob] [-lines] [-n] [-o]
[-s] [-v] [-w]
  [-r BreakErrorLevel] [-t PrintErrorLevel]
  [-hd] [-pd] [-pe] [-pt #] [-pv] [-x | -x{e|d|n|i}
  [-- | -p pid | -pn name | command-line | -z CrashDmpFile]
  [-zp CrashPageFile] [-premote transport] [-robp]
  [-aDllName] [-c "command"] [-i ImagePath] [-y SymbolsPath]
  [-clines #] [-srcpath SourcePath] [-QR ] [-wake ]
  [-remote transport:server=name,portid] [-server
transport:portid]
  [-ses] [-sfce] [-sicv] [-snul] [-noio] [-failinc]
[-noshell]
  where: -? displays this help text
  command-line is the command to run under the debugger
  -- is the same as -G -g -o -p -1 -d -pd
  -aDllName sets the default extension DLL
  -c executes the following debugger command
  -clines number of lines of output history retrieved by a remote
  -failinc causes incomplete symbol and module loads to fail
  -d sends all debugger output to kernel debugger via
  -d cannot be used with debugger remoting
  -d can only be used when the kernel debugger is enabled
  -g ignores initial breakpoint in debuggee
  -G ignores final breakpoint at process termination
  -hd specifies that the debug heap should not be used
  for created processes. This only works on Windows Whistler.
  -o debugs all processes launched by debuggee
  -p pid specifies the decimal process Id to attach to
  -pd specifies that the debugger should automatically detach
  -pe specifies that any attach should be to an existing debug
  -pn name specifies the name of the process to attach to
  -pt # specifies the interrupt timeout
  -pv specifies that any attach should be noninvasive
  -r specifies the (0-3) error level to break on
(SeeSetErrorLevel)
  -robp allows breakpoints to be set in read-only memory
  -t specifies the (0-3) error level to display
(SeeSetErrorLevel)
  -w specifies to debug 16 bit applications in a separate VDM
  -x sets second-chance break on AV exceptions
  -x{e|d|n|i} &event& sets the break
status for the specified event
  -2 creates a separate console window for debuggee
  -i ImagePath specifies the location of the executables that
  the fault (see _NT_EXECUTABLE_IMAGE_PATH)
  -lines requests that line number information be used if
  -myob ignores version mismatches in DBGHELP.DLL
  -n enables verbose output from symbol handler
  -noio disables all I/O for dedicated remoting servers
  -noshell disables the .shell (!!) command
  -QR && queries for remote
  -s disables lazy symbol loading
  -ses enables strict symbol loading
  -sfce fails critical errors encountered during file
  -sicv ignores the CV record when symbol loading
  -snul disables automatic symbol loading for unqualified
  -srcpath &SourcePath& specifies the
source search path
  -v enables verbose output from debugger
  -wake wakes up a sleeping debugger and exits
  -y &SymbolsPath& specifies the
symbol search path (see _NT_SYMBOL_PATH)
  -z &CrashDmpFile& specifies the
name of a crash dump file to debug
  -zp &CrashPageFile& specifies the
name of a page.dmp file
  to use with a crash dump
  -remote lets you connect to a debugger session started with
  must be the first argument if present
  transport: tcp | npipe | ssl | spipe | 1394 | com
  name: machine name on which the debug server was created
  portid: id of the port the debugger server was created on
  for tcp use: port=&socket port
  for npipe use: pipe=&name of
  for 1394 use: channel=&channel
  for com use: port=&COM
port&,baud=&baud
  channel=&channel #&
  for ssl and spipe see the documentation
  example: ... -remote npipe:server=yourmachine,pipe=foobar
  -server creates a debugger session other people can connect
  must be the first argument if present
  transport: tcp | npipe | ssl | spipe | 1394 | com
  portid: id of the port remote users can connect to
  for tcp use: port=&socket port
  for npipe use: pipe=&name of
  for 1394 use: channel=&channel
  for com use: port=&COM
port&,baud=&baud
  channel=&channel #&
  for ssl and spipe see the documentation
  example: ... -server npipe:pipe=foobar
  -premote transport specifies the process server to connect
  transport arguments are given as with remoting
  Environment Variables:
  _NT_SYMBOL_PATH=[Drive:][Path]
  Specify symbol image path.
  _NT_ALT_SYMBOL_PATH=[Drive:][Path]
  Specify an alternate symbol image path.
  _NT_DEBUGGER_EXTENSION_PATH=[Drive:][Path]
  Specify a path which should be searched first for extensions
  _NT_EXECUTABLE_IMAGE_PATH=[Drive:][Path]
  Specify executable image path.
  _NT_SOURCE_PATH=[Drive:][Path]
  Specify source file path.
  _NT_DEBUG_LOG_FILE_OPEN=filename
  If specified, all output will be written to this file from offset
  _NT_DEBUG_LOG_FILE_APPEND=filename
  If specified, all output will be APPENDed to this file.
  _NT_DEBUG_HISTORY_SIZE=size
  Specifies the size of a server's output history in
  Control Keys:
  &Ctrl-B&&Enter&
Quit debugger
  &Ctrl-C& Break into Target
  &Ctrl-F&&Enter&
Force a break into debuggee (same as Ctrl-C)
  &Ctrl-P&&Enter&
Debug Current debugger
  &Ctrl-V&&Enter&
Toggle Verbose mode
  &Ctrl-W&&Enter&
Print version information
  ntsd: exiting - press enter ---
  选项option:
  -2打开一个用于调试字符模式的应用程序的新窗口
  -d将输出重定向到调试终端-g 使执行自动通过第一个断点
  -G使 NTSD 在子程序终止时立即退出o启用多个进程的调试,默认值为由调试程序衍生的一个进程
  -p指定调试由进程 ID 标识的进程
  -v产生详细的输出。
  例如,假设 inetinfo.exe 的进程 ID 为 104。键入命令“NTSD -p 104”将 NTSD 调试程序连接到
inetinfo 进程 (IIS)。也可使用 NTSD 启动一个新进程来进行调试。例如,NTSD notepad.exe
将启动一个新的 notepad.exe
进程,并与它建立连接。一旦连接到某个进程,您就可以用各种命令来查看堆栈、设置断点、转储内存,等等。
  命令含义~显示所有线程的一个列表KB 显示当前线程的堆栈轨迹~*KB显示所有线程的堆栈轨迹R显示当前
  帧的寄存器输出U反汇编代码并显示过程名和偏移量D[type][&
range&]转储内存BP设置断点BC[]清除一个或多个断点BD[]禁用一个或多个断点BE[&
bp&]启用一个或多个断点BL[]列出一个或多个断点。
  个人意见,有一个非常重要的参数就是-v参数,我们可以通过它发现一个进程下面挂接了哪些连接库文件。有很多病毒,木马,或者恶意软件,都喜欢把自己做成动态库,然后注册到系统正常程序的加载库列表中,达到隐藏自己的目的.
  首先我们需要设置一下ntsd的输出重定向,最好是重定向到一个文本文件,方便我们分析研究.
  c:\&set _NT_DEBUG_LOG_FILE_APPEND=c:\pdw.txt
  注意,虽然输出重定向了,但是我们的输出依然会继续显示在屏幕上,而且会进入到debug模式,我们使用-c
q参数,就可以避免这个问题.
  c:\&ntsd -c q -v notepad.exe
  现在我们的pdw.txt文件中,就可以看见notepad.exe文件的调试信息.
  可以知道,ntsd的软件终止能力是很好很强大的,一些taskkill都无法终止的软件(如Student.exe这一类或木马)可以用ntsd轻易终止
ntsd和system权限
把程序写成service的形式,就可以获得system权限。
对进程而言,system权限最高,对用户而言,administrator最高。是不同的概念。
当然,system权限比admin权限要高。
WinExec("&&
c:\\",SW_SHOW); 執行&&
,完成後關閉&&
WinExec("&&
c:\\",SW_SHOW); 執行&&
,完成後不關閉&&
MSDOS&& 視窗&
问:任务管理器已被系统管理员禁用或者按ctrl+alt+del键,任务管理器成灰色不可用。
方法1:运行regedit打开注册表,HKEY_CURRENT_USER\software\microsoft\windows\currentversion\policies\system将右侧disabletaskmgr值设为0或者删除该键。
方法2:开始--运行--gpedit.msc--用户配置 --管理模板--系统--Alt+Ctrl+Del选项
--双击删除任务管理器--末配置--确定。
问:按下“Ctrl+Alt+Del”键调不出任务管理器,切换到“Windows安全”窗口,然后再选择“任务管理器”按钮才能打开。
答:单击“开始→控制面板→用户账户”,然后在打开的“用户账户”窗口中点击“更改用户登录或注销方式”,然后选择“使用欢迎屏幕”,这样就可以继续“Ctrl+Alt+Del”来调出任务管理器了。
问:开机桌面什么也没用。
答:进入任务管理器,结束explor.exe进程,然后点击新任务,浏览--c:\winnt\explor.exe,确定。或者打开任务管理器,文件,新建任务,输入explorer.exe。
问:有某个软件在任务管理器的应用程序没有,在进程结束它,它照样运行,如winamp。
答:在任务管理器勾选--显示所有用户的进程
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 重定向和转发的区别 的文章

 

随机推荐