为什么要用pchar来强制转换一个string类型的参数。。谁可以详细的叙述一下pchar的作用最好用例子来阐述。讲讲string ,pchar ,数组之间的区别
解决方案 »
- 代码实现AdoQuery字段编辑器中Add All Fields功能
- [急]有关delphi实现ftp多线程下载的问题:该如何将主线程分割成多个线程?
- CoolTrayIcon控件在win98中ShowBalloonHint为啥不能显示?
- 急寻图像处理分析高手,有报酬!!!
- 请问代码执行不了,提示delphi版本不对怎么解决?
- object pascal的问题和弱点?!
- 谁能帮我解释一下这句话,很简单的.
- 求教关于SQL ALTER 的两个简单问题!
- 我有好几十个dbedit控件,我想用几句话来控制它们全部enabled为false
- cxGrid 过滤筛选如何操作筛选后的数据集!
- 如何选择“源”,即从数码相机和扫描仪中获取图形文件?
- dbedit1与ComboBox1的上下宽度为什么不能一样?
String类型是Delphi的一种比较特别的类型,处理字符串用。但是不是针对指针操作的。
而Pchar 相当于C++的字符串数组,是有起始内存和结束内存的地址的,所以一般用C++写的
DLL中Char * 参数在Delphi中使用时一般都用Pchar来代替,如果用String的话由于没有内存地址而容易出错!
string是Delphi中的一般类型(还有基础类型例如ShortString,AnsiString,WideString)
类型 最大长度 占用内存 用于
ShortString 255个字符 2到256字节 向后兼容
AnsiString 大约2^31个字符 4字节到2GB 8位(ANSI)字符
WideString 大约2^30个字符 4字节到2GB 多用户服务和多语言应用程序
Delphi推荐使用一般类型。因为string是以#0结尾所以和PChar兼容。
procedure tform1.getDomain(DomainName:string;s:string);
var n,j:integer;
sbuf:array[0..512] Of char;
stmp:array[0..80] Of char;
sFlag:pchar;
str:string;
f:textfile; // 文本文件类型
i:Integer;
begin
AssignFile(f,'ftpdini.ini');
Reset(f);
n:=0;
while not Eof(f) do
begin
Readln(f, str);
strpcopy(sbuf,str);
for i:=0 to 80 do
begin
stmp[i]:=sbuf[i];
if sbuf[i]='=' then
begin
stmp[i]:=#0;
n:=i+1;
break;
end;
end;
if strcomp(PChar(domainname),Pchar('UPIP'))=0 then
begin
if strcomp(stmp,'UPIP')=0 then
begin
for i:=n to 80 do
begin
if sbuf[i]=';' then
n:=i+1;
end;
j:=0;
for i:=n to 80 do
begin
stmp[j]:=sbuf[i];
if sbuf[i]=#0 then
break;
j:=j+1;
end;
strcopy(Pchar(s),stmp);
end;
end;
if strcomp(PChar(domainname),pchar('HOSTIP'))=0 then
begin
if strcomp(stmp,'HOSTIP')=0 then
begin
j:=0;
for i:=n to 80 do
begin
stmp[j]:=sbuf[i];
if sbuf[i]=';' then
begin
stmp[j]:=#0;
break;
end;
j:=j+1;
end;
strcopy(Pchar(s),stmp);
end;
end;
if strcomp(Pchar(domainname),pchar('LOCALIP'))=0 then
begin
if strcomp(stmp,'LOCALIP')=0 then
begin
for i:=n to 80 do
begin
if sbuf[i]=';' then
n:=i+1;
end;
j:=0;
for i:=n to 80 do
begin
stmp[j]:=sbuf[i];
if sbuf[i]=#0 then
break;
j:=j+1;
end;
strcopy(Pchar(s),stmp);
end;
end;
end;
CloseFile(f); // 关闭文件
end;
使用是:getDomain('HOSTIP',s1);
编译没有问题?但使用它的时候就要内参溢出,这是为什么?