title: Which handle should I use to initialize my scanner app
body: <p>I wish to operate a scanner using a windows service hosting a web API.
  The reason I am doing so is to be able to operate the scanner from javascript using ajax and I do not wish to use ActiveX.</p>
  
  <p>So I built a Windows Service hosting a Web API.
  I tried for the web API to start a scan application (had to bypass the UAC for this) but I run into a consistency issue (sometimes it worked and sometimes not).</p>
  
  <p>So for now the controller of the web API has the code to start scanning built into it (instead of an external application). The problem is that I don`t have a handle to give the twain wrapper (it needs it for the windows message hook).</p>
  
  <p>I tried to take the current process MainWindowHandle and even tried something silly as taking iexplore MainWindowHandle and I get an exception "Error initialising DSM".</p>
  
  <p>As a twain library I use <a href="https://github.com/tmyroadctfig/twaindotnet" rel="nofollow noreferrer">TwainDotNet</a></p>
  
  <p>The code for doing is:</p>
  
  <pre><code>var p = Process.GetCurrentProcess();
  _twain = new Twain(new WinFormsWindowMessageHook(p.MainWindowHandle));
  </code></pre>
  
  <p>Any idea on a good way to get a handle that will work? as I wrote above I ok with "stealing" another application handle if that will do the job (e.g. iexplore)</p>
  
  <p>The exception is thrown from the TwainDotNet class DataSourceManager constructor in this code:</p>
  
  <pre><code>TwainResult result = Twain32Native.DsmParent(
              ApplicationId,
              IntPtr.Zero,
              DataGroup.Control,
              DataArgumentType.Parent,
              Message.OpenDSM,
              ref windowHandle);
  
  if (result == TwainResult.Success)
  {
      ...
  }
  else
  {
      throw new TwainException("Error initialising DSM: " + result, result);
  }
  </code></pre>
  
  <p>Thanks</p>