delphi 字符串到“const buffer”
我有一个 dll,该程序接受:“const buffer”参数。
还有一个给该参数传值的例子:
var
str: array [0..200 - 1] of Char;
LTextSend: string;
begin
LTextSend := 'Text';
StrLCopy(PChar(@str[0]), PChar(LTextSend), High(str));
Dll_procedure(str, Length(LTextSend));
end;
该参数是否只能通过这样的声明来传递:str: array [0..200 - 1] of Char;
是否有更简单的方法将字符串(不同长度)传递给此 dll 参数?
谢谢
I have a dll which procedure accepts: "const buffer" parameter.
There is also an example of passing value to this parameter:
var
str: array [0..200 - 1] of Char;
LTextSend: string;
begin
LTextSend := 'Text';
StrLCopy(PChar(@str[0]), PChar(LTextSend), High(str));
Dll_procedure(str, Length(LTextSend));
end;
can this parameter be only passed through this declaration: str: array [0..200 - 1] of Char;
Isn't there an easier way to pass string (of different length) to this dll parameter?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样调用该过程:
这之所以有效,是因为 Delphi 无类型参数的发送方式与
var
参数相同,它们是通过引用发送的。编译器将发送一个指向str[1]
的指针,即字符串中的第一个字符,但这实际上是指向整个字符串的指针。像往常一样,确保字符串实际上包含第一个字符(即:它不为空)。你的第一个问题完全取决于 DLL。 “签名”允许发送任意长度的数据,但这并不意味着 DLL 已做好准备。也许它一次只能处理 200 个字节。
You can call the procedure like this:
This works because Delphi untyped parameters are sent the same way as
var
parameters, they're sent by reference. The compiler will send a pointer tostr[1]
, the first char in the string, but that's actually a pointer to the whole string. As usual, make sure the string actually contains an first character (ie: it's not empty).Your number 1 problem depends entirely on the DLL. The "signature" allows any length of data to be sent, but that doesn't mean the DLL is prepared for anything. Maybe it can only handle 200 bytes at a time.
我不知道你为什么要复制到临时缓冲区。
仅当 Dll_procedure 修改缓冲区时才有意义。但我怀疑它会。
所以你可以简单地写:
或者在调用之前使字符串唯一的变体:
I don't know why you're making a copy to a temporary buffer.
It does only make sense if the
Dll_procedure
will modify the buffer. But I doubt it will.So you could simply write:
Or a variant making the string unique before the call: