Home Page The Mule™ The Bray DLL shopping cart
site currency

currency info
spacer Home spacer Barcode Mill spacer Barcode Fonts spacer How to order spacer Email us spacer

For 30 years I have been helping organisations and individuals with their Barcode and Auto ID issues but now I have decided it is time to move on and retire from being the Barcode Man.

I will continue to respond to emails from existing customers about their earlier purchases, their special programming configurations and warranty issues but I regret I cannot help with new purchases or issues nor recommend alternative products or sources.

Lee Allen, The Barcode Man. February 2010

Web This site

Mule Resources

bulletMule Home
bulletMule Dispatcher
bulletBray Overview
bulletBray Basics
bulletBray Script Index
bulletBray DLL
bulletKeyboard Emulator
bulletKeyboard Scan Codes
bulletMule Quickstart
bulletTechnical Manual
bulletEngineering details
bulletLegacy Tech Manual
bulletLegacy downloads

The Mule is a trademark of Altek Instruments Ltd

Support Services

bulletTechnical Support
bulletHow to Order
bulletEmail us
bulletWho we are
bulletPolicy Statements
bulletSpam email from here?


In this example it is assumed mulebray.dll is located in the same folder as the caller code (recommended) or in some other standard location where Windows can find it. If not you may have to prepend the path to the dll filename.

This example shows how to call the braymain function in the Bray DLL using C# running under the Microsoft.NET Framework.

A bray class 'wrapper' is constructed to contain all the code that interacts directly with the DLL. Data marshaling is used to convert the native C# parameters to the form needed by the DLL. Notice that although the DLL uses ANSI (ASCII) strings the final result passed back to the caller is a native C# Unicode string.

The test example consists of a single button on a form. Run the code and click the button. A zero value command is sent to mulebray.dll and it returns the current version number which is displayed in a message box.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;       // dont forget to add this 

namespace braycaller
    public partial class Form1 : Form
   	public Form1()
        private void button1_Click(object sender, EventArgs e)
            uint braycmd;
            string braystring;
            string brayretstring;

            braycmd = 0;
            braystring = "";

            brayretstring = bray.braymain(braycmd,braystring);
    class bray
        public static string braymain(uint braycmd, string braystring)
            return braymaindll(braycmd, braystring);
        EntryPoint="braymain" , CharSet = CharSet.Ansi,
        CallingConvention = CallingConvention.StdCall)]
        public static extern string braymaindll(uint braycmd,
        [MarshalAs(UnmanagedType.LPStr)]string braystring);

You can use this code framework to experiment with sending Bray commands to the DLL to control the Mule (and through it a secondary slave computer). Just change value of braycmd and give a string value to braystring when necessary.

A simple 'Hello World!' example is shown below.

First you need to tell Bray which port the Mule is connected to. This only needs to be done once. The DLL retains the information until it is unloaded from memory when the program ends.

Now you can send Bray commands direct to the DLL by loading braycmd with the value 1 and putting the Line of Bray in braystring. The DLL parses and executes the content of braystring. The example shows how to create keystrokes for Hello World! on the remote computer. Remember that Bray needs ASCII strings to be surrounded by double quotes so you will need to escape these characters with a backslash.

This is a very basic example. It works but if you run it a couple of times you will get an error because the code attempts to reopen an open port (you opened it on the previous run). Real applications would need to check for error conditions and provide feedback to the user. More comprehensive Bray examples can be found on the Further Bray DLL Examples page.

Further Bray DLL Examples

Table of all the DLL commands and parameter values

braycmd = 20;			//Command to tell Bray which COM port to use
braystring = "COM1";		//Assumes the Mule is connected to COM1
brayretstring = bray.braymain(braycmd,braystring);
braycmd = 1;				//parse and execute Bray
braystring = "\"Hello World!{ret}\"";	//line of bray (dquotes are escaped) 
brayretstring = bray.braymain(braycmd,braystring);
Top Home            © Lee Allen,2018