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
CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT)CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT) par FREMYCOMPANY
Bonjour à tous, Je viens de publier une proposition comprenant 5 pseudo-classes pour le CSS Working Group ayant trait à l'état de chargement d'un élément (ex: IMG,VIDEO,AUDIO,OBJECT pour l'HTML.). Si le c½ur vous en dit, vous pouvez retrouver cette p...
Cliquez pour lire la suite de l'article par FREMYCOMPANY MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ?MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ? par ROMELARD Fabrice
Formation initiale Durant la formation, le découpage classique est le suivant (je donnerai les équivalences Suisse lorsque je les connaîtrais) : Ecole primaire jusqu'au Collège : Formation générale permettant d'obtenir les méthodes...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice Y'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENTY'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENT par Aleks
Quand on a ce genre d'erreur sans log :
Et bas on a juste envie de choper le gas de Microsoft qu'a développé ça et lui foutre des baffes de Coboye ! ...
Cliquez pour lire la suite de l'article par Aleks [HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL[HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL par Pierrick CATRO-BROUILLET
Avec la sortie prochaine de la Beta Consumer Preview de Windows 8, j'avais envie de revenir sur une des fonctionnalités que j'attends le plus et que, en bon geek que je suis, j'utilise déjà : Hyper-V 3 ainsi son module PowerShell.
Il y a déjà pléthor...
Cliquez pour lire la suite de l'article par Pierrick CATRO-BROUILLET IIS7 - COMPRESSION GZIPIIS7 - COMPRESSION GZIP par cyril
La compression GZIP permet d'améliorer les performances de navigation en compressant ce qu'envoie le serveur à un client. Pour comprendre comment cela fonctionne, regardons ce qu'il se passe au niveau HTTP lorsqu'un client tente d'accéder à une ress...
Cliquez pour lire la suite de l'article par cyril
Logiciels
Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning Academy System (17.1.3.0)ACADEMY SYSTEM (17.1.3.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|