Microsoft KB Archive/195605

From BetaArchive Wiki

HOWTO: Replace a File that Is In Use Under Windows CE

Q195605



The information in this article applies to:


  • Microsoft Win32 Application Programming Interface (API), used with:
    • Microsoft Windows CE for the Handheld PC





SUMMARY

Sometimes an installation program needs to replace a file that is currently being used by the operating system. This is often necessary when a driver is being updated. This article explains how to handle such an update under Windows CE using the MoveFile API.

You should use the method described in this article only if the file is being used the operating system (for example, the file is a .dll or an .exe that houses a Windows CE driver). If you need to replace a file that is being used by another process, you should exit that process as you would normally to release the file so it can be updated.



MORE INFORMATION

Many Windows CE drivers are listed in the registry under the key

HKEY_LOCAL_MACHINE\Drivers\BuiltIn 

Each built-in driver has a subkey that contains various values that the operating system uses when loading the driver. The "Dll" value is a REG_SZ value, which identifies the file that houses the driver. This file is in use as soon as the operating system is started.

NOTE: Under Windows NT, a file that is in use should be replaced with the MoveFileEx() API, which allows the file to be replaced upon a system restart. Under Windows 9x, you can use a WINInit.ini file to replace a file upon system restart. For more information on these methods of replacing files, please see the following article in the Microsoft Knowledge Base:

Q140570 HOWTO: Move Files That Are Currently in Use

Unfortunately, Windows CE does not support the MoveFileEx function or the use of a WINInit.ini file. Furthermore, a call to DeleteFile() fails if the file is in use, and a call to GetLastError() returns error code 5

(ERROR_ACCESS_DENIED)

Under Windows CE, you can use the MoveFile() API to move a file to a different directory, even if the file is in use. The system continues to use the file from its new location so an attempt to delete it after the move still fails. However, once the file has been moved, a replacement file can be stored at the original location. Upon restart of the Windows CE device, the operating system uses the new file and the old file can be successfully deleted.

The following sample code demonstrates how to move a file that is in use to the recycle bin. The program first tries to delete the file with the DeleteFile() API. If this fails, it calls the MoveFile() function to move the file.

Sample Code

   #include <windows.h>

   int WINAPI WinMain(
         HINSTANCE hInstance,
         HINSTANCE hPrevInstance,
         LPTSTR lpCmdLine,
         int nCmdShow) {

      WCHAR szMsg[255];

      if ( DeleteFile( L"\\Windows\\MyDriver.dll" ) ) {

         MessageBox( NULL, L"DeleteFile() succeeded.",
               L"Information", MB_ICONINFORMATION );

      } else {

         wsprintf( szMsg, L"DeleteFile() failed.  Error %d\n",
               GetLastError() );
         MessageBox( NULL, szMsg, L"Error", MB_ICONEXCLAMATION );

         if ( MoveFile( L"\\Windows\\MyDriver.dll",
                        L"\\Recycled\\MyDriver.old" ) ) {

            MessageBox( NULL, L"MoveFile() succeeded.",
                  L"Information", MB_ICONINFORMATION );

         } else {

            wsprintf( szMsg, L"MoveFile() failed.  Error %d\n",
                  GetLastError() );
            MessageBox( NULL, szMsg, L"Error", MB_ICONEXCLAMATION );
         }
      }

      return 0;
   } 

NOTE: When a file that is in use is moved to the Recycle Bin, its name becomes unreadable. It usually shows up in Windows Explorer as nonsense characters. If you want to open a file by name after moving it, you should move it to a temporary directory. Furthermore, any attempt to empty the Recycle Bin does not successfully remove a file that is in use. Once the device has been restarted, you can successfully empty the Recycle Bin.

Additional query words:

Keywords : kbAPI kbDLL kbKernBase kbOSWinCE100 kbOSWinCE200
Issue type : kbhowto
Technology : kbAudDeveloper kbWin32sSearch kbWin32API


Last Reviewed: February 9, 2000
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.