Article View: borland.public.cppbuilder.nativeapi
Article #4769Re: How to unmap file ?
From: Ed Mulroy
Date: Wed, 30 Nov 2005 16:12
Date: Wed, 30 Nov 2005 16:12
82 lines
3703 bytes
3703 bytes
I have concerns about doing it that way. One item is the race condition.
The gross way to have a program delete itself is to create a batch file
which delays then does the delete, execute the command interpreter with the
batch file as a command line argument and then end the program. That
"usually" works. A variant is to create an executable which delays and then
deletes and execute it but antivirus programs may not be happy with this
technique.
To have a program delete itself is a bit difficult. The 2 part article at
these links discusses the issues and how to do it:
http://www.microsoft.com/msj/archive/sf9c.aspx
http://www.microsoft.com/msj/0198/win320198.aspx
. Ed
> Juris wrote in message
> news:438db468@newsgroups.borland.com...
>
> Delete self - sample.
>
> ////////////////////////////////////////////////////////
>
> #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 = 0;
> 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;
> }
Technical Details
Message-ID:
<438dcf60$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!feed5.newsreader.com!newsreader.com!news-out.nntp.be!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<4344fc1d$1@newsgroups.borland.com> <438db468@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,dbb65edaa3343676 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Ed Mulroy" <dont_email_me@bitbuc.ket> Newsgroups: borland.public.cppbuilder.nativeapi References: <4344fc1d$1@newsgroups.borland.com> <438db468@newsgroups.borland.com> Subject: Re: How to unmap file ? Date: Wed, 30 Nov 2005 11:12:58 -0500 Lines: 84 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 X-RFC2646: Format=Flowed; Response NNTP-Posting-Host: 69.134.170.196 Message-ID: <438dcf60$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1133367136 69.134.170.196 (30 Nov 2005 08:12:16 -0700) X-Authenticated-User: edmulroy Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!feed5.newsreader.com!newsreader.com!news-out.nntp.be!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail