Home > Back-end >  For dynamic library how to write characters together?
For dynamic library how to write characters together?

Time:10-17

For dynamic library how to write characters together? Masters problems to solve, be urgent!!!

CodePudding user response:

http://embarcadero.newsgroups.archived.at/public.delphi.nativeapi/201103/1103314922.html

DLL:
 
The library Project1.

{Important note about DLL memory management: ShareMem must be the
The first unit in your library 's USES clause AND your project' s (select
The Project - the View Source) USES clause if your DLL exports any procedures or
Functions provides that pass strings as parameters or function results. This
Applies to all strings passed to and from your DLL - even those that
Are nested in records and classes. ShareMem is the interface unit to
The BORLNDMM. DLL Shared memory manager, which must be deployed along
With your DLL. To get the using BORLNDMM. DLL, pass the string information
Using PChar or ShortString parameters.}

USES the
Forms,
SysUtils,
Unit1 in 'Unit1. Pas' {fmMain};

{$R *. Res}

The function JoinString (stringOne, stringTwo: PChar) : PChar; Export; Stdcall;
Var
Buffer: PChar;
The begin
GetMem (Buffer, Length (stringOne) + Length (stringTwo) + 1);
StrCopy (Buffer, PChar (stringOne));
SysUtils. StrCat (Buffer, stringTwo);
Result:=Buffer;
FreeMem (Buffer);
end;

Exports
JoinString;

The begin
End.



Call:
 
The unit Unit2.

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

Type
TForm1=class (TForm)
For: TButton;
Edit1: TEdit;
Edit2: TEdit;
Memo1: TMemo;
Button2: TButton;
Procedure Button2Click (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
end;

Var
Form1: TForm1;

Implementation

{$R *. DFM}

The function JoinString (stringOne, stringTwo: PChar) : PChar; Export; Stdcall; External 'Project1. DLL';

Procedure TForm1. Button2Click (Sender: TObject);
The begin
Memo1. Lines. The Add (JoinString (PChar (Edit1. Text), PChar (Edit2. Text)));
end;

End.

CodePudding user response:

Actually can use WideString directly this is the format of Microsoft, nor the memory manager management by Delphi.
He is OLE with API SysAllocStrLen BSTR is distributed.
  • Related