如何从 Delphi 传递 cstring

发布于 2024-08-26 19:35:00 字数 367 浏览 5 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事↓在人 2024-09-02 19:35:00

该规范显然使用术语 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.

献世佛 2024-09-02 19:35:00

没有什么真正的区别。 “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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文