Be sure to create a new SPSite which is the root security reference while you're impersonating. Here's a small code snippet:
SPWeb web = SPContext.Current.Web;
SPUser user = web.CurrentUser; // the calling user
// Uses the SHAREPOINT\system creds with the SPUser's identity reference of user
SPSecurity.RunWithElevatedPrivileges(delegate() {
// Gets a new security context using SHAREPOINT\system
using (SPSite site = new SPSite(this.Page.Request.Url.ToString())) {
using (SPWeb thisWeb = site.OpenWeb()) {
thisWeb.AllowUnsafeUpdates = true;
SPList theList = thisWeb.Lists[listName];
SPListItem record = theList.Items.Add();
record["User"] = user; // calling user
record.Update(); // uses SHAREPOINT\system
}
}
});
For more info, read my post @ http://daniellarson.spaces.live.com/blog/cns!D3543C5837291E93!927.entry
Also, you should dispose of the SPSite object created within the RunWithElevatedPrivileges() before exiting the scope, because that SPSite will still have the SHAREPOINT\system identity even outside of the RunWithElevatedPrivileges() scope.
RunWithElevatedPrivileges() has no effect when running in a standalone exe.