How Do I Call a DLL Function? (Magic xpa 4.x)
There are a lot of DLLs available in the Windows environment, in packages you may purchase, and you may even want to write some of your own. Every DLL call is different, because most of the work is matching your parameter string to what the DLL expects, and to figure that out you need the documentation for the DLL. We will go through a simple example for calling the windows API to show a message box.
In Magic xpa, you can call a DLL using the CallDll() function, or by using the Invoke UDP operation. We will see how to use both here.
-
Press F4 to open up a line where you want to make the call.
-
Press U to create an Update operation, or select the operation from the pull-down list.
-
Select the variable you want to use for the return value. In this case, our DLL returns a numeric value, so we are using a numeric for the return code. The number happens to represent the user’s response.
-
Zoom from the With: field to enter your DLL call. You DLL call will use the CallDLL() function.
The syntax is:
CallDLL(DLLName,ArgString,Arg1,Arg2...)
Where
DLLName is the name of the DLL. In this case, it is user32.MessageBoxA.
ArgString is a string of characters, where each character stands for the data type of the argument. There is one character for each argument, plus the last one, which is for the return value.
Argx are the arguments being passed. In this case there are four: the Window handle, two text strings, and a number representing which style of box to use.
|
Letter
|
Data Type
|
1
|
Char
|
2
|
Short
|
4
|
Long
|
F
|
Float
|
8
|
Double
|
D
|
Double pointer
|
E
|
Float pointer
|
L
|
Long pointer
|
A
|
Null terminated string pointer
|
V
|
Void pointer
|
0
|
Void
|
|
When we are done entering the CallDLL and we run it, we get the Windows message box. When the user presses a button, that returns a number to our calling program (6=Yes, 7=No, 2=Cancel).
Hint: When using operating-system dependent functions such as this one, it’s a good idea to encapsulate them inside a Magic xpa task or function. That way, if you decide to use a different call, or something changes about your installation, it’s easy to change.
Calling a DLL with an Invoke UDP operation is very similar to using the CallDLL() function.
-
Go to the appropriate Logic unit, and press F4 to open up a line.
-
Type I to select the Invoke operation. Tab to the next field.
-
Type U to select UDP. Tab to the next field.
-
Zoom to the Expressions list, and type in the name of the DLL you want to call, preceded by an ‘@’. In our example, we are calling the Windows user32.MessageBoxA, so we enter
'@user32.MessageBoxA'
-
Press Enter to select the expression, then tab to the next field.
-
Zoom to add your arguments. These use the same format as those in the CallDLL() function, but they are easier to format.
-
The first argument contains one character for each argument required by the DLL. MessageBoxA takes 4 arguments, so we have 4 characters. Each character represents the data type of the argument: a ‘4’ is a Long integer, and an ‘A’ is a null-delimited string.
-
For the other arguments, zoom from the Var or Exp columns as needed.
The Online and Rich Client Samples projects (program EL09 and REL09)