关于delphi Move函数的用法详解
使用delphi多年,前些天忽然遇到不会string转pbyte,很是失落,此时对于编程基本功的重要性深有体会.这其中用到MOVE函数.
搞了好一会才搞明白其用法.所以想贴出来帮助需要帮助的人.
var
s:string;
ps:Pchar;
b:pbyte;
len:integer;
begin
s:=edit1.Text; //字符串
ps:=pchar(s); //转成pchar类型,
len:=length(s);//取字符串长度,占用多少字节
getmem(b,len);//申请内存,pchar,pbyte在使用前都必须要申请内存,因为他们是指针.
move(ps^,b^,len);//这里 ps^意思是pchar指向内存数据的第一个字节地址,B^是表示申请内存的第一个字节地址,这样就可以一个一个字节的移到b里去了.
memo1.Text:=pchar(b);//显示.
freemem(b);
end;
有些人遇到的困惑是为什么 move(s,b,len)不行呢?同样我也遇到这样的困惑.
看了一样move的函数源码才明白.
procedure Move( const Source; var Dest; count : Integer );
{$IFDEF PUREPASCAL}
var
S, D: PChar;
I: Integer;
begin
S := PChar(@Source);//取内存地址
D := PChar(@Dest);//取内存地址
if S = D then Exit;
if Cardinal(D) > Cardinal(S) then
for I := count-1 downto 0 do
D[I] := S[I]
else
for I := 0 to count-1 do
D[I] := S[I];
end;
如果直接传入s,
S := PChar(@Source);//取内存地址\
就相当于取的字符串S地址的地址.
如果传入的是ps^
S := PChar(@Source);//取内存地址
就相当于取pchar 所指向字符串实际数据的地址.
相关文档:
Delphi和C++数据类型对照表
Delphi 字长/值域 C++
ShortInt &n ......
Procedure TForm1.Button1Click(Sender: TObject);
Var
xmlstr,FileName: String;
f: Textfile;
Begin
xmlStr := '<?xml version="1.0" encoding="gb2312"?>';
xmlstr := xmlstr + '<user><name>张三</name><sex>男</sex></user>';
sh ......
delphi dll 实例 与 dll窗体实例
本动态链接库方法有
Min,Max,SynAPP,ShowForm,showmyform
dll工程文件
Library Project1;
uses
dllUnit1 in 'dllUnit1.pas' {Form1};
function Min(X, Y: Integer): Integer; export;
begin
if X < Y then Min := X else Min := Y;
end;
function Max(X, Y: Integer): ......
dll 调用方法有 静态调用和动态调用两种方法
用到的dll为上篇文章所编写的dll.
总结如下:
Unit Unit1;
Interface
Uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
Type
TForm1 = Class(TForm)
Button1: TButton; ......
Delphi 7之后的版本,增加了运算符的重载。虽然不尽人意(需要写特定英文),但有总比没有强。
例:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
T3DPoint = record
X, Y, Z: Doub ......