如何从 Delphi 传递 cstring
我正在 Delphi 中为服务器编写一个 tcp 客户端,该服务器具有一系列定义为 c 结构的消息。下面是其中一条消息的示例转换:
struct {
int32 Reserved;
cstring Name;
int32 flags;
}
msg1 = record
Reserved : integer;
Name : cstring???;
flags : integer;
end
谷歌搜索类型告诉我,cstring 与我期望在这种情况下传递的标准 char 数组不同,但我似乎无法找出 a 的内部表示cstring。
我如何在记录中表示 cstring 以传递到服务器?
I'm writing a tcp client in Delphi for a server that has a series of messages defined as c structs. Below is an example conversion of one of the messages:
struct {
int32 Reserved;
cstring Name;
int32 flags;
}
msg1 = record
Reserved : integer;
Name : cstring???;
flags : integer;
end
Googling the type tells me that a cstring is different than the standard array of char I would expect to pass in this situation, but I can't seem to find out the internal representation of a cstring.
How would I represent cstring in the record for passing to the server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该规范显然使用术语 cstring 来表示 char 数组,后跟 null 终止符,而不是实际的 CString 类型。显然只是规范中令人恼火的术语混乱。
The spec apparently uses the term cstring to mean an array of char followed by a null terminator instead of an actual CString type. Apparently just an irritating confusion of terminology in the spec.
没有什么真正的区别。 “C 字符串”是一个指向以 null 结尾的字符数组的指针,仅此而已。 C 中没有像 Delphi 中那样的真正的“字符串类型”。 Delphi用PChar类型来表示。 (请注意,在 D2009 及更高版本中,您必须了解 PAnsiChar 和 PWideChar 之间的区别。)
不过,如果文档将数组本身称为 C 字符串,而不是指向的指针,请小心数组,这是一个潜在的陷阱。您必须在记录中使用数组,并且必须确切地知道它应该有多长。
There's no real difference. A "C string" is a pointer to a null-terminated array of chars, nothing more. There's no real "string type" in C like there is in Delphi. Delphi represents it with the PChar type. (Be aware that in D2009 and later, you have to know the difference between PAnsiChar and PWideChar.)
Careful, though, if the documentation is calling the array itself a C string, as opposed to a pointer to the array, that's a potential pitfall. You'll have to use an array in your record, and you'll have to know exactly how long it's supposed to be.