Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2707665
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2009-10-13 22:56:27

主控程序,能resume/suspend thread
控制dos程序的打印输出(暂停或继续)
 
#include
#include
#include
 
DWORD fGetPID( char *szProcessName );
typedef HANDLE (WINAPI *OPENTHPROC)(DWORD, BOOL, DWORD);
OPENTHPROC openThread;
void InitProcs()
{
 HINSTANCE hKernel32 = GetModuleHandle("kernel32.dll");
 openThread = (OPENTHPROC)GetProcAddress(hKernel32, "OpenThread");
 //HANDLE (WINAPI *OPENTHPROC)(DWORD, BOOL, DWORD) = (OPENTHPROC)GetProcAddress(hKernel32, "OpenThread");
 FreeLibrary(hKernel32);
}
void ActUpponThread(DWORD dwParentPid, BOOL bThread)
{
 HANDLE hThread, hSnapshot;
 THREADENTRY32 te32;
 hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
 te32.dwSize = sizeof(THREADENTRY32);
 if(Thread32First(hSnapshot, &te32) != TRUE)
  MessageBox(NULL, "Thread32First Failed!", "DEBUG", MB_ICONINFORMATION | MB_OK);
 do
 {
  if(te32.th32OwnerProcessID == dwParentPid)
  {
   hThread = openThread(THREAD_ALL_ACCESS, TRUE, te32.th32ThreadID);
   if(bThread)
   {
    MessageBox(NULL, "SuspendThread", "DEBUG", MB_ICONINFORMATION | MB_OK);
    printf("SuspendThread = \n");
    SuspendThread(hThread);
   }
   else
   {
    MessageBox(NULL, "ResumeThread", "DEBUG", MB_ICONINFORMATION | MB_OK);
    printf("ResumeThread =\n");
    ResumeThread(hThread);
   }
   CloseHandle(hThread);
  }
 } while(Thread32Next(hSnapshot, &te32));
 CloseHandle(hSnapshot);
}
int main(int argc, char *argv[])
{
 DWORD dwParentPid;
 HWND hWnd;
 char wText[100];
 //Sleep(2000);
 hWnd = GetForegroundWindow();
 GetWindowThreadProcessId(hWnd, &dwParentPid);
 dwParentPid = fGetPID( "runProc.exe" );
 InitProcs();
 //GetWindowText(hWnd, wText, 100);
 sprintf(wText, "pid = %d\n", dwParentPid);
 MessageBox(NULL, wText, "DEBUG", MB_ICONINFORMATION | MB_OK);
 while(TRUE)
 {
  if(GetAsyncKeyState(VK_F4))
   ActUpponThread(dwParentPid, TRUE);
  if(GetAsyncKeyState(VK_F5))
   ActUpponThread(dwParentPid, FALSE);
 }
 return 0;
}

/*********************************/

int main3()
{
 DWORD dwPid;
 dwPid = fGetPID( "process.exe" );
 char wText[100];
 sprintf(wText, "pid = %d\n", dwPid);
 MessageBox(NULL, wText, "DEBUG", MB_ICONINFORMATION | MB_OK);
 DebugActiveProcess( dwPid );
 Sleep( 10000 );
 DebugActiveProcessStop( dwPid );
 return 1;
}
DWORD fGetPID( char *szProcessName )
{
 PROCESSENTRY32 pe;
 HANDLE ss;
 DWORD dwRet;
 ss = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
 if( ss ) {
  if( Process32First( ss, &pe ) )
   while( Process32Next( ss, &pe ) )
    if( !strcmp( pe.szExeFile, szProcessName ) ) {
     dwRet = pe.th32ProcessID;
     break;
    }
    CloseHandle( ss );
 }
 return dwRet;
}
 
/***********************************/
// 生成runProc.exe
// runProc.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
int _tmain(int argc, _TCHAR* argv[])
{
 int i=0;
 for(i=0; i<10000; i++)
 {
  printf("index = %d\n", i);
  Sleep(500);
 }
 return 0;
}

 
/********************************************************/
下面类似的代码参考
 
/*++
Module Name:
    ice.c
Abstract:
    This utility "freezes" and "thaws" processes.
Author:
    Michael Wookey 6-Jun-2003 ()
Notes:
    ice.exe [freeze|thaw] pid
Compiler:
    VC7
Build:
    cl ice.c
--*/
#define STRICT
#define WIN32_LEAN_AND_MEAN
#include
//
// The native functions exported from ntdll.
//
typedef LONG ( NTAPI *_NtSuspendProcess )( IN HANDLE ProcessHandle );
typedef LONG ( NTAPI *_NtResumeProcess )( IN HANDLE ProcessHandle );
int main( int argc, char* argv[] )
{
    HANDLE ProcessHandle = 0;
    _NtSuspendProcess NtSuspendProcess = 0;
    _NtResumeProcess NtResumeProcess = 0;
    //
    // Make sure we have enough arguments.
    //
    if( 3 > argc )
    {
        printf( "ice [freeze|thaw] pid\n" );
        return 0;
    }
    //
    // Obtain our function imports.
    //
    NtSuspendProcess = (_NtSuspendProcess)
        GetProcAddress( GetModuleHandle( "ntdll" ), "NtSuspendProcess" );
    NtResumeProcess = (_NtResumeProcess)
        GetProcAddress( GetModuleHandle( "ntdll" ), "NtResumeProcess" );
    //
    // Attempt to open the target process.
    //
    ProcessHandle = OpenProcess( PROCESS_ALL_ACCESS, FALSE, atoi( argv[2] ));
    //
    // Freeze or thaw the process. Note that these alter the process'
    // suspend count, so freezing the process twice will require thawing
    // the process twice to restore.
    //
    if( ! ProcessHandle )
    {
        printf( "Unable to open process id %d\n", atoi( argv[2] ));
    }
    else
    {
        if( ! strcmpi( argv[1], "freeze" ))
        {
            if( NtSuspendProcess )
            {
                NtSuspendProcess( ProcessHandle );
            }
        }
        else if( ! strcmpi( argv[1], "thaw" ))
        {
            if( NtResumeProcess )
            {
                NtResumeProcess( ProcessHandle );
            }
        }
        else
        {
            printf( "ice [freeze|thaw] pid\n" );
        }
    }
    //
    // Close our process handle.
    //
    if( ProcessHandle )
    {
        CloseHandle( ProcessHandle );
    }
    return 0;
}
/* EOF */
阅读(1186) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~