title: OpenWindowStation error 183 /ERROR_ALREADY_EXISTS/
body: <p>I'm writing a program with remote administration functionality and I stuck with some problems. The main issue is, that every calling of 'OpenWindowStation' results in 'error 183' and a Null-pointer.</p>
  
  <p>This code runs as a service on a LocalSystem account. I also tried to allow a desktop interaction, but all of these were in vain.</p>
  
  <p>I've reviewed a couple of similar questions here, but no one of their decisions helped me.</p>
  
  <pre><code>    namespace sasserv
      {
  public class sasserv : ServiceBase
  {
      [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
      static extern IntPtr OpenWindowStation( string name, bool fInherit, uint needAccess );
  
      [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
      static extern IntPtr CloseWindowStation( IntPtr hWinSta );
  
      [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
      static extern IntPtr OpenDesktop( string name, Int32 flags, bool fInherit, long param );
  
      [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
      static extern IntPtr CloseDesktop( IntPtr hDesktop );
  
      [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
      static extern bool PostMessage( IntPtr hWnd, UInt32 msg, UInt32 wParam, IntPtr lParam );
  
      [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
      static extern bool SetThreadDesktop( IntPtr hDesktop );
  
      [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
      static extern bool SetProcessWindowStation( IntPtr hWinSta );
  
      public const string MyServiceName = "sasserv";
  
      public sasserv()
      {
          InitializeComponent();
      }
  
      private void InitializeComponent()
      {
          this.ServiceName = MyServiceName;
      }
  
      /// &lt;summary&gt;
      /// Start this service.
      /// &lt;/summary&gt;
      protected override void OnStart(string[] args)
      {
          RunWinlogon();
      }
  
      /// &lt;summary&gt;
      /// Stop this service.
      /// &lt;/summary&gt;
      protected override void OnStop()
      {
      }       
  
      protected void OutputLastError(string funcName) {
          int err = Marshal.GetLastWin32Error();
          if ( err &gt; 0 )
              Out(funcName, "ERR:"+err);
      }
  
      private void RunWinlogon()
      {
          int WINSTA_ALL_ACCESS = 0x37F;
          long DESKTOP_CREATEMENU = 0x0004L;
          long DESKTOP_CREATEWINDOW = 0x0002L;
          long DESKTOP_ENUMERATE = 0x0040L;
          long DESKTOP_HOOKCONTROL = 0x0008L;
          long DESKTOP_JOURNALPLAYBACK = 0x0020L;
          long DESKTOP_JOURNALRECORD = 0x0010L;
          long DESKTOP_READOBJECTS = 0x0001L;
          long DESKTOP_SWITCHDESKTOP = 0x0100L;
          long DESKTOP_WRITEOBJECTS = 0x0080L;
          int HWND_BROADCAST = 0xffff;
          uint WM_HOTKEY = 0x0312;
          short MOD_ALT = 0x0001;
          short MOD_CONTROL = 0x0002;
          short VK_DELETE = 0x2E;
          uint MAXIMUM_ALLOWED = 0x2000000;
  
          uint WLX_WM_SAS = 0x0659;
          uint WLX_SAS_TYPE_CTRL_ALT_DEL = 0x0001;
  
          uint GENERIC_READ  = 0x80000000;
          uint GENERIC_WRITE  = 0x40000000;
          uint  GENERIC_EXECUTE  = 0x20000000;
          uint GENERIC_ALL  = 0x10000000;
          uint  GENERIC_RIGHTS_CHK  = 0xF0000000;
  
          Out("OpenWindowStation", "Running2");
          var hWinSta = OpenWindowStation( "WinSta0", false,  MAXIMUM_ALLOWED );
          if (  hWinSta == IntPtr.Zero ) {
              Out("OpenWindowStation", "hWinSta=ZERO");
              return;
          }
          OutputLastError("OpenWindowStation");
  
  
          Out("SetProcessWindowStation", "Running");
          if ( !SetProcessWindowStation( hWinSta ) ) {
              Out("SetProcessWindowStation", "FALSE");
              return;
          }
  
          OutputLastError("SetProcessWindowStation");
  
          Out("OpenDesktop", "Running");
          var hDesk = OpenDesktop( "Winlogon", 0, false, MAXIMUM_ALLOWED);
  
          OutputLastError("OpenDesktop");
  
          if ( hDesk == IntPtr.Zero ) {
              Out("OpenDesktop", "HDESK=ZERO");
              return;
          }
  
  
          if ( !SetThreadDesktop(hDesk) ) {
              Out("SetThreadDesktop", "FALSE");
              return;
          }
  
          OutputLastError("SetThreadDesktop");
  
          Out("PostMessage", "Running...");
          var postSucc = PostMessage( new IntPtr(HWND_BROADCAST),
                                     WLX_WM_SAS,
                                     WLX_SAS_TYPE_CTRL_ALT_DEL,
                                     IntPtr.Zero);
  
          OutputLastError("PostMessage");         
  
          CloseDesktop( hDesk );
          CloseWindowStation( hWinSta );          
      }
  
      static IntPtr MakeLongPtr( short wLow, short wHigh ) {          
          return new IntPtr( (UInt32)(wLow | ( (UInt32)wHigh &lt;&lt; 16 )));
      }
  
      static void Out( string func, string info ) {
          NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
  
          logger.Info( "FUNC: "+func+" . INFO: "+info );
      }
  }
  }
  </code></pre>
  
  <p>Here is a log of execution:</p>
  
  <p>Here is a log of execution: 
  12.07.2016 10:29:47.3526|sasserv|FUNC: OpenWindowStation . INFO: Running2
  12.07.2016 10:29:47.3646|sasserv|FUNC: SetProcessWindowStation . INFO: Running
  12.07.2016 10:29:47.3646|sasserv|FUNC: SetProcessWindowStation . INFO: ERR:183
  12.07.2016 10:29:47.3746|sasserv|FUNC: OpenDesktop . INFO: Running
  12.07.2016 10:29:47.3746|sasserv|FUNC: OpenDesktop . INFO: ERR:183
  12.07.2016 10:29:47.4025|sasserv|FUNC: SetThreadDesktop . INFO: ERR:183
  12.07.2016 10:29:47.4065|sasserv|FUNC: PostMessage . INFO: Running...
  12.07.2016 10:29:47.4065|sasserv|FUNC: PostMessage . INFO: ERR:87</p>