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
.
.
 
ANNOUNCEMENT

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

Google
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?
lll

Visual Basic v.6.0

This code is not compatible with Microsoft .NET Framework. Visual Basic .NET code can be found 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 Visual Basic 6.0. It should also work for earlier Visual Basics v.4 and v.5.

With Visual Basic there are some string issues which make it difficult to call the braymain function direct. To overcome this we create a 'wrapper' around the DLL function. We can then call the braymain wrapper in exactly the way we would have expected to call the DLL. All the string conversions are handled transparently by the wrapper.

We need to make three declarations. The first is to the braymain function within mulebray.dll. Notice we are using an alias - braymaindll. This allows us to reserve the braymain name for the wrapper without creating a name conflict.

The other declarations allow us to use two special Windows API functions. We use them to retrieve the string returned by the braymain function. The API functions we need are in "kernel32" which is a part of Windows and already on your Windows PC.

The Declarations and Wrapper code can all be put in a separate module like this...

Declare Function braymaindll Lib "mulebray" _
    Alias "braymain" (ByVal braycmd As Long, _
    ByVal braystring As String) As Long

Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" _
    (dest As Any, source As Any, ByVal bytes As Long)

Private Declare Function lstrlenA Lib "kernel32" _
    (ByVal ptr As Long) As Long
    

Public Function braymain(braycmd As Long, braystring As String) As String Dim ptr As Long Dim length As Long Dim buffer As String ptr = braymaindll(braycmd, braystring) 'get pointer to retstring length = lstrlenA(ptr) 'get len of retstring buffer = Space$(length) 'create a buffer to accept retstring CopyMem ByVal buffer, ByVal ptr, length 'copy from mem to buffer braymain = buffer 'assign string to function End Function

This shows how the braymain wrapper function is used with a simple example attached to a button. 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.



Private Sub Command1_Click()
    Dim braycmd As Long
    Dim braystring As String
    Dim brayretstring As String
    
    braycmd = 0		'Zero command returns the DLL version number
    braystring = ""	'The zero command does not need a string parameter
    
    'The braymain function we call here is a wrapper around the
    ' "real" braymain function within the DLL. Working this way we
    ' can sidesteps all Visual Basic string compatibility issues
    ' and call braymain in the way we would expect
    brayretstring = braymain(braycmd, braystring)
    
    MsgBox brayretstring
    
End Sub

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. To do this create a string containing a double quote character then use the concatenation operator to attach it to either end of the string.

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



Dim DQ As String
DQ = Chr(34)                    'Define a double quote character as a string

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 = DQ & "Hello World!{ret}" & DQ  'line of bray surrounded by dquotes 
brayretstring = braymain(braycmd,braystring)
   
.
.
Top Home            © Lee Allen,2018