title: Consuming COM-object from C# console-project
body: <p>I have a problem consuming a COM-class from C#. The COM-class is developed in C++ ATL 32-bit.</p>
  
  <p>The COM-class works fine when i use it from VBA, VB6, C++, Javascript and even from MSTest/C#</p>
  
  <p>The wierd thing is that when i create an instance from a NUnit-test or from a Console application it fails with the exception:</p>
  
  <pre><code>System.InvalidCastException : Unable to cast COM object of type 'PvtsFlashLib.FlashClass' to interface type 'PvtsFlashLib.IFlash4'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{07065455-85CD-42C5-94FE-DDDC1B1A110F}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
  </code></pre>
  
  <p>Can someone point me in the right direction?</p>
  
  <p>Thanks in advance for your help.</p>
  
  <p>The build config for the test-project and the console project is set to:</p>
  
  <pre><code>Platform = x86
  </code></pre>
  
  <p>The COM reference in both projects is set to:</p>
  
  <pre><code>Copy Local = True
  Embed Interop Types = False
  Isolated = False
  </code></pre>
  
  <p>Code for MSTest that works fine:</p>
  
  <pre><code>using Microsoft.VisualStudio.TestTools.UnitTesting;
  using PvtsFlashLib;
  
  namespace TestProject1
  {
      [TestClass]
      public class TestOfComMSTest
      {
          [TestMethod]
          public void CreateFlash()
          {
              var flash = new Flash();
              flash.AdvancedOptions = PvtsFlashAdvancedOptionsEnum.AllProperties;
          }
      }
  }
  </code></pre>
  
  <p>Code for NUnit test that fails:</p>
  
  <pre><code>using NUnit.Framework;
  using PvtsFlashLib;
  
  namespace Test
  {
      [TestFixture]
      public class TestOfComNUnit
      {
          [Test]
          public void CreateFlash()
          {
              var flash = new Flash();
              flash.AdvancedOptions = PvtsFlashAdvancedOptionsEnum.AllProperties;
          }
      }
  }
  </code></pre>
  
  <p>Code for Console app that also fails:</p>
  
  <pre><code>using PvtsFlashLib;
  
  namespace ConsoleTest
  {
      class Program
      {
          static void Main(string[] args)
          {
              var flash = new Flash();
              flash.AdvancedOptions = PvtsFlashAdvancedOptionsEnum.AllProperties;
          }
      }
  }
  </code></pre>
  
  <p>I haven't got enough reputation points to answer my own question. But her it is anyway:</p>
  
  <p>For some reason the COM-object can't be created from an MTAThread. MSTest defaults to STAThread and NUnit and Console defaults to MTAThread. Applying the [STAThread] attribute to ConsoleTest.Main() and Test.CreateFlash() solves the problem.</p>