Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best Practice, Send and recieve textmasses from DLL

Status
Not open for further replies.

Nordlund

Programmer
Jul 17, 2001
458
SE
Hi there.
What's the best way to send/recieve textmasses from/to a Dynamic link library?
You may not use Delphi ShareMem unit.
Should I solve it in another way, or is may way the preferred way :)
(The way I split First and Lastname can be solved more efficient, but it is'nt the main issue in this case.)
The DLL:
Code:
library TestDLL;

uses
  Dialogs, StrUtils, SysUtils, Classes, Commonclasses;

{$R *.res}

procedure SeparateNames(lpFullName: pChar; lpResultFirstName, lpResultLastName: pChar; MaxLen: Integer); export; stdcall;
var
  s: String;
  lp1, lp2: pChar;
begin
  s := StrPas(lpFullName);

  GetMem(lp1, MaxLen);
  GetMem(lp2, MaxLen);

  try
    // Separate First and Last Name
    StrPCopy(lp1, Copy(s, 1, Pos(' ', s)-1));
    StrPCopy(lp2, Copy(s, Pos(' ', s)+1, Length(s)));

    StrLCopy(lpResultFirstName, lp1, MaxLen);
    StrLCopy(lpResultLastName, lp2, MaxLen);
  finally
    FreeMem(lp1);
    FreeMem(lp2);
  end;

end;

exports
  SeparateNames;

begin
end.

The Testapplication:
Code:
procedure CallTheDll;
const
  MAX_LEN = 100;
var
  DLLHandle: THandle;
  SeparateNames: procedure(lpFullName: pChar; lpResultFirstName, lpResultLastName: pChar; MaxLen: Integer); stdcall;
  lpFirstName, lpLastName: pChar;
begin
  GetMem(lpFirstName, MAX_LEN);
  GetMem(lpLastName, MAX_LEN);

  try
    DLLHandle := LoadLibrary('TestDLL.dll');
    if DLLHandle <> 0 then
    begin
      @SeparateNames := GetProcAddress(DLLHandle, 'SeparateNames');
      if @SeparateNames <> nil then
      begin
        SeparateNames(pChar('FirstName Lastname'), lpFirstName, lpLastName, MAX_LEN);
        ShowMessage(StrPas(lpFirstName) +#13#10+ StrPas(lpLastName));
      end;

      FreeLibrary(DLLHandle);
    end;
  finally
    FreeMem(lpFirstName);
    FreeMem(lpLastName);
  end;
end;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-"There is always another way to solve it, but I prefer my way.
 
other option is to use MM4 memory manager from pierre Le Riche, no extra dll has to be included. dll and app must use the Fastmm unit though. It IMO one of the best memory managers available for the moment (that's why borland uses it for BDS2006)

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
You don't need to use the "MaxLen", the DLL code can obtain the PChars length - StrLen(s) + 1 - and get the buffers accordingly.

You need to balance two issues here:

a) From some point of view, declaring the MaxLen can make things more understandable.

b) If you are going to send strings of very different lengths, the MaxLen approach can make your code unefficient memory wise (if you send two strings of 10 bytes and 64K bytes you need 2x64k buffers).

It is up to you to considere the options.

Other than that I can't see any problem with your strategy.

Note: If your strings are going to be lesser than 255 bytes, use the classic PASCAL strings - string[xx] - and save yourself a handfull of code lines.

buho (A).
 
Thanks for the input.

Whosrdaddy, Does the MM4 memory manager fit Delphi 7 also?
Is this is the case I Have to try it.

buho, The StrLen was a great tip, I'l use it.

Thanks gyus....

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-"There is always another way to solve it, but I prefer my way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top