Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Windows.Movefile does not work sometimes


This question is answered. Helpful answers available: 0. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Sep 11, 2014 1:50 PM Last Post By: Hubert Anemaat
Hubert Anemaat

Posts: 9
Registered: 9/26/02
Windows.Movefile does not work sometimes  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 10, 2014 3:15 AM
Hello,

On some Windows 7 PC's the command:

Windows.MoveFile( PChar( 'C:\users\' + UserName + '\AppData\Local\VirtualStore\Program Files (x86)' + SetupFile),
PChar( 'C:\Program Files (x86)\' + SetupFile))

does not work. On other Windows 7 PC's this works. What is the difference between the two ?

Windows does not return any warning. It is no problem to move the file manually in the Explorer.

How can this problem be solved so that this command works or, if not allowed, the users gets an error message ?

Thanks,
Hubert
Tom Brunberg

Posts: 329
Registered: 12/27/04
Re: Windows.Movefile does not work sometimes
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 10, 2014 4:27 AM   in response to: Hubert Anemaat in response to: Hubert Anemaat
Hubert Anemaat wrote:

Hello,

On some Windows 7 PC's the command:

Windows.MoveFile( PChar( 'C:\users\' + UserName + '\AppData\Local\VirtualStore\Program Files
(x86)' + SetupFile), PChar( 'C:\Program Files (x86)\' +
SetupFile))

does not work. On other Windows 7 PC's this works. What is the difference between the two ?

Hubert,
Isn't it a little bit too much to expect somebody here to know the difference between the two PC's?
Nevertheless, I'm going to guess that one is 32 bit and other is 64 bit?

Windows does not return any warning. It is no problem to move the file manually in the Explorer.

But you are not checking the result of MoveFile!
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365239(v=vs.85).aspx
"
Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call
GetLastError.
"
In Delphi the return value is converted to BOOL.
GetLastError:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms679360(v=vs.85).aspx
And the list of error codes:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx

Try:
var
  err: DWORD;
  ...
  if not Windows.MoveFile(.... ) then
    err := GetLastError;

--
Tom Brunberg
firstname.lastname@welho.com

Hubert Anemaat

Posts: 9
Registered: 9/26/02
Re: Windows.Movefile does not work sometimes  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 11, 2014 1:49 PM   in response to: Tom Brunberg in response to: Tom Brunberg
Thanks,
Hubert
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Windows.Movefile does not work sometimes
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 10, 2014 12:17 PM   in response to: Hubert Anemaat in response to: Hubert Anemaat
Hubert wrote:

On some Windows 7 PC's the command:

Windows.MoveFile( PChar( 'C:\users\' + UserName +
'\AppData\Local\VirtualStore\Program Files (x86)' + SetupFile),
PChar( 'C:\Program Files (x86)\' + SetupFile))

does not work.

First, the RTL has a RenameFile() method in the SysUtils unit that uses MoveFile()
internally, so that will make your code easier to understand what the "move"
is actually doing.

Second, don't hard-code system-defined folder paths like that. Ask the OS
where those folders are actually located. For example:

var
  AppData, ProgramFiles: array[0..MAX_PATH] of Char;
begin
  if Succeeded(SHGetFolderPath(Handle, CSIDL_LOCAL_APPDATA, 0, SHGFP_TYPE_CURRENT, 
AppData)) and
    Succeeded(SHGetFolderPath(Handle, CSIDL_PROGRAM_FILESX86, 0, SHGFP_TYPE_CURRENT, 
ProgramFiles)) then
  begin
    RenameFile(IncludeTrailingPathDelimiter(AppData) + 'VirtualStore\Program 
Files (x86)\' + SetupFile, IncludeTrailingPathDelimiter(ProgramFiles) + SetupFile);
    ...
  end;
end;


Alternatively:

var
  AppData, ProgramFiles: PWSTR;
begin
  if Succeeded(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, 0, @AppData)) 
then
  try
    if Succeeded(SHGetKnownFolderPath(FOLDERID_ProgramFilesX86, 0, 0, @ProgramFiles)) 
then
    try
      RenameFile(IncludeTrailingPathDelimiter(AppData) + 'VirtualStore\Program 
Files (x86)\' + SetupFile, IncludeTrailingPathDelimiter(ProgramFiles) + SetupFile);
      ...
    finally
      CoTaskMemFree(ProgramFiles);
    end;
  finally
    CoTaskMemFree(AppData);
  end;
end;


If you need to query the VirtualStore path for a user other than the current
user, SHGetFolderPath() and SHGetKnownFolderPath() let you specify a user
access token for that very purpose.

Third, the "Program Files" and "Program Files (x86)" folders are protected,
only administrators can write to them. Make sure your app is running elevated
with admin permissions.

Last, if your app is not manifested with a requestedExecutionLevel value,
trying to write to "Program Files" or "Program Files (x86)" will redirect
to the VirtualStore. Obviously, you cannot move a file onto itself. Make
sure your app is manifested properly.

--
Remy Lebeau (TeamB)
Hubert Anemaat

Posts: 9
Registered: 9/26/02
Re: Windows.Movefile does not work sometimes  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 11, 2014 1:50 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks Remy.

Hubert
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02