title: Fast C++ program, C# GUI, possible?
body: <p>I'm looking into developing an application that will process data from a line-scan camera at around 2000 lines (frames) per second.  For this real-time application, I feel that C/C++ are the way to go.  (It is my feeling, and others will agree that Managed code just isn't right for this task.)</p>
  
  <p>However, I've done <em>very</em> little MFC, or any other C++ GUI. I am really getting to do C# GUIs very well, though.</p>
  
  <p>So it seems natural to me to write the data-intensive code in C/C++, and the GUI in C#.  The GUI will be used for set-up/calibration/on-line monitoring (and possibly outputting of data via UDP, because it's easier in C#.</p>
  
  <p>So first, I'd like to see if anyone agrees that this would be the way to go.  Based on my programming experience (good at low-level C algorithms, and high-level C# GUI design), it just feels right.</p>
  
  <p>Secondly, I'm not sure the right way to go about it.  I just threw together a solution in VS2005, which calls some (extern "C") DLL functions from a C# app.  And to make sure I could do it, I wrote to some global variables in the DLL, and read from them:</p>
  
  <p><strong>test.h</strong></p>
  
  <pre><code>int globaldata;
  extern "C" __declspec(dllexport) void set(int);
  extern "C" __declspec(dllexport) int  get();
  </code></pre>
  
  <p><strong>test.cpp</strong></p>
  
  <pre><code>extern int data=0;
  __declspec(dllexport) void set(int num) {
      data = num;
  }
  
  __declspec(dllexport) int get() {
      return data;
  }
  </code></pre>
  
  <p><strong>test.cs</strong></p>
  
  <pre><code>[DllImport("test")]
  private static extern void set(int num);
  
  [DllImport("test")]
  private static extern int get();
  </code></pre>
  
  <p>Calling <code>get()</code> and <code>set()</code> work properly (<code>get()</code> returns the number that I passed to <code>set()</code>).</p>
  
  <p>Now, I know that you can export a C++ class as well, but does it have to be managed? How does that work?  Am I going about this the right way?</p>
  
  <p>Thanks for all your help!</p>
  
  <p><strong><code>*** EDIT ***</code></strong></p>
  
  <p>First of all, <em>THANK YOU</em> for your fantastic answers so far! I'm always incredibly impressed with Stack Overflow...</p>
  
  <p>I guess one thing I should have hit on more, was not necessarily raw speed (this can be prototyped and benchmarked).  One thing that has me more concerned is the non-deterministic behavior of the Garbage Collector. This application would <em>not</em> be tolerant of a 500ms delay while performing garbage collection.</p>
  
  <p>I am all for coding and trying this in pure C#, but if I know ahead of time that the GC and any other non-deterministic .NET behavior (?) will cause a problem, I think my time would be better spent coding it in C/C++ and figuring out the best C# interface.</p>