Conversation

Impersonate user from service: SetTokenInformation err 998 "Invalidaccess

(overview of excerpts from articles in conversation)

Newsgroup: embarcadero.public.delphi.nativeapi

# Lines
wrote on 27-Jan-2010:

I want to set a HKCU registry setting from my service app; I know a Windows user is logged on at that moment.
I used the sample code at http://www.delphifaq.com/faq/delphi_windows_API/f663.shtml to impersonate the logged on user.
I get error 998 "Invalid access to memory location" at the call to SetTokenInformation
I failed to contact the original author.
The original call to DuplicateTokenEx used params SecurityIdentification and TokenPrimary, I also tried SecurityImpersonation and TokenImpersonation (see code)
245
  
Christian Wimmer replied on 27-Jan-2010:

That should be
  @dwSessionId instead of Pointer(dwSessionId)
  in SetTokenInformation
  Christian Wimmer
  
4
    
Jan Doggen replied on 28-Jan-2010:

(snip)

That removed the error thanks.
    It still does not work though, whether I call
    DuplicateTokenEx(hPToken,
    MAXIMUM_ALLOWED,
    Nil,
65
  
Christian Wimmer replied on 28-Jan-2010:

You should use RegOpenCurrentUser instead of RegOpenKey(Ex) (TRegistry).
  These functions don't work with impersonation.
  Christian Wimmer
  
  
5
    
Jan Doggen replied on 16-Feb-2010:

Thanks, it works now.
    Here is the full code fromm a test service:
    This is what I wanted to do:
    procedure TWorkerThread.SetControlPanelAccess(Access: Boolean);
    const
769
  
Jan Doggen replied on 16-Feb-2010:

As noted at the top of the xbwLogion unit:
  We only use some of the code, so the rest is UNTESTED.
  If anyone uses this code and improves/corrects it I would like to get notified.
  Thansk
  Jan
8
    
Christian Wimmer replied on 27-Feb-2010:

Jan Doggen schrieb:
    (snip)

Sorry, I have to tell you: this code looks horrible and I advise not to
    use it. There are a lot of missing error checks (WTSQueryUserToken),
    some error checks are done but there is only a boolean value set to true
    (bError) and then the processing is continued,
41
FYI: Phrase searches are enclosed in either single or double quotes
 
 
Originally created by
Tamarack Associates
Thu, 28 Mar 2024 17:35:10 UTC
Copyright © 2009-2024
HREF Tools Corp.