I was playing around with SSPI, the Security Support Provider Interface. I stumbled across a behavior that I cannot explain : you cannot call LoadLibrary when you are impersonating.

If you check you program with SysInternals (now Microsoft’s) Process Monitor, you’ll find an error saying”Bad Impersonation”.

LoadLibrary returns NULL and a call to GetLastError() says “Either a required impersonation level was not provided, or the provided impersonation level is invalid”.

If you just specify a filename, LoadLibrary will look in the current directory only. If supply a full path to the DLL, LoadLibrary will find it but the CreateFile will fail with Bad Impersonation.

I am still baffled by this behavior. I will keep looking into it. Meanwhile, here is a simple program that reproduces the problem.

BadImpersonation.cpp

You can get the full project using anonymous subversion :

svn co [http://src.paralint.com/spikes/badimpersonation](http://src.paralint.com/spikes/badimpersonation) BadImpersonation

Good luck !