Neat way to programmatically impersonate in c# 10/05/2017 03:52

//code executing under default account
using (Impersonator impersonator = new Impersonator("myDomain", "myUsername", "myPassword"))
    // code executing under myUsername account
//code executing under default account
If you need to execute few methods under different account than the one your code is currently executing under (account shown in User Name column in Task Manager for your process) all you need to do is to surround them with above code and include below class into your project.

  public class Impersonator : IDisposable
        private WindowsImpersonationContext _impersonatedUser = null;
        private IntPtr _userHandle;

        public Impersonator(string userDomain, string username, string password)
            this._userHandle = new IntPtr(0);
            bool returnValue = LogonUser(username, userDomain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref this._userHandle);
            if (!returnValue)
                throw new ApplicationException("Could not impersonate user");
            WindowsIdentity newId = new WindowsIdentity(this._userHandle);
            this._impersonatedUser = newId.Impersonate();

        #region IDisposable Members
        public void Dispose()
            if (this._impersonatedUser != null)

        #region Interop imports/constants
        public const int LOGON32_LOGON_INTERACTIVE = 2;
        public const int LOGON32_LOGON_SERVICE = 3;
        public const int LOGON32_PROVIDER_DEFAULT = 0;
        [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
        public static extern bool LogonUser(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public extern static bool CloseHandle(IntPtr handle);
Above code is from here.
Selfish BuildingBlocks? Check out video. 07/03/2017 23:42
In added video I explain my BuildingBlocks solution. Go here.
F4CIOs Answering System - Master project is done! 26/09/2012 17:15
Finally I presented my final (master) project at faculty. The work itself was interesting experience. If you are tech savvy kind of a person you can read whole theoretical background. If you are not, you can just try how it works in practice.
F4CIOs Answering System
Collision Avoider - fuzzy logic demo 16/12/2011 20:03

As part of one project at faculty I have made this application to demonstrate the power of Fuzzy logic. Thanks to the "Fuzzy Logic Library for Microsoft .Net" i was able to finish this demo in one night!

More about library itself at:

To download demo and/or source code go to this page.


How to swap Y and Z keys programmatically? 20/02/2011 15:54

Making errors too often when using Y and Z keys?

If so, change their positions programmatically in windows.

  • Easy way to get that done is by using SharpKeys application BUT it does not allows you to program keys selectively depending on chosen keyboard layout. 
  • More advanced way is to use Microsoft Keyboard Layout Creator application. If you are running Windows 7, after you download its setup file, make sure to run it in Windows Vista compatibility mode. And don't forget to redefine keys for cases with Shift, CapsLock, Alt, and/or Ctrl pressed.

I have physical keyboard with Y\Z (Z key positioned above Y key) (Serbian edition) and that makes no problems as far as I use just one keyboard layout in windows (Serbian-latin). But, I switch to US keyboard layout on regular basis and it expects Z\Y (Z key to be below Y) so it was very irritating to give attention to these details every time I use these keys.
My solution was:

  1. I concluded that I want to learn and get use to US keyboard layout since it is more common. Buying new US keyboard was not an option since it was notebook.
  2. I physically swapped keys so that Y end up above Z (Z\Y, as for US).
  3. In windows, for keyboard layout that expects different, Y\Z, swap keys programmatically. This can be done with Microsoft Keyboard Layout Creator mentioned above.
    If you are using Serbian-Latin layout, like me, don't bother to learn that application. Just install my Serbian-Latin layout with YZ keys swapped.

I you have physical keyboard with Y\Z (serbian or another non-US) and you want to train your self, and use Y\Z AND you are using US-International layout just install my US-International layout with YZ keys swapped.

After installing any of above don't forget to change layout in Windows to newly installed. Provided layout setups are tested on Windows 7 (32bit), XP and Windows 10 (64bit) but should work on any other Windows platform.

2 3> >>|
<October 2022>