如何将C ++ _tcscpy,_tcscat转换为Delphi? [英] How do you convert C++ _tcscpy, _tcscat to Delphi?

查看:217
本文介绍了如何将C ++ _tcscpy,_tcscat转换为Delphi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在转换此代码从C ++到Delphi,但我没有得到以下部分的代码。谁能解释我下面的代码意味着什么? szBuff缓冲区发生了什么?

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 :)

任何人都可以帮助我翻译这段代码到Delphi(或指示我正确的文档)?

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;

对于那些对链接中的整个代码感兴趣的人,我可以告诉它应该是一个窍门如何访问网络管道写作为匿名用户在Windows Vista上面。对于整篇文章,

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

推荐答案

_tcscpy _tcscat TCHAR C标准库函数的宏版本用于复制和连接C字符串的strcpy strcat 。它们根据您定位的项目类型或类型评估为ANSI或Unicode版本。在我看来,它真的是C代码而不是C ++代码。

_tcscpy and _tcscat are TCHAR macro versions of C standard library functions strcpy and strcat 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.

在Delphi中,您只需使用如下的字符串变量:

In Delphi you would simply use string variables like this:

function TSecInfo.BuildSecurityAttributes(var SecAttrs: TSecurityAttributes): boolean;
var
  pszSidUser: PChar;
  Buff: string;
begin
  // pszSidUser at this time contains user SID like this
  // S-1-5-21-1454471165-1004336348-1606980848-5555

  Buff := 'D:(A;;GA;;;'+pszSidUser+')(A;;GWGR;;;AN)(A;;GWGR;;;WD)S:(ML;;NW;;;S-1-16-0)';
  SomeOtherWindowsAPICall(PChar(Buff));    
end;

可能在C代码中有一个调用另一个Windows API函数接收 LPCTSTR 。 C代码将通过 szBuff ,但你可以简单地传递 PChar(Buff),如上面所示。

Presumably in the C code there is a call to another Windows API function that receives an LPCTSTR. The C code will pass szBuff but you can simply pass PChar(Buff) as I have shown above.

C代码使用固定长度的缓冲区,因为它没有像Delphi的 string 这样的动态分配的字符串类 std :: string 。这样的固定长度缓冲区经常导致缓冲区溢出。在Delphi中不要使用固定长度的缓冲区,如果你可以避免它。

The C code is using a fixed length buffer because it doesn't have available a dynamically allocated string class like Delphi's string or std::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.

这是一个典型的例子为什么内置字符串处理的语言更容易工作而不是C。

This is a classic example of why languages with built in string handling are so much easier to work with than C.

这篇关于如何将C ++ _tcscpy,_tcscat转换为Delphi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆