EXE
function GetName(szName: PChar; MaxLen: Cardinal): Cardinal; stdcall;
begin
Result := MaxLen;
GetUserName(szName, Result);
end;
procedure TForm1.Button1Click(Sender: TObject);
type
TSayHello = procedure;
var
hDLL: THandle;
SayHello: TSayHello;
begin
hDLL := LoadLibrary('Project2.DLL');
if hDLL <> 0 then
begin
@SayHello := GetProcAddress(hDLL, 'SayHello');
SayHello;
FreeLibrary(hDLL);
end
else
begin
ShowMessage(SysErrorMessage(GetLastError));
end;
end;
exports
GetName;
DLL
function wvsprintf(Output: PChar; Format: PChar; Arg_List: array of const): Integer;
begin
Result := Windows.wvsprintf(Output, Format, @Arg_List[Low(Arg_List)]);
end;
procedure SayHello; stdcall;
type
TGetName = function(szName: PChar; nMaxLen: Cardinal): Cardinal; stdcall;
var
GetName: TGetName;
szName : array[0..255] of Char;
Buf : PChar;
begin
@GetName := GetProcAddress(0, 'GetName');
ZeroMemory(@szName, SizeOf(szName));
GetName(szName, SizeOf(szName));
Buf := StrAlloc(4096);
try
wvsprintf(Buf, 'Hello, %s!', [@szName]);
MessageBox(0, Buf, 'SayHello', MB_OK);
finally
StrDispose(Buf);
end;
end;
exports
SayHello;