双空终止字符串 [英] Double null-terminated string

查看:23
本文介绍了双空终止字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了使用 SHFileOperation,我需要将字符串格式化为双空终止字符串.

I need to format a string to be double null-terminated string in order to use SHFileOperation.

有趣的部分是我发现以下工作之一,但不是两个:

Interesting part is i found one of the following working, but not both:

  // Example 1
  CString szDir(_T("D:\Test"));
  szDir = szDir + _T('') + _T('');

  // Example 2  
  CString szDir(_T("D:\Test"));
  szDir = szDir + _T("");

  //Delete folder
  SHFILEOPSTRUCT fileop;
  fileop.hwnd   = NULL;    // no status display
  fileop.wFunc  = FO_DELETE;  // delete operation
  fileop.pFrom  = szDir;  // source file name as double null terminated string
  fileop.pTo    = NULL;    // no destination needed
  fileop.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;  // do not prompt the user
  fileop.fAnyOperationsAborted = FALSE;
  fileop.lpszProgressTitle     = NULL;
  fileop.hNameMappings         = NULL;
  int ret = SHFileOperation(&fileop);

有人对此有想法吗?

还有其他方法可以附加双终止字符串吗?

Is there other way to append double-terminated string?

推荐答案

CString 类本身对包含空字符的字符串没有问题.问题在于首先将空字符放入字符串中.第一个示例有效,因为它附加的是单个字符,而不是字符串 - 它按原样接受字符,而不检查它是否为空.第二个示例尝试附加一个典型的 C 字符串,根据定义,该字符串在第一个空字符处结束 - 您实际上是在附加一个空字符串.

The CString class itself has no problem with a string containing a null character. The problem comes with putting null characters into the string in the first place. The first example works because it is appending a single character, not a string - it accepts the character as is without checking to see if it's null. The second example tries appending a typical C string, which by definition ends at the first null character - you're effectively appending an empty string.

这篇关于双空终止字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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