All our code is going to be added to the Unit1 unit, so we
need to open it first.
Click on the Unit1 tab at the bottom of the code window. If the
tab isn't there, choose View/Units.
The Add function is quite simple. To pass/return an integer, word, or long, you don't have to do anything special. The function can be created the same way as any other Delphi function.
When you create a function you want to use in APW, you must declare as an exported function.
After the 'interface' keyword, add this code.
uses sysutils, wintypes, winprocs;
function Add( n1, n2 : Integer ) : Integer ; export;
Delphi32 requires an additional piece of code. It needs to have the 'stdcall' directive added to it. However, Delphi won't understand what stdcall means and will generate an error. So we have to use a conditional include - one that is included in 32bit, but ignored in 16bit. Add this piece of code at the end of the function declaration.
{$ifdef WIN32} stdcall ; ($endif}
This tells the compiler to include that bit of code only under 32bit. It should look like this:
function Add( n1, n2 : Integer ) : Integer ; export; {$ifdef WIN32} stdcall ; {$endif}
Now we can add the code for the function.
After the 'implementation' keyword, add this code.
function Add( n1, n2 : Integer )
: Integer ;
begin
Result := n1 + n2;
end;
on to: Testing the Add function
back to: Setting up the project Start