8

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?

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
wengseng
  • 1,330
  • 1
  • 14
  • 27
  • Neither of those *ought* to work. It's just an accident if one of them does. – Greg Hewgill Jan 06 '11 at 02:33
  • @Greg: shouldn't the second example work since CString has the following `friend CString operator+( const CString& string, LPCTSTR lpsz );`? (by work I mean the + operation, not passing `CString` to `SHFileOperation`) – Eugen Constantin Dinca Jan 06 '11 at 02:42
  • @Eugen: No, because the `+` operator of `CString` sees the second operand as an ordinary NUL-terminated `char *`, and it has no right to be looking at memory beyond the *first* NUL terminator. – Greg Hewgill Jan 06 '11 at 03:34
  • @Greg: Makes perfect sense. I was interpreting the "not working" part as it "did not compile". – Eugen Constantin Dinca Jan 06 '11 at 17:38
  • you may want to read this: http://blogs.msdn.com/b/oldnewthing/archive/2010/02/18/9965469.aspx and this http://blogs.msdn.com/b/oldnewthing/archive/2009/10/08/9904646.aspx – juFo Jun 04 '13 at 14:19

2 Answers2

9

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 1
    @Mr_and_Mrs_D Not just single quotes, but single quotes around a *single character*. As Mooing Duck observes, you can put two characters inside the single quotes but you won't append two characters to the string. – Mark Ransom Mar 16 '15 at 22:19
6

You cannot use CString for this purpose. You will need to use your own char[] buffer:

char buf[100]; // or large enough
strcpy(buf, "string to use");
memcpy(buf + strlen(buf), "\0\0", 2);

Although you could do this by only copying one more NUL byte after the existing NUL terminator, I would prefer to copy two so that the source code more accurately reflects the intent of the programmer.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285