C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 팁&트릭
C++Builder Programming Tip&Tricks
[593] 스스로 지워지는 프로그램을 만드려면
김태선 [jsdkts] 8559 읽음    2006-04-16 10:17
// 제프 리처가 제안했던 방법
// 이 함수를 종료 바로 직전에 호출하면 자동으로 실행파일이 삭제되게 된다.
// C++빌더 및 VC++에서 바로 적용가능.

#define DELUNSETUPBAT    __TEXT("\\DelUS.bat")

void     DeleteSelf()
{
    HANDLE hfile;
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    hfile = CreateFile(DELUNSETUPBAT, GENERIC_WRITE, 0, NULL,
        CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
    if (hfile != INVALID_HANDLE_VALUE)
    {
        TCHAR szBatFile[1000];
        TCHAR szUnsetupPathname[_MAX_PATH];
        TCHAR szUnsetupPath[_MAX_PATH];
        DWORD dwNumberOfBytesWritten;

        // Get the full pathname of our executable file.
        GetModuleFileName(NULL, szUnsetupPathname, _MAX_PATH);

        // Get the path of the executable file (without the filename)
        lstrcpy(szUnsetupPath, szUnsetupPathname);
        *strrchr(szUnsetupPath, __TEXT('\\')) = 0;    // Chop off the name

        // Construct the lines for the batch file.
        wsprintf(szBatFile,
            __TEXT(":Repeat\r\n")
            __TEXT("del \"%s\"\r\n")
            __TEXT("if exist \"%s\" goto Repeat\r\n")
            __TEXT("del \"%s\"\r\n"),
            szUnsetupPathname, szUnsetupPathname, DELUNSETUPBAT);

        // Write the batch file and close it.
        WriteFile(hfile, szBatFile, lstrlen(szBatFile) * sizeof(TCHAR),
            &dwNumberOfBytesWritten, NULL);
        CloseHandle(hfile);

        // Get ready to spawn the batch file we just created.
        ZeroMemory(&si, sizeof(si));
        si.cb = sizeof(si);

        // We want its console window to be invisible to the user.
        si.dwFlags = STARTF_USESHOWWINDOW;
        si.wShowWindow = SW_HIDE;

        // Spawn the batch file with low-priority and suspended.
        if (CreateProcess(NULL, DELUNSETUPBAT, NULL, NULL, FALSE,
            CREATE_SUSPENDED | IDLE_PRIORITY_CLASS, NULL,
            __TEXT("\\"), &si, &pi))
        {
           
            // Lower the batch file's priority even more.
            SetThreadPriority(pi.hThread, THREAD_PRIORITY_IDLE);
           
            // Raise our priority so that we terminate as quickly as possible.
            SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
            SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
           
            // Allow the batch file to run and clean-up our handles.
            CloseHandle(pi.hProcess);
            ResumeThread(pi.hThread);
            // We want to terminate right away now so that we can be deleted
            CloseHandle(pi.hThread);
        }
    }
}

+ -

관련 글 리스트
593 스스로 지워지는 프로그램을 만드려면 김태선 8559 2006/04/16
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.