Accueil > > > DLL ET PCHAR
DLL ET PCHAR
Information sur la source
Description
Exemple d'une dll avec 2 Pchar en entrée et renvoyant un pchar qui est la concaténation des 2 Pchar en entrée pour Delphi.
Source
- Tout d'abord la dll:
- library JprStr;
-
- { Important note about DLL memory management: ShareMem must be the
- first unit in your library's USES clause AND your project's (select
- Project-View Source) USES clause if your DLL exports any procedures or
- functions 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 avoid using BORLNDMM.DLL, pass string information
- using PChar or ShortString parameters. }
-
- uses
- SysUtils,
- Types,
- Classes;
-
- {$R *.res}
- {H+}
-
- function JpConcAt( Pstr1 : Pchar; Pstr2 : Pchar;Pstr3 : Pchar):Pchar;stdcall;
- var
- str1,str2,str3:string;
- i:integer;
- chaine: array[0..39] of char;
- begin
- str1:=Strpas(Pstr1);
- str2:=Strpas(Pstr2);
- str3:=str1 + str2;
- strPLcopy(Pstr3,str3,40); // transformation String -> PChar
-
- for i:=0 to 39 do
- chaine[i]:=str3[i+1];
- result:=@chaine;// renvoie un Pchar
-
-
- end;
-
- exports
- JpConcAt;
-
- begin
- end.
-
-
- maintenant l'appel à partir d'un form contenant 2 TEdit(les 2 chaines à concaténer), 1 Tbutton, 1 Tedit (Résultat):
- unit AppelJpStr;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls;
-
- type
- TFrmAppelJpStr = class(TForm)
- Edit1: TEdit;
- Edit2: TEdit;
- Button1: TButton;
- Edit3: TEdit;
-
- procedure Button1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- FrmAppelJpStr: TFrmAppelJpStr;
-
-
- implementation
-
- function JpConcAt( Pstr1 : Pchar ;Pstr2 : Pchar;Pstr3 :Pchar):Pchar;stdcall;external '.\Jprstr.dll';
-
- {$R *.dfm}
-
- Procedure TFrmAppelJpStr.Button1Click(Sender: TObject);
- var
- Chaine1:array[0..19] of char;
- chaine2:array[0..19] of char;
- chaine3:array[0..39] of char;
- str1,str2,str3:string;
- i:Integer;
- Pstr1,Pstr:Pchar;
- Pstr2:Pchar;
- Pstr3:Pchar;
- begin
- str1:=edit1.Text;
- str2:=edit2.Text;
- str3:='';
- // initialisation Pstr1
- for i :=0 to length(str1) do
- chaine1[i]:= str1[i+1];
- Pstr1:=@chaine1;
- // initialisation Pstr2
- for i :=0 to length(str2) do
- chaine2[i]:= str2[i+1];
- Pstr2:=@chaine2;
- //réservation pour Pstr3
- Pstr3:=strAlloc(40);
- // appel library
- JpConcat(Pstr1,Pstr2,Pstr3);
- str3:=strpas(Pstr3);
- edit3.Text:= str3;
- end;
-
- Nota: si les passages Pchar->String ou String ->Pchar ne marchent pas (ca arrive...?), procédez Pchar->Array of Char ->String ou String -> Array Of Char -> Pchar
Tout d'abord la dll:
library JprStr;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions 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 avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Types,
Classes;
{$R *.res}
{H+}
function JpConcAt( Pstr1 : Pchar; Pstr2 : Pchar;Pstr3 : Pchar):Pchar;stdcall;
var
str1,str2,str3:string;
i:integer;
chaine: array[0..39] of char;
begin
str1:=Strpas(Pstr1);
str2:=Strpas(Pstr2);
str3:=str1 + str2;
strPLcopy(Pstr3,str3,40); // transformation String -> PChar
for i:=0 to 39 do
chaine[i]:=str3[i+1];
result:=@chaine;// renvoie un Pchar
end;
exports
JpConcAt;
begin
end.
maintenant l'appel à partir d'un form contenant 2 TEdit(les 2 chaines à concaténer), 1 Tbutton, 1 Tedit (Résultat):
unit AppelJpStr;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TFrmAppelJpStr = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FrmAppelJpStr: TFrmAppelJpStr;
implementation
function JpConcAt( Pstr1 : Pchar ;Pstr2 : Pchar;Pstr3 :Pchar):Pchar;stdcall;external '.\Jprstr.dll';
{$R *.dfm}
Procedure TFrmAppelJpStr.Button1Click(Sender: TObject);
var
Chaine1:array[0..19] of char;
chaine2:array[0..19] of char;
chaine3:array[0..39] of char;
str1,str2,str3:string;
i:Integer;
Pstr1,Pstr:Pchar;
Pstr2:Pchar;
Pstr3:Pchar;
begin
str1:=edit1.Text;
str2:=edit2.Text;
str3:='';
// initialisation Pstr1
for i :=0 to length(str1) do
chaine1[i]:= str1[i+1];
Pstr1:=@chaine1;
// initialisation Pstr2
for i :=0 to length(str2) do
chaine2[i]:= str2[i+1];
Pstr2:=@chaine2;
//réservation pour Pstr3
Pstr3:=strAlloc(40);
// appel library
JpConcat(Pstr1,Pstr2,Pstr3);
str3:=strpas(Pstr3);
edit3.Text:= str3;
end;
Nota: si les passages Pchar->String ou String ->Pchar ne marchent pas (ca arrive...?), procédez Pchar->Array of Char ->String ou String -> Array Of Char -> Pchar
Conclusion
l'exemple fonctionne en Delphi 6 et Delphi 7
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
PARUTION DE MON LIVRE SUR WPF 4PARUTION DE MON LIVRE SUR WPF 4 par odewit
La 2e édition de mon livre sur WPF sort aujourd'hui en version numérique et lundi en version papier :-)
L'ouvrage présente de façon approfondie les fonctionnalités de WPF 4 : graphisme 2D et 3D, animation, multimédia, interfaces utilisateur, databind...
Cliquez pour lire la suite de l'article par odewit EDM : COMMENT UTILISER L'HORIZONTAL ENTITY SPLITTINGEDM : COMMENT UTILISER L'HORIZONTAL ENTITY SPLITTING par Matthieu MEZIL
Une des raisons pour lesquelles j'adore l'Entity Framework est la puissance de son mapping. Beaucoup de développeurs pour ne pas dire la plus part n'en n'ont pas conscience. Pour rappel, j'ai réalisé des videos (en anglais) sur le mapping . Certains scena...
Cliquez pour lire la suite de l'article par Matthieu MEZIL [WP7DEV][REACTIVE] RENDRE LES REACTIVE EXTENSIONS PLUS STABLES[WP7DEV][REACTIVE] RENDRE LES REACTIVE EXTENSIONS PLUS STABLES par jay
Lorsque l'on développe des applications .NET, les exceptions non gérées dans des threads ont le désagréable effet de terminer le processus courant.
Dans l'exemple suivant.......(read more) ...
Cliquez pour lire la suite de l'article par jay WINDBG / SOS / PSSCOR2 : FAILED TO LOAD DATA ACCESS DLL (MSCORDACWKS)WINDBG / SOS / PSSCOR2 : FAILED TO LOAD DATA ACCESS DLL (MSCORDACWKS) par coq
Ceux d'entre nous qui analysent des dumps d'applications .NET (notamment ceux créés via WER après un crash) en dehors de l'environnement initial ont probablement tous été confrontés au moins une fois au message suivant, à la saisie d'une commande SOS ...
Cliquez pour lire la suite de l'article par coq
Logiciels
Microsoft Office (2010)MICROSOFT OFFICE (2010)Microsoft Office 2010 offre de nouveaux moyens flexibles et puissants pour optimiser votre travai... Cliquez pour télécharger Microsoft Office SeaMonkey (2.0.7)SEAMONKEY (2.0.7)Le projet SeaMonkey est issu d'un effort communautaire pour developper une application tout en un... Cliquez pour télécharger SeaMonkey Safari (5.0.2)SAFARI (5.0.2)Le navigateur d'Apple a lui aussi été mis à jour, aussi bien dans sa mouture Windows que celle po... Cliquez pour télécharger Safari Mozilla FireFox (4.0 béta 5)MOZILLA FIREFOX (4.0 BéTA 5)Firefox 4.0 béta 5
L'une des nouveautés visibles les plus attendues réside sans doute dans l'a... Cliquez pour télécharger Mozilla FireFox Mozilla Firefox (3.6.9)MOZILLA FIREFOX (3.6.9)Firefox 3.6.9 corrige les problèmes suivants :
* Introduced support for the X-FRAME-OPTION... Cliquez pour télécharger Mozilla Firefox
|