由于上班不能被BOSS看到在上网,于是自己写了一个能关闭IE的程序,这样就能用一个快捷键及时关掉所有IE。下面是如何关掉IE的,都是GOOGLE+MSDN+摸索……
void
CloseIEProcess(
void
)
{
HANDLE SnapshotHandle = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, TH32CS_SNAPALL );
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof (PROCESSENTRY32);
if ( SnapshotHandle != INVALID_HANDLE_VALUE )
{
if ( Process32First( SnapshotHandle, & pe32 ) )
{
do
{
AnsiString ExeFileName = AnsiString( pe32.szExeFile );
if ( ExeFileName.UpperCase() == " IEXPLORE.EXE " )
{
HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, false , pe32.th32ProcessID );
if ( hProcess )
{
unsigned long nExitCode;
if ( GetExitCodeProcess( hProcess, & nExitCode ) )
TerminateProcess( hProcess, nExitCode );
}
else
{
LPSTR lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language
(LPSTR) & lpMsgBuf,
0 ,
NULL
);
// Display the string.
MessageBox( NULL, lpMsgBuf, " Error " , MB_OK | MB_ICONSTOP );
// Free the buffer.
LocalFree( lpMsgBuf );
}
CloseHandle( hProcess );
}
} while ( Process32Next( SnapshotHandle, & pe32 ) );
}
}
CloseHandle( SnapshotHandle );
}
{
HANDLE SnapshotHandle = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, TH32CS_SNAPALL );
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof (PROCESSENTRY32);
if ( SnapshotHandle != INVALID_HANDLE_VALUE )
{
if ( Process32First( SnapshotHandle, & pe32 ) )
{
do
{
AnsiString ExeFileName = AnsiString( pe32.szExeFile );
if ( ExeFileName.UpperCase() == " IEXPLORE.EXE " )
{
HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, false , pe32.th32ProcessID );
if ( hProcess )
{
unsigned long nExitCode;
if ( GetExitCodeProcess( hProcess, & nExitCode ) )
TerminateProcess( hProcess, nExitCode );
}
else
{
LPSTR lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language
(LPSTR) & lpMsgBuf,
0 ,
NULL
);
// Display the string.
MessageBox( NULL, lpMsgBuf, " Error " , MB_OK | MB_ICONSTOP );
// Free the buffer.
LocalFree( lpMsgBuf );
}
CloseHandle( hProcess );
}
} while ( Process32Next( SnapshotHandle, & pe32 ) );
}
}
CloseHandle( SnapshotHandle );
}
如果是通过运行别人的账号上网的(使用Run As…的那种),这样关闭IE会出现“存取被拒”的错误,需要提升自己程序的权限,加入DEBUG权限,才能关闭IE。以下函数在程序开始时执行一次即可,调用为 EnablePrivilege( SE_DEBUG_NAME );
bool
EnablePrivilege( LPCTSTR szPrivName )
{
HANDLE hToken;
if ( OpenProcessToken( GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
& hToken ) )
{
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1 ;
LookupPrivilegeValue( 0 , szPrivName, & tp.Privileges[ 0 ].Luid );
tp.Privileges[ 0 ].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges( hToken, false , & tp, sizeof (tp), 0 , 0 );
}
CloseHandle( hToken );
return ( GetLastError() == ERROR_SUCCESS );
}
{
HANDLE hToken;
if ( OpenProcessToken( GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
& hToken ) )
{
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1 ;
LookupPrivilegeValue( 0 , szPrivName, & tp.Privileges[ 0 ].Luid );
tp.Privileges[ 0 ].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges( hToken, false , & tp, sizeof (tp), 0 , 0 );
}
CloseHandle( hToken );
return ( GetLastError() == ERROR_SUCCESS );
}