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

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

问题描述

我需要将一个字符串格式化为双NULL结束的字符串,以便使用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('\0') + _T('\0');

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

  //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);

有没有人有这个想法?

Does anyone has idea on this?

是否有其他方法追加双端字符串?

Is there other way to append double-terminated string?

推荐答案

CString类本身对于包含空字符的字符串没有问题。问题出在将空字符放在字符串中。第一个示例工作,因为它是附加一个单一的字符,而不是一个字符串 - 它接受字符作为是没有检查,看看它是否为null。第二个示例尝试附加一个典型的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.

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

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