为什么?
Delphi中很多“解决不了”的问题只要使用Build with Runtime Packages
就可以解决?
难道不这样子就不行?——怪不得Delphi的版本兼容性巨差!用呀用呀,用到D7才发现:
在Delphi帮助里:
String to PChar conversionsDelphi6中:
Long string to PChar conversions are not automatic. Some of the differences between strings and PChars can make conversions problematic:Long strings are reference-counted, while PChars are not.
Assigning to a string copies the data, while a PChar is a pointer to memory.
Long strings are null-terminated and also contain the length of the string, while PChars are simply null-terminated.Situations in which these differences can cause subtle errors are discussed in this section.Delphi7中:
Long string to PChar conversions are not automatic. Some of the differences between strings and PChars can make conversions problematic:Long strings are reference-counted, while PChars are not.
Assigning to a string copies the data, while a PChar is a pointer to memory.
Long strings are null-terminated and also contain the length of the string, while PChars are simply null-terminated.Situations in which these differences can cause subtle errors are discussed in the following topics:String dependencies
Returning a PChar local variable
Passing a local variable as a PChar 多了三个连接:
String dependencies
Returning a PChar local variable
Passing a local variable as a PChar
其中 Returning a PChar local variable 写道:
A common error when working with PChars is to store a local variable in a data structure, or return it as a value. When your routine ends, the PChar disappears because it is a pointer to memory, and not a reference counted copy of the string. For example:function title(n: Integer): PChar;
var
s: string;
begin
s := Format('title - %d', [n]);
Result := PChar(s); // DON'T DO THIS
end;This example returns a pointer to string data that is freed when the title function returns.DON'T DO THIS!!!Delphi不让这么做啦~!?!在帮助 Passing a local variable as a PChar 中讲道:
Consider the case where you have a local string variable that you need to initialize by calling a function that takes a PChar. One approach is to create a local array of char and pass it to the function, then assign that variable to the string:// assume FillBuffer is a predefined function
function FillBuffer(Buf:PChar;Count:Integer):Integer
begin
. . .
end;
// assume MAX_SIZE is a predefined constant
var
i: Integer;
buf: array[0..MAX_SIZE] of char;
S: string;
begin
i := FillBuffer(0, buf, SizeOf(buf)); // treats buf as a PChar
S := buf;
//statements
end;This approach is useful if the size of the buffer is relatively small, since it is allocated on the stack. It is also safe, since the conversion between an array of char and a string is automatic. The Length of the string is automatically set to the right value after assigning buf to the string.To eliminate the overhead of copying the buffer, you can cast the string to a PChar (if you are certain that the routine does not need the PChar to remain in memory). However, synchronizing the length of the string does not happen automatically, as it does when you assign an array of char to a string. You should reset the string Length so that it reflects the actual width of the string. If you are using a function that returns the number of bytes copied, you can do this safely with one line of code:var
S: string;
begin
SetLength(S, MAX_SIZE; // when casting to a PChar, be sure the string is not empty
SetLength(S, GetModuleFilename( 0, PChar(S), Length(S) ) );
// statements
end;我考~
真烦~
郁闷~仔细一看,其实在Delphi6的Help中就有这个了,就是没在“主页”做连接!惨呀~伤心了~!
难道真的逼我......
Delphi中很多“解决不了”的问题只要使用Build with Runtime Packages
就可以解决?
难道不这样子就不行?——怪不得Delphi的版本兼容性巨差!用呀用呀,用到D7才发现:
在Delphi帮助里:
String to PChar conversionsDelphi6中:
Long string to PChar conversions are not automatic. Some of the differences between strings and PChars can make conversions problematic:Long strings are reference-counted, while PChars are not.
Assigning to a string copies the data, while a PChar is a pointer to memory.
Long strings are null-terminated and also contain the length of the string, while PChars are simply null-terminated.Situations in which these differences can cause subtle errors are discussed in this section.Delphi7中:
Long string to PChar conversions are not automatic. Some of the differences between strings and PChars can make conversions problematic:Long strings are reference-counted, while PChars are not.
Assigning to a string copies the data, while a PChar is a pointer to memory.
Long strings are null-terminated and also contain the length of the string, while PChars are simply null-terminated.Situations in which these differences can cause subtle errors are discussed in the following topics:String dependencies
Returning a PChar local variable
Passing a local variable as a PChar 多了三个连接:
String dependencies
Returning a PChar local variable
Passing a local variable as a PChar
其中 Returning a PChar local variable 写道:
A common error when working with PChars is to store a local variable in a data structure, or return it as a value. When your routine ends, the PChar disappears because it is a pointer to memory, and not a reference counted copy of the string. For example:function title(n: Integer): PChar;
var
s: string;
begin
s := Format('title - %d', [n]);
Result := PChar(s); // DON'T DO THIS
end;This example returns a pointer to string data that is freed when the title function returns.DON'T DO THIS!!!Delphi不让这么做啦~!?!在帮助 Passing a local variable as a PChar 中讲道:
Consider the case where you have a local string variable that you need to initialize by calling a function that takes a PChar. One approach is to create a local array of char and pass it to the function, then assign that variable to the string:// assume FillBuffer is a predefined function
function FillBuffer(Buf:PChar;Count:Integer):Integer
begin
. . .
end;
// assume MAX_SIZE is a predefined constant
var
i: Integer;
buf: array[0..MAX_SIZE] of char;
S: string;
begin
i := FillBuffer(0, buf, SizeOf(buf)); // treats buf as a PChar
S := buf;
//statements
end;This approach is useful if the size of the buffer is relatively small, since it is allocated on the stack. It is also safe, since the conversion between an array of char and a string is automatic. The Length of the string is automatically set to the right value after assigning buf to the string.To eliminate the overhead of copying the buffer, you can cast the string to a PChar (if you are certain that the routine does not need the PChar to remain in memory). However, synchronizing the length of the string does not happen automatically, as it does when you assign an array of char to a string. You should reset the string Length so that it reflects the actual width of the string. If you are using a function that returns the number of bytes copied, you can do this safely with one line of code:var
S: string;
begin
SetLength(S, MAX_SIZE; // when casting to a PChar, be sure the string is not empty
SetLength(S, GetModuleFilename( 0, PChar(S), Length(S) ) );
// statements
end;我考~
真烦~
郁闷~仔细一看,其实在Delphi6的Help中就有这个了,就是没在“主页”做连接!惨呀~伤心了~!
难道真的逼我......
string to PChar
就直接 PChar(str);
PChar to string
就直接
str := StrPas(ch);
就可以了啊
如果你能克服这些,致力于你的专业,你的技术,你等于2年学了1门语言,水平自然不用说了。
对自己帮助不少啊。
进步也很大的。
我先是按F1找不到答案就到GOOGLE。
再找不到就到CSDN收索贴子。
再找不到就发贴请教咯!