Well I think it’s time to get our hands dirty with some code. In this simple application, I intend to demonstrate how to get MSMQ up and running on the device and send our first “Hello World” message to a local queue on the device. We’ll also receive the message from the queue and display it in a message box. While this is not terribly fanciful it provides the foundation required to utilize this technology on the device. My primary aim in this program is to show how to install and start MSMQ service on the device from managed code.

You’ll have to grab the following components from the SDK and add them to the project as type content (msmqadm.exe,msmqadmext.dll,msmqd.dll and msmqrt.dll)

using System;

using System.Drawing;

using System.Collections;

using System.Windows.Forms;

using System.IO;

using System.Data;

using System.Messaging; //You’ll have to use Add Reference to add the

//System.Messaging dll to your Project

using System.Runtime.InteropServices;

 

namespace SimpleMessagingApp

{

    public partial class Form1 : System.Windows.Forms.Form

    {

        private System.Windows.Forms.MainMenu mainMenu1;

       

 

  public class ProcessInfo

        {

            public IntPtr hProcess;

            public IntPtr hThread;

            public Int32 ProcessId;

            public Int32 ThreadId;

        }

 

        [DllImport("CoreDll.DLL", SetLastError = true)]

        private extern static

            int CreateProcess(String imageName,

            String cmdLine,

            IntPtr lpProcessAttributes,

            IntPtr lpThreadAttributes,

            Int32 boolInheritHandles,

            Int32 dwCreationFlags,

            IntPtr lpEnvironment,

            IntPtr lpszCurrentDir,

            IntPtr lpsiStartInfo,

            ProcessInfo pi);

 

        [DllImport("CoreDll.dll")]

        private extern static

            Int32 GetLastError();

        [DllImport("CoreDll.dll")]

        private extern static

             Int32 GetExitCodeProcess(IntPtr hProcess, out Int32 exitcode);

        [DllImport("CoreDll.dll")]

        private extern static

             Int32 CloseHandle(IntPtr hProcess);

        [DllImport("CoreDll.dll")]

        private extern static

            IntPtr ActivateDevice(

              string lpszDevKey,

                Int32 dwClientInfo);

 

 

        [DllImport("CoreDll.dll")]

        private extern static

            Int32 WaitForSingleObject(IntPtr Handle,

            Int32 Wait);

 

        public static bool CreateProcess(String ExeName, String CmdLine)

        {

            Int32 INFINITE;

            unchecked { INFINITE = (int)0xFFFFFFFF; }

 

            ProcessInfo pi = new ProcessInfo();

          

            if (CreateProcess(ExeName, CmdLine, IntPtr.Zero, IntPtr.Zero,

                0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, pi) == 0)

            {

                return false;

            }

           

            WaitForSingleObject(pi.hProcess, INFINITE);

 

            Int32 exitCode;

            if (GetExitCodeProcess(pi.hProcess, out exitCode) == 0)

            {

                MessageBox.Show("Failure in GetExitCodeProcess");

                CloseHandle(pi.hThread);                        // Free handles

                CloseHandle(pi.hProcess);

                return false;

            }

 

            CloseHandle(pi.hThread);                        // Free handles

            CloseHandle(pi.hProcess);

 

            if (exitCode != 0)

                return false;

            else

                return true;

 

        }

    

        //This is the MSMQAdm.exe utility we’ll be using

        private const String MSMQ_ADM = @"\windows\msmqadm.exe";

        private const String MSMQ_DRIVER_REG = @"Drivers\BuiltIn\MSMQD";

        public Form1()

        {

            InitializeComponent();

 

            //We need to move the MSMQ components deployed to the system folder

CopyFilesRequired();

            //Check status of MSMQ (is it installed and running yet?

 

            if (!(CreateProcess(MSMQ_ADM, "status")))

            {

               

                //Deletes the MSMQ registry key and store directory.

    //All messages are lost.

                CreateProcess(MSMQ_ADM, "register cleanup");

                 

    //Installs MSMQ as device drivcers.

    if (CreateProcess(MSMQ_ADM, "register install"))

                {

                    MessageBox.Show("Registered Drivers Successfully");

                }

                else

                {

                    MessageBox.Show("Failed to install MSMQ! System Error = " + GetLastError().ToString());

                    return;

                }

   //Creates the MSMQ Configuration in Registry

                if (CreateProcess(MSMQ_ADM, "register"))

                {

                    MessageBox.Show("Registered MSMQ Successfully");

                }

                else

                {

                    MessageBox.Show("Failed to Register MSMQ! System Error = " + GetLastError().ToString());

                    //  return;

                }

    //Enables the native MSMQ protocol

                if (CreateProcess(MSMQ_ADM, "enable binary"))

                {

                    MessageBox.Show("Enable Binary Successfully");

                }

                else

                {

                    MessageBox.Show("Failed to enable Binary! System Error = " + GetLastError().ToString());

                    return;

                }

    //Starts the MSMQ service

                if (CreateProcess(MSMQ_ADM, "start"))

                {

                    MessageBox.Show("Started MSMQ Successfully");

                }

                else

                {

  //This is one additional step that is needed for PocketPCs

  //The Device Drivers have to be loaded before the service

  //can be started

                    //ActivateDevice will load the device drivers

                    IntPtr handle = ActivateDevice(MSMQ_DRIVER_REG, 0);

                    CloseHandle(handle);

                    //Let us check if MSMQ is running

                    if (CreateProcess(MSMQ_ADM, "status"))

                    {

                        MessageBox.Show("MSMQ is up and running");

                    }

                    else

                    {

                        MessageBox.Show("Failed to start MSMQ! System Error = " + GetLastError().ToString());

                    }

                  }

            }

            else

                MessageBox.Show("MSMQ is already up and running");

 

        }

        private static void CopyFilesRequired()

        {

            string _path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

            if (!File.Exists(@"\windows\msmqadm.exe"))

                File.Copy(_path+"\\msmqadm.exe", @"\windows\msmqadm.exe");

            if (!File.Exists(@"\windows\msmqadmext.dll"))

                File.Copy(_path + "\\msmqadmext.dll", @"\windows\msmqadmext.dll");

            if (!File.Exists(@"\windows\msmqd.dll"))

                File.Copy(_path + "\\msmqd.dll", @"\windows\msmqd.dll");

            if (!File.Exists(@"\windows\msmqrt.dll"))

                File.Copy(_path + "\\msmqrt.dll", @"\windows\msmqrt.dll");

 

            File.Delete(_path + "\\msmqadm.exe");

            File.Delete(_path + "\\msmqadmext.dll");

            File.Delete(_path + "\\msmqd.dll");

            File.Delete(_path + "\\msmqrt.dll");

        }

      

       //We will send and receive a message in button1’s click event handler

      private void button1_Click(object sender, EventArgs e)

        {

//The Queue on the device we will be sending messages to

            string strDestQ = @".\private$\destq";

            try

            {

                if (!MessageQueue.Exists(strDestQ))

                    MessageQueue.Create(strDestQ);

                MessageQueue mq = new MessageQueue(strDestQ);

                Message m = new Message();

      

                m.Label = "Hello World";

                m.Body = "Hello World";

                mq.Send(m);

                mq.Close();

                MessageBox.Show("We sent our First Message!!");

              

               mq.Formatter = new XmlMessageFormatter(new Type[]

                {typeof(String)});

               Message messageReceived = mq.Receive();

 

            MessageBox.Show((string)messageReceived.Body);

           }

            catch (Exception ex)

            {

                MessageBox.Show(ex.ToString());

            }

        }

    }

}