如何转换 C++ _tcscpy、_tcscat 到 Delphi?
我正在将 此代码 从 C++ 转换为Delphi,但我没有得到以下部分代码。谁能解释一下以下代码的含义; szBuff 缓冲区发生了什么?
我很确定它是这种格式(替换),但我什至不知道预期的结果是什么,而且我找不到任何有关所用函数的合理文档(也许我只是一个蹩脚的:)
任何人都可以帮助我将此代码翻译为Delphi(或指导我正确的文档)吗?
我不喜欢你如何自己转换类型的问题,所以我至少在问题标题中提到了函数名称,以便将来其他人可以搜索到。
function TSecInfo.BuildSecurityAttributes(var SecAttrs: TSecurityAttributes): boolean;
var
pszSidUser: PChar;
szBuff: array [0..1024] of Char;
begin
// pszSidUser at this time contains user SID like this
// S-1-5-21-1454471165-1004336348-1606980848-5555
// TCHAR szBuff[1024]; // I'm not sure with array [0..1024] of Char;
_tcscpy(szBuff, _T("D:"));
_tcscat(szBuff, _T("(A;;GA;;;"));
_tcscat(szBuff, pszSidUser);
_tcscat(szBuff, _T(")"));
_tcscat(szBuff, _T("(A;;GWGR;;;AN)"));
_tcscat(szBuff, _T("(A;;GWGR;;;WD)"));
...
_tcscat(szBuff, _T("S:(ML;;NW;;;S-1-16-0)"));
end;
对于那些对链接中的完整代码感兴趣的人,我可以说这应该是一个技巧,如何访问网络管道,以便在 Windows Vista 上以匿名用户身份进行编写。整篇文章请遵循 这个链接。
感谢您的宝贵时间
问候
I'm converting this code from C++ to Delphi but I don't get the following part of the code. Can anyone explain me what the following code means; what's happening to the szBuff buffer ?
I'm pretty sure it's such kind of formatting (replacement), but I don't even know what is expected as a result and I can't find any sensible documentation of the used functions (maybe I'm just a lame :)
Can anyone help me with the translation of this code to Delphi (or direct me to proper documentation) ?
I don't like this how do you convert kind of questions by myself, so I mentioned at least function names in the question title so it might searchable to someone else in the future.
function TSecInfo.BuildSecurityAttributes(var SecAttrs: TSecurityAttributes): boolean;
var
pszSidUser: PChar;
szBuff: array [0..1024] of Char;
begin
// pszSidUser at this time contains user SID like this
// S-1-5-21-1454471165-1004336348-1606980848-5555
// TCHAR szBuff[1024]; // I'm not sure with array [0..1024] of Char;
_tcscpy(szBuff, _T("D:"));
_tcscat(szBuff, _T("(A;;GA;;;"));
_tcscat(szBuff, pszSidUser);
_tcscat(szBuff, _T(")"));
_tcscat(szBuff, _T("(A;;GWGR;;;AN)"));
_tcscat(szBuff, _T("(A;;GWGR;;;WD)"));
...
_tcscat(szBuff, _T("S:(ML;;NW;;;S-1-16-0)"));
end;
For those who are interested in what's the whole code from the link about I can tell it should be a trick how to access network pipes for writing as an anonymous user on Windows Vista above. To the whole article follow this link.
Thanks for your time
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
_tcscpy
和_tcscat
是 C 标准库函数strcpy
和strcat
的TCHAR
宏版本用于复制和连接 C 字符串。它们评估为 ANSI 或 Unicode 版本,具体取决于您所针对的项目是否或类型。在我看来,这实际上是 C 代码而不是 C++ 代码。在 Delphi 中,您只需使用如下字符串变量:
大概在 C 代码中,有一个对另一个接收
LPCTSTR
的 Windows API 函数的调用。 C 代码将传递szBuff
,但您可以简单地传递PChar(Buff)
,如我上面所示。C 代码使用固定长度缓冲区,因为它没有可用的动态分配字符串类,如 Delphi 的
string
或 C++ 中的std::string
。像这样的固定长度缓冲区通常会导致缓冲区溢出。在 Delphi 中,如果可以避免的话,不要使用固定长度的缓冲区。这是一个典型的例子,说明了为什么具有内置字符串处理功能的语言比 C 语言更容易使用。
_tcscpy
and_tcscat
areTCHAR
macro versions of C standard library functionsstrcpy
andstrcat
for copying and concatenating C strings. They evaluate to ANSI or Unicode versions depending on whether or the type of project you are targeting. It's really C code rather than C++ code in my view.In Delphi you would simply use string variables like this:
Presumably in the C code there is a call to another Windows API function that receives an
LPCTSTR
. The C code will passszBuff
but you can simply passPChar(Buff)
as I have shown above.The C code is using a fixed length buffer because it doesn't have available a dynamically allocated string class like Delphi's
string
orstd::string
in C++. Fixed length buffers like this often lead to buffer overruns. In Delphi don't use a fixed length buffer if you can avoid it.This is a classic example of why languages with built in string handling are so much easier to work with than C.
看起来代码正在使用 TCHARS,基本上它们是一个宏,使得从 unicode 到非 unicode 更容易。
_tcscpy
正在将参数复制到szBuff
,_tcscat
正在将参数附加到szBuff
。如果您熟悉strcpy
和strcat
,它们会做同样的事情。It looks like the code is using TCHARS, basically they are a macro which makes going from unicode to non-unicode easier.
_tcscpy
is copying the parameter toszBuff
,_tcscat
is appending the parameter toszBuff
. If you are familar withstrcpy
andstrcat
they do the same thing.