title: Unmanaged code calling managed code works on dev machine, not on deployed machine
body: <p>I am building a webcam app based on the VideoRendererElement project (http://videorendererelement.codeplex.com/), which uses the DirectShowLib and Interop with an unmanaged activex component. I am using Visual Studio 2010, but am targeting .NET 3.5 (2.0 clr runtime).</p>
  
  <p>I have been able to build and run the app on my development machine with no problem (outside of Visual Studio, even). However, when I deployed the app on the target machine (Windows 7,.NET up to date, C++ redistributable up to date) the app crashes. I set up an additional method to log the running application to a text file on the target machine a traced down one key difference, the unmanaged code calls the method in the managed code, only the static fields defined previously are missing. This only happens on the target machine (the one I deployed the code to). Here are some code snippets and what I logged. What is confusing is that this error ONLY occurs on the target machine. Any help pointing me in the right direction would be greatly appreciated.</p>
  
  <pre><code>namespace MediaBridge
  {
      .....
  
      public class MediaBridgeManager
      {
          public delegate void NewMediaGraphInfo(MediaBridgeGraphInfo GraphInfo);
  
          private static readonly Dictionary&lt;string, NewMediaGraphInfo&gt; _delegateHash = new Dictionary&lt;string, NewMediaGraphInfo&gt;();
  
          ......
  
          public static void AddMediaGraphInfo(MediaBridgeGraphInfo GraphInfo)
          {
              if (_delegateHash.ContainsKey(GraphInfo.MediaUrl))
              {
                  NewMediaGraphInfo callback = _delegateHash[GraphInfo.MediaUrl];
                  _delegateHash.Remove(GraphInfo.MediaUrl);
  
                  /* Suppress all errors on the callback */
                  try
                  {
                      callback(GraphInfo);
                  }
                  catch {}
              }
          }
  
          ......
  
          public static bool RegisterCallback(string MediaUrl, NewMediaGraphInfo Callback)
          {
              bool returnval = true;
              MediaUrl = FormatUrl(MediaUrl);
  
              if (!_delegateHash.ContainsKey(MediaUrl))
              {
                  _delegateHash.Add(MediaUrl, Callback);
              }
              else
              {
                  returnval = false;
              }
  
              return returnval;
          }
      }
  }
  </code></pre>
  
  <p>Before the .NET part of the app calls RegisterCallback() first and populates _delegateHash with a URL and callback function. This was verified in the logfile on the target machine. </p>
  
  <p>Now the unmanaged part:</p>
  
  <pre><code>STDMETHODIMP CMediaBridgeSourceFilter::Load(LPCOLESTR lpwszFileName, const AM_MEDIA_TYPE *pmt)
  {
      /* Thread-saftey */
      CAutoLock cAutoLockShared(&amp;m_cSharedState);
  
      HRESULT phr = S_OK;
  
      /* Create the output pin for our filter */
      m_pPin = new CMediaBridgeSourceFilterPin(&amp;phr, this);
  
      /* Create a new class to store information about us/graph in it */
      MediaBridge::MediaBridgeGraphInfo ^ graphInfo = gcnew MediaBridge::MediaBridgeGraphInfo();
  
      /* Set the media url sent */
      graphInfo-&gt;MediaUrl = gcnew System::String(lpwszFileName);
  
      /* Set the pointer to the filter graph */
      graphInfo-&gt;FilterGraph = System::IntPtr(this-&gt;GetFilterGraph());
  
      /* Store the pointer for our instance of the filter */
      graphInfo-&gt;SourceFilter = System::IntPtr(this);
  
      /* Do the callback into our managed code */
      MediaBridge::MediaBridgeManager::AddMediaGraphInfo(graphInfo);
  
      return phr;
  }
  </code></pre>
  
  <p>According to my log file MediaBridge::MediaBridgeManager::AddMediaGraphInfo() is being called on the .NET side but the call back is never reached because the _delegateHash variable is now empty.</p>
  
  <p>Here is what my log file says on the target machine that I'm trying to deploy the app on:</p>
  
  <pre><code>RegisterCallback(): MediaUrl = dshowmediabridge://d0ffd222-d023-483b-8fc7-4b4035ce3922/ Contains Key: True Delegate Hash count: 1
  
  InitializeDirectShow(): RegisterCallback == true, Url == DShowMediaBridge://d0ffd222-d023-483b-8fc7-4b4035ce3922
  
  AddMediaGraphInfo(): MediaUrl = dshowmediabridge://d0ffd222-d023-483b-8fc7-4b4035ce3922/ FilterGraph: 71122304 Contains Key: False Delegate Hash count: 0
  </code></pre>
  
  <p>Note how the Delegate Hash count (_delegateHash.Count) is now at 0 when the unmanaged code calls AddMediaGraphInfo().</p>
  
  <p>Is there something I should be doing to identify the dll to COM/C++ on the target machine that VS is doing for me on my development machine?</p>