很巧妙的进程防杀方法[秋镇菜原创]

类别:编程语言 点击:0 评论:0 推荐:
很巧妙的进程防杀方法[秋镇菜原创] document.title="很巧妙的进程防杀方法[秋镇菜原创] - "+document.title 刚刚学内核对象,想写个可以防杀的进程,但其他方法太高级,
本菜鸟不感高攀,想了几天,想到一个很本的办法,不正确的
方还请高手指点一下.
程序运行两个事例,每个实例互相监视另外的实例是否存在,
如果不存在,就运行一个.
代码:
// test_process.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "tchar.h"
#include "iostream.h"
#include "windows.h"

LPCTSTR lpszMutex1 = _T("mutex1");
LPCTSTR lpszMutex2 = _T("mutex2");

DWORD WINAPI Thread1(LPARAM lParam);
DWORD WINAPI Thread2(LPARAM lParam);

int main(int argc, char* argv[])
{
    HANDLE hMutex1 = CreateMutex(NULL, FALSE, lpszMutex1);
    if (hMutex1 != NULL)
    if (GetLastError() == ERROR_ALREADY_EXISTS)
    {
        CloseHandle(hMutex1);
        cout << lpszMutex1 << " already exist." << endl;
        HANDLE hMutex2 = CreateMutex(NULL, FALSE, lpszMutex2);
        if (hMutex2 != NULL)
        if (GetLastError() == ERROR_ALREADY_EXISTS)
        {
            cout << lpszMutex2 << " already exist." << endl;
            return 1;
        }
    }
    DWORD dwThreadId;
    HANDLE hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Thread1,NULL,0,&dwThreadId);
    CloseHandle(hThread);
    while (1)
    Sleep(1);
    return 0;
}
DWORD WINAPI Thread1(LPARAM lParam)
{
    HANDLE hMutex;
    while (1)
    {
        hMutex = OpenMutex(MUTEX_ALL_ACCESS, TRUE, lpszMutex1);
        if (! hMutex)
        {
            STARTUPINFO si = { sizeof STARTUPINFO };
            PROCESS_INFORMATION pi;
            TCHAR szBuf[MAX_PATH];
            GetModuleFileName(GetModuleHandle(NULL), szBuf, MAX_PATH);
            CreateProcess(szBuf, NULL, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
            CloseHandle(pi.hThread);
            CloseHandle(pi.hProcess);
        }
        CloseHandle(hMutex);
        hMutex = OpenMutex(MUTEX_ALL_ACCESS, TRUE, lpszMutex2);
        if (! hMutex)
        {
            cout << "mutex 2 created." << endl;
            STARTUPINFO si = { sizeof STARTUPINFO };
            PROCESS_INFORMATION pi;
            TCHAR szBuf[MAX_PATH];
            GetModuleFileName(GetModuleHandle(NULL), szBuf, MAX_PATH);
            CreateProcess(szBuf, NULL, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
            CloseHandle(pi.hThread);
            CloseHandle(pi.hProcess);
        }
        CloseHandle(hMutex);
        cout << "Thread      ..." << endl;
        Sleep(1);
    }
    return 1;
}

本文地址:http://com.8s8s.com/it/it21860.htm