Knowledge Base

PRB: Writing to Resources May Change Last Error

Article ID: 137289

Article Last Modified on 7/11/2005


APPLIES TO


This article was previously published under Q137289

SYMPTOMS

When writing to an application's resources, the value returned by GetLastError may change. This behavior occurs in Windows 95, Windows 98 and Windows 2000, but not in Windows NT.

CAUSE

When an application writes to its own resources, a page fault occurs, which is handled internally by the kernel. The handling of this page fault may change the value returned by GetLastError.

RESOLUTION

To avoid this problem:
  1. Don't write to your own resources. The pointer returned by LockResource should be treated as read-only. This was always the intention, however, writing to your own resources happens to work on Windows NT.
  2. If you really need to write to your own resource, do not rely on the last error being preserved. If the value returned by GetLastError is important to you, call GetLastError before writing to your own resource. Then call SetLastError to restore the value after you are finished.

STATUS

This behavior is by design.

MORE INFORMATION

It is legitimate for an API to change the last error even when it is successful.

Keywords: kbprb kbresource KB137289