Re: Impersonate Issues to note

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




"Tony Proctor" <tony_proctor@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:%2335SAP0OHHA.140@xxxxxxxxxxxxxxxxxxxxxxx
I've been trying to use ImpersonateSelf here, and ran into a few problems.
I
just want to jot them down in case anyone else finds them useful.

To start with, the API TextViewer declaration is wrong. It declares it as:

Private Declare Function ImpersonateSelf Lib "advapi32.dll"
(ImpersonationLevel As Integer) As Long

but this gives parameter validation errors. After looking at the C header
file, I changed it to the following and it was a lot better:

Private Declare Function ImpersonateSelf Lib "advapi32.dll" (ByVal
ImpersonationLevel As Long) As Long

The next problem is the definition of the SECURITY_IMPERSONATION_LEVEL
values. The API TextViewer gives just:

Private Const SecurityAnonymous = 1
Private Const SecurityIdentification = 2

even though these blatantly disagree with the MSDN documentation. I
noticed
that a lot of old posts also seem to use these bad values. Again, after
looking at the C header files, I used the following definition instead:

Private Enum SECURITY_IMPERSONATION_LEVEL
SecurityAnonymous = 0
SecurityIdentification
SecurityImpersonation
SecurityDelegation
End Enum


It's for this reason I always use the header files to find definitions and
signatures. Then build the ones I want to use into a type library.


.