본문 바로가기
Code review

[WinApi]self deleting executables

by cafrisun 2009. 5. 27.
 

스스로 실행후 삭제되는 기능이 필요해서 여러가지를 찾아보다 발견한 코드.
한마디로 훌륭하다. 우선순위를 바꿔준다니..  대단하십니다~

출처 : http://www.codeguru.com/Cpp/W-P/win32/article.php/c4533

#include <windows.h>
#include <shlobj.h>

BOOL SelfDelete()
{
        SHELLEXECUTEINFO sei;

        TCHAR szModule [MAX_PATH], szComspec[MAX_PATH], szParams[MAX_PATH];

        // get file path names:
        if((GetModuleFileName(0,szModule,MAX_PATH)!=0) &&
                (GetShortPathName(szModule,szModule,MAX_PATH)!=0) &&
                (GetEnvironmentVariable("COMSPEC",szComspec,MAX_PATH)!=0))
        {
                // set command shell parameters
                lstrcpy(szParams,"/c del ");
                lstrcat(szParams, szModule);
                lstrcat(szParams, " > nul");

                // set struct members
                sei.cbSize       = sizeof(sei);
                sei.hwnd         = 0;
                sei.lpVerb       = "Open";
                sei.lpFile       = szComspec;
                sei.lpParameters = szParams;
                sei.lpDirectory  = NULL;        // 0 -> NULL
                sei.nShow        = SW_HIDE;
                sei.fMask        = SEE_MASK_NOCLOSEPROCESS;

                // increase resource allocation to program
                SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
                SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);

                // invoke command shell
                if(ShellExecuteEx(&sei))
                {
                        // suppress command shell process until program exits
                        SetPriorityClass(sei.hProcess,IDLE_PRIORITY_CLASS);
                        SetProcessPriorityBoost(sei.hProcess,TRUE);

                        // notify explorer shell of deletion
                        SHChangeNotify(SHCNE_DELETE,SHCNF_PATH,szModule,0);
                        return TRUE;
                }
                else // if error, normalize allocation
                {
                        SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
                        SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
                }
        }
        return FALSE;
}

반응형

'Code review' 카테고리의 다른 글

[TCP/IP] hello world server 예제  (0) 2009.05.27
[WinApi]Dialog base start 2  (0) 2009.05.27
[C]time functions  (0) 2009.05.27