BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to unmap file ?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API)
View previous topic :: View next topic  
Author Message
Juris
Guest





PostPosted: Thu Oct 06, 2005 10:32 am    Post subject: How to unmap file ? Reply with quote



Good day!

When program loads into memory - windows maps(memory mapping) this file.
After that there is no chance to apdate this file.

How can I unmap this file ?

B.R. Juris


Back to top
Alan Bellingham
Guest





PostPosted: Thu Oct 06, 2005 10:51 am    Post subject: Re: How to unmap file ? Reply with quote



"Juris" <juris.vaivods (AT) inbox (DOT) lv> wrote:

Quote:
When program loads into memory - windows maps(memory mapping) this file.
After that there is no chance to apdate this file.

How can I unmap this file ?

Simple - unload the program.

If that doesn't answer your real problem, that's because you didn't ask
the real question.

Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK

Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Thu Oct 06, 2005 11:23 am    Post subject: Re: How to unmap file ? Reply with quote



Juris wrote:

Quote:
When program loads into memory - windows maps(memory mapping) this
file. After that there is no chance to apdate this file.

How can I unmap this file ?

You can't without unloading the application. The point of memory
mapping an executable is to allow it to be loaded on demand rather than
wasting RAM holding executable code that is never called. The point of
locking it is to prevent the executable code changing while it is
running.

Fundamentally you are trying to do something that is inadvisable -
modify a running executable. There are some techniques and situations
where this is a viable and neccessary activity but those are pretty
rare.

I suspect you are writing an installer or installation support tool. In
that case the only thing you can do is write a program to unload the
target application, apply the update, then relaunch the target
application.

I believe that Windows supports the concept of 'copying at bootstrap
time' for those executables that can't be shutdown and restarted
without a reboot.

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html

Back to top
Juris
Guest





PostPosted: Wed Nov 30, 2005 2:22 pm    Post subject: Re: How to unmap file ? Reply with quote

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;
}


Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Wed Nov 30, 2005 3:04 pm    Post subject: Re: How to unmap file ? Reply with quote

Juris wrote:

Quote:
Delete self - sample.

Yeah that's doing what we said you'd have to. It creates an external
process to delete itself then it closes itself down before the created
process gets around to attempting the delete.

It seems to be doing this through a slightly underhand method though.
It creates the process at a very low priority after boosting itself to
a very high priority. IOW it's setting up a critical race and heavily
biasing the race in favour of itself. Whether this bias is enough to
guarantee successful deletion or just make it very likely I don't know.

I think it would be better and simpler to avoid all the clever trickery
and just have an external process that is responsible for (re)launching
the one you want to delete.

This is actually a very well known and documented technique. The
application the user invokes is just a wrapper and it takes care of
launching the real application and waiting for it to exit. When the
real application exits it signals the wrapper what to do next.

This is used for the purpose you seem to need and also to relaunch
processes if/when they abend.

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html

Back to top
Ed Mulroy
Guest





PostPosted: Wed Nov 30, 2005 4:12 pm    Post subject: Re: How to unmap file ? Reply with quote

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

Quote:
Juris wrote in message
news:438db468 (AT) newsgroups (DOT) borland.com...

Delete self - sample.

////////////////////////////////////////////////////////

#include <windows.h
#include
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;
}



Back to top
Bob Gonder
Guest





PostPosted: Wed Nov 30, 2005 7:22 pm    Post subject: Re: How to unmap file ? Reply with quote

Ed Mulroy wrote:

Quote:
I have concerns about doing it that way. One item is the race condition.

Yep.

Quote:
The gross way to have a program delete itself is to create a batch file

Yes quite gross.

Quote:
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.

NAV doesn't complain, but I'm replacing, not patching, the exe with an
update..

A second program that takes an ascii PID can do quite well.

if( argc > 3 ) /* if launched, wait for launcher to quit */
{
ProcessID = atol(argv[3]);
OldProcess = OpenProcess(PROCESS_QUERY_INFORMATION,
FALSE, ProcessID);
if( OldProcess == NULL )
{
MessageBox( 0, "Did not aquire old process handle",
"Failure!",MB_OK|MB_ICONSTOP|MB_TOPMOST);
return 9;
};
WaitForSingleObject(OldProcess, INFINITE);
CloseHandle(OldProcess);
Sleep(4000); // give Windows more time to release
/* delete/update the original exe */
{ ... }
};

The origianl exe launches the updater with its ASCII PID as a
parameter. To be generic, I also pass the original exe name, and the
name of the replacement file.



Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Thu Dec 01, 2005 9:03 am    Post subject: Re: How to unmap file ? Reply with quote

Bob Gonder wrote:

Quote:
A second program that takes an ascii PID can do quite well.

Lol, reminds me of an old DOS program I wrote. That passed a memory
address to its children on the command line. A primitive form of shared
memory :)

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.