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 easy it is to call the braymain function in the Bray DLL using Delphi.

Before the braymain function can be used Delphi has to be told its location and the calling parameters to use. This is done by a function declaration. After the function is declared the braymain function can be called directly.

Here is a complete program listing including the proper function declaration. It consists of just a single button on a form. When the button is clicked the Bray command 0 is sent to the DLL. The DLL responds by sending back the DLL identification code (version number) which is displayed in a message box. This command does not need a string parameter to be sent so braystring is sent as a null string.

unit Unit1;
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    { Private declarations }
    { Public declarations }
  Form1: TForm1;
{$R *.dfm}
function braymain(braycmd: longint;
   braystring: PChar):
   PChar; stdcall; external 'mulebray.dll';
procedure TForm1.Button1Click(Sender: TObject);
var braycmd: longint;       {brayman command parameter}
var braystring: PChar;      {braymain string parameter}
var brayretstring: PChar;   {braymain return string}
braycmd := 0;     {Zero command returns the DLL version number}
braystring :='';  {no string parameter is needed for zero command}
brayretstring:= braymain(braycmd, braystring);
messagebox(0,brayretstring,'braymain result',MB_OK);

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 the 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-quote characters and Scancode Strings to be surrounded by single-quote characters. These quote characters must be included in the string sent to the DLL.

The second time 'Hello World' is sent the Carriage Return is written as a raw ScanCode String. This is to illustrate how to overcome the difficulty of embedding a single-quote character in a Delphi string. A single-quote character has to be entered twice.

Alternatively declare the single-quote character as a string constant and use the concatenation operator.

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 := braymain(braycmd,braystring);

braycmd := 1;				          {parse and execute Bray}
braystring := '"Hello World 1 !{ret}"'; {line of bray with surrounding Dquotes}
brayretstring := braymain(braycmd,braystring);

  { This shows how to embed a single-quote character inside a }
  { Delphi string - the Single quote must be entered twice    }

braycmd := 1;				          {parse and execute Bray}
braystring := '"Hello World 2 !"''5AF05A'''; {ScanCode string with Squotes x 2}
brayretstring := braymain(braycmd,braystring);

  {    Alternatively use a string constant declared as a Squote character }
  {    and use the concatenation operator. The declaration looks like this...   }
  {    const SQ = chr(39);   // put this in the Public Declaration section      } 

braycmd := 1;		              {parse and execute Bray}
braystring := '"Hello World 3 !"' + SQ + '5AF05A' + SQ;  {Concatenated Squotes}
brayretstring := braymain(braycmd,braystring);

Top Home            © Lee Allen,2018