title: .NET CF - MSMQ ActivateDevice() Crash
body: <p>I have a .NET 3.5 Compact Framework application that uses MSMQ.
  We are running this application on an Intermec CN3, Windows Mobile 5.0 device.</p>
  
  <p>However, when our application first tries to active the MSMQ service with ActivateDevice (pinvoke), the application crashes and we get the error report message:</p>
  
  <blockquote>
    <p>A problem has occuurred with myApp.exe</p>
    
    <p>Please tell Microsoft about this problem, at not cost to you. ect..</p>
  </blockquote>
  
  <p>What we have done is this:</p>
  
  <ol>
  <li>Hard Reset the Device</li>
  <li>Install NETCFv35.wm.armv4i.cab</li>
  <li>Install msmq.arm.CAB</li>
  <li>*Run a CF console app that sets up MSMQ and the registry</li>
  <li>Soft reset the PDA</li>
  <li>*Run our application which calls ActivateDevice() on startup</li>
  </ol>
  
  <p>After doing a soft reset, the first time that ActivateDevice() is called, the application crashes.</p>
  
  <p>However, now that we have called ActivateDevice(), MSMQ services are working on the device atleast until it is soft reset again.</p>
  
  <p>Also, any calls to ActivateDevice() will not crash the application.</p>
  
  <p>The console app that we run after a hard reset is basically this:</p>
  
  <pre><code>class InstallRegister
  {
      public void Main()
      {
          RunMsmqAdmin("install");
          RunMsmqAdmin("register install");
          RunMsmqAdmin("register");
  
          SetQuotaValueRegistry("MachineQuota");
          SetQuotaValueRegistry("DefaultLocalQuota");
          SetQuotaValueRegistry("DefaultQuota");
  
          RunMsmqAdmin("enable binary");
          RunMsmqAdmin("enable srmp");
          RunMsmqAdmin("start");
  
          RegFlushKey(0x80000002);
      }
  
      private void SetQuotaValueRegistry(string quotaValueName)
      {
          Microsoft.Win32.Registry.SetValue(
                  "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\MSMQ\\SimpleClient\\"
                  , quotaValueName
                  , 100000);
      }
  
      private void RunMsmqAdmin(string command)
      {
          using (Process _process = new Process())
          {
              _process.StartInfo.FileName = @"\windows\msmqadm.exe";
              _process.StartInfo.Arguments = command;
              _process.StartInfo.UseShellExecute = true;
              _process.Start();
              _process.WaitForExit();
          }
      }
      [System.Runtime.InteropServices.DllImport("CoreDll.dll", EntryPoint = "RegFlushKey", SetLastError = true)]
      private static extern uint RegFlushKey(uint hKey);
  }
  </code></pre>
  
  <p>Our applications call to ActivateDevice() is basically this:</p>
  
  <pre><code>class ActivateMSMQ
  {
      public void Active()
      {
          var handle = ActivateDevice("Drivers\\BuiltIn\\MSMQD", 0);
          CloseHandle(handle);
      }
      [System.Runtime.InteropServices.DllImport("CoreDll.dll", SetLastError = true)]
      private static extern IntPtr ActivateDevice(string lpszDevKey, Int32 dwClientInfo);
  
      [System.Runtime.InteropServices.DllImport("CoreDll.dll", SetLastError = true)]
      private extern static Int32 CloseHandle(IntPtr hProcess);
  }
  </code></pre>
  
  <p>ActivateDevice() still causes our app the crash whenever the device is soft reset.</p>
  
  <p>Has anyone else experienced this with MSMQ on the compact framework?</p>