MessageQueue StrConv Conversion To C#

By Robbe Morris

If you are looking for a workaround concerning VB's StrConv and converting it to C# for use with message queues, the code below is what you need. This is also helpful for sending byte arrays to a message queue.

using System;
using System.Messaging;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;

public static void SendMessageToQueue(string queueName, string message)
{
    using (MessageQueue queue = new .MessageQueue(queueName))
    {
        var msg = new Message();        
         var bytes = ConvertStringToASCII(message);

        msg.AppSpecific = -1;
        msg.BodyType = (int)VarEnum.VT_BSTR;

        using(Stream stream = msg.BodyStream)
        {
           stream.Write(bytes, 0, bytes.Length);
           queue.Send(msg);
        }

     }

}

public static byte[] ConvertStringToASCII(string text)
{
      var unicode = Encoding.Unicode;
      var unicodeBytes = unicode.GetBytes(text);
      return Encoding.Convert(unicode, Encoding.ASCII, unicodeBytes);
}

MessageQueue StrConv Conversion To C#  (2121 Views)