프로그램을 업데이트 하는데 업데이트 프로그램을 업데이트 해야 하는 그러한 상황이 발생하기도 한다. 일반적으로 업데이트라 함은, 메인 프로세스를 Kill하고, 해당 프로세스의 실행파일을 덮어쓰고 패치후에 프로세스를 다시 실행하면 되지만 업데이트 프로그램은 어떻게 하느냐?
그리고 디바이스 드라이버 같이 항상 동작중인 파일은 어떻게 변경할까?
 이를 해결하기 위한 함수가 MoveFileEx이다. 이 함수를 이용하면 OS가 재부팅하는 시점에 파일을 지우거나 새로 덮어쓸 수 있게 된다. 자세한 사용법은 어김없이 MSDN에서.

MoveFileEx

The MoveFileEx function moves an existing file or directory.

The MoveFileWithProgress function is equivalent to the MoveFileEx function, except that MoveFileWithProgress allows you to provide a callback function that receives progress notifications.

BOOL MoveFileEx(
  LPCTSTR lpExistingFileName,
  LPCTSTR lpNewFileName,
  DWORD dwFlags
);

Parameters

lpExistingFileName
[in] Pointer to a null-terminated string that names an existing file or directory on the local computer.

If dwFlags specifies MOVEFILE_DELAY_UNTIL_REBOOT, the file cannot exist on a remote share because delayed operations are performed before the network is available.

In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File

Windows 2000:  If you prepend the filename with "\\?", you cannot also specify the MOVEFILE_DELAY_UNTIL_REBOOT flag for dwFlags.
lpNewFileName
[in] Pointer to a null-terminated string that specifies the new name of lpExistingFileName on the local computer.

When moving a file, the destination can be on a different file system or volume. If the destination is on another drive, you must set the MOVEFILE_COPY_ALLOWED flag in dwFlags.

When moving a directory, the destination must be on the same drive.

If dwFlags specifies MOVEFILE_DELAY_UNTIL_REBOOT and lpNewFileName is NULL, MoveFileEx registers the lpExistingFileName file to be deleted when the system restarts. If lpExistingFileName refers to a directory, the system removes the directory at restart only if the directory is empty.

dwFlags
[in] This parameter can be one or more of the following values.
Value Meaning
MOVEFILE_COPY_ALLOWED If the file is to be moved to a different volume, the function simulates the move by using the CopyFile and DeleteFile functions.

This value cannot be used with MOVEFILE_DELAY_UNTIL_REBOOT.

MOVEFILE_CREATE_HARDLINK Reserved for future use.
MOVEFILE_DELAY_UNTIL_REBOOT The system does not move the file until the operating system is restarted. The system moves the file immediately after AUTOCHK is executed, but before creating any paging files. Consequently, this parameter enables the function to delete paging files from previous startups.

This value can be used only if the process is in the context of a user who belongs to the administrator group or the LocalSystem account.

This value cannot be used with MOVEFILE_COPY_ALLOWED.

Windows 2000:  If you specify the MOVEFILE_DELAY_UNTIL_REBOOT flag for dwFlags, you cannot also prepend the filename specified by lpExistingFileName with "\\?".
MOVEFILE_FAIL_IF_NOT_TRACKABLE The function fails if the source file is a link source, but the file cannot be tracked after the move. This situation can occur if the destination is a volume formatted with the FAT file system.
Windows NT:  This value is not supported.
MOVEFILE_REPLACE_EXISTING If a file named lpNewFileName exists, the function replaces its contents with the contents of the lpExistingFileName file.

This value cannot be used if lpNewFileName or lpExistingFileName names a directory.

MOVEFILE_WRITE_THROUGH The function does not return until the file has actually been moved on the disk.

Setting this value guarantees that a move performed as a copy and delete operation is flushed to disk before the function returns. The flush occurs at the end of the copy operation.

This value has no effect if MOVEFILE_DELAY_UNTIL_REBOOT is set.

Return Values

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.

Remarks

If the dwFlags parameter specifies MOVEFILE_DELAY_UNTIL_REBOOT, MoveFileEx fails if it cannot access the registry. The function stores the locations of the files to be renamed at restart in the following registry value:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations

This registry value is of type REG_MULTI_SZ. Each rename operation stores the following pair of NULL-terminated strings:

szDstFile\0\0
szSrcFile\0szDstFile\0\0

The system uses these registry entries to complete the operations at restart in the same order that they were issued. For example, the following code fragment creates registry entries that delete szDstFile and rename szSrcFile to be szDstFile at restart:

[C++]

MoveFileEx(szDstFile, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
MoveFileEx(szSrcFile, szDstFile, MOVEFILE_DELAY_UNTIL_REBOOT);

Because the actual move and deletion operations specified with the MOVEFILE_DELAY_UNTIL_REBOOT flag take place after the calling application has ceased running, the return value cannot reflect success or failure in moving or deleting the file. Rather, it reflects success or failure in placing the appropriate entries into the registry.

The system deletes a directory tagged for deletion with the MOVEFILE_DELAY_UNTIL_REBOOT flag only if it is empty. To ensure deletion of directories, move or delete all files from the directory before attempting to delete it. Files may be in the directory at boot time, but they must be deleted or moved before the system can delete the directory.

The move and deletion operations are carried out at boot time in the same order they are specified in the calling application. To delete a directory that has files in it at boot time, first delete the files.

If a file is moved across volumes, MoveFileEx does not move the security descriptor with the file. The file will be assigned the default security descriptor in the destination directory.

The MoveFileEx function coordinates its operation with the link tracking service, so link sources can be tracked as they are moved.

Windows Me/98/95:  The MoveFileEx function is not supported. To rename or delete a file at restart, use the following procedure.

To rename or delete a file on Windows Me/98/95

  1. Check for the existence of the WININIT.INI file in the Windows directory.
  2. If WININIT.INI exists, open it and add new entries to the existing [rename] section. If the file does not exist, create the file and create a [rename] section.
  3. Add lines of the following format to the [rename] section:
    DestinationFileName=SourceFileName

    Both DestinationFileName and SourceFileName must be short file names. To delete a file, use NUL as the value for DestinationFileName.

The system processes WININIT.INI during system boot. After WININIT.INI has been processed, the system names it WININIT.BAK.

To delete or rename a file, you must have either delete permission on the file or delete child permission in the parent directory. If you set up a directory with all access except delete and delete child and the ACLs of new files are inherited, then you should be able to create a file without being able to delete it. However, you can then create a file, and you will get all the access you request on the handle returned to you at the time you create the file. If you requested delete permission at the time you created the file, you could delete or rename the file with that handle but not with any other.

Creative Commons License
Creative Commons License

'Programming > Code helper' 카테고리의 다른 글

InterlockedExchange - Synchronization Function.  (0) 2008/09/30
MoveFileEx라는 특이한 함수  (0) 2008/08/25
GINA에 대해서...  (0) 2008/08/14
WinDbg!!  (0) 2008/07/28

TRACKBACK http://blog.niu.kr/trackback/35 관련글 쓰기

댓글을 달아 주세요

<PREV 1 ... 28 29 30 31 32 33 34 35 36 ... 54 NEXT>