The ReturnString function merits some discussion. Any string that you want to pass back to APW must go through this function. What it does is allocate some memory for the string, copy the string into it, and then return it to APW. Note that APW does the freeing of the memory allocated. I will make some more comments when creating the UCase function. This function should not be exported; it will not be called directly from APW.
Open the Unit1 code window.
First we need to declare our function. In the 'interface'
section, after the line that declares the Add function, add this
line:
function ReturnString(StringIn: Pchar): Pchar ; far;
There is no need to use the 'stdcall' directive on this function. The 'stdcall' is only necessary on functions that are to be exported. Your interface section should look like this
uses sysutils, wintypes, winprocs;
function Add( n1, n2 : Integer )
: Integer ; export; {$ifdef WIN32} stdcall ;
{$endif}
function ReturnString(StringIn: Pchar): Pchar ; far;
Here is the ReturnString function. It is placed in the 'implementation' section, after the Add function.
function ReturnString(StringIn:
Pchar): Pchar ;
{ Puts a string on the
windows heap and returns a windows handle to the string }
var
hGlobalMemory: THandle;
lpGlobalMemory: pointer ;
begin
{This
line sets up a character pointer from windows heap }
hGlobalMemory
:= GlobalAlloc(GMEM_MOVEABLE , strlen(StringIn)+1);
if hGlobalMemory <> 0 then { memory allocated OK }
begin
{ This sets up the string in the memory
grabbed from the heap }
lpGlobalMemory :=
GlobalLock(hGlobalMemory);
lstrcpy(lpGlobalMemory,StringIn);
GlobalUnlock(hGlobalMemory);
end;
{ This returns a pointer to the string to APW
}
ReturnString := Pchar(hGlobalMemory);
end;
on to: Creating the UCase function
back to: Testing the Add function Start