關於PChar應用

  1. 功能實現,尚有關於Delphi裏函數參數B: PChar 與 var B: PChar 不同的疑問,暫放。
  2. VC--------- Dll
  3. // CharDll.cpp : 定義 DLL 應用程序的導出函數。
  4. //
  5. #include "stdafx.h"
  6. #include "CharDll.h"
  7. // 這是導出變量的一個示例
  8. CHARDLL_API int nCharDll=0;
  9. // 這是導出函數的一個示例。
  10. CHARDLL_API int fnCharDll(void)
  11. {
  12.  return 42;
  13. }
  14. // 這是已導出類的構造函數。
  15. // 有關類定義的信息,請參閱 CharDll.h
  16. CCharDll::CCharDll()
  17. {
  18.  return;
  19. }
  20. CHARDLL_API int FnTest1(char *A, char *B)
  21. {
  22.  strcpy(B, A);
  23.  strcat(B, "PYG");
  24.  return strlen(B);
  25. }
  26. CHARDLL_API int FnTest2(char *A, char **B)
  27. {
  28.  strcpy(*B, A);
  29.  strcat(*B, "PYG");
  30.  return strlen(*B);
  31. }
  32. CHARDLL_API int FnTest3(char *A, char *&B)
  33. {
  34.  strcpy(B, A);
  35.  strcat(B, "PYG");
  36.  return strlen(B);
  37. }
  38.  ---------------------------------------調用及用Delphi寫的相應函數--------------------------------------
  39. unit Unit1;
  40. interface
  41. uses
  42.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  43.   Dialogs, StdCtrls;
  44. type
  45.   PPChar = ^PChar;
  46. type
  47.   TForm1 = class(TForm)
  48.     Edit1: TEdit;
  49.     Edit2: TEdit;
  50.     TestFn1: TButton;
  51.     Label1: TLabel;
  52.     Label2: TLabel;
  53.     TestFn2: TButton;
  54.     TestFn3: TButton;
  55.     Button4: TButton;
  56.     Button5: TButton;
  57.     procedure TestFn1Click(Sender: TObject);
  58.     procedure TestFn2Click(Sender: TObject);
  59.     procedure TestFn3Click(Sender: TObject);
  60.     procedure Button4Click(Sender: TObject);
  61.     procedure Button5Click(Sender: TObject);
  62.   private
  63.     { Private declarations }
  64.   public
  65.     { Public declarations }
  66.     function DelTest1(A: PChar; B: PChar): Integer;
  67.     function DelTest2(A: PChar; B: PPChar): Integer;    
  68.   end;
  69. var
  70.   Form1: TForm1;
  71.   function FnTest1(A: PChar; B: PChar): Integer; cdecl; external 'CharDll.dll';
  72.   function FnTest2(A: PChar; B: PChar): Integer; cdecl; external 'CharDll.dll';
  73.   function FnTest3(A: PChar; B: PChar): Integer; cdecl; external 'CharDll.dll';
  74. implementation
  75. {$R *.dfm}
  76. procedure TForm1.TestFn1Click(Sender: TObject);
  77. var
  78.   pA, pB: PChar;
  79.   iB, i: Integer;
  80. begin
  81.   pA := PChar(Edit1.Text);
  82.   GetMem(pB, 100);
  83.   iB := FnTest1(pA, pB);
  84.   Edit2.Text := StrPas(pB);
  85.   FreeMem(pB);
  86. end;
  87. procedure TForm1.TestFn2Click(Sender: TObject);
  88. var
  89.   pA, pB: PChar;
  90.   iB, i: Integer;
  91. begin
  92.   pA := PChar(Edit1.Text);
  93.   GetMem(pB, 100);
  94.   iB := FnTest2(pA, @pB);
  95.   Edit2.Text := StrPas(pB);
  96.   FreeMem(pB);
  97. end;
  98. procedure TForm1.TestFn3Click(Sender: TObject);
  99. var
  100.   pA, pB: PChar;
  101.   iB, i: Integer;
  102. begin
  103.   pA := PChar(Edit1.Text);
  104.   GetMem(pB, 100);
  105.   iB := FnTest3(pA, @pB);
  106.   Edit2.Text := StrPas(pB);
  107.   FreeMem(pB); 
  108. end;
  109. function TForm1.DelTest1(A, B: PChar): Integer;
  110. var
  111.   iLen: Integer;
  112. begin
  113.   iLen := Length(A) + 3;
  114.   FillChar(B^, iLen, #0);
  115.   StrCopy(B, A);
  116.   StrCat(B, 'PG');   
  117. end;
  118. procedure TForm1.Button4Click(Sender: TObject);
  119. var
  120.   pA, pB: PChar;
  121.   iB: Integer;
  122. begin
  123.   pA := PChar(Edit1.Text);
  124.   GetMem(pB, StrLen(pA)+3);
  125.   DelTest1(pA, pB);
  126.   Edit2.Text := StrPas(pB);
  127.   FreeMem(pB);
  128. end;
  129. function TForm1.DelTest2(A: PChar; B: PPChar): Integer;
  130. var
  131.   iLen: Integer;
  132. begin
  133.   iLen := Length(A) + 3;
  134.   FillChar(PChar(B^)^, iLen, #0);
  135.   StrCopy(PChar(B^), A);
  136.   StrCat(PChar(B^), 'PG');
  137. end;
  138. procedure TForm1.Button5Click(Sender: TObject);
  139. var
  140.   pA, pB: PChar;
  141.   pC: PPChar;
  142.   iB: Integer;
  143. begin
  144.   pA := PChar(Edit1.Text);
  145.   pC := @pB;
  146.   GetMem(pB, StrLen(pA)+3);
  147.   DelTest2(pA, pC);
  148.   Edit2.Text := StrPas(pB);
  149.   FreeMem(pB);
  150.   {相當於上邊的代碼
  151.   pA := PChar(Edit1.Text);
  152.   GetMem(pC, 100);
  153.   GetMem(pC^, StrLen(pA)+3);
  154.   DelTest2(pA, pC);
  155.   Edit2.Text := StrPas(pC^);
  156.   FreeMem(pC^);
  157.   FreeMem(pC);}
  158. end;
  159. end.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章