Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double null-terminated string

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?

like image 553
wengseng Avatar asked Jan 06 '11 02:01

wengseng


People also ask

What is meant by null-terminated string?

In computer programming, a null-terminated string is a character string stored as an array containing the characters and terminated with a null character (a character with a value of zero, called NUL in this article).

What is null-terminated string in C++?

The null terminated strings are basically a sequence of characters, and the last element is one null character (denoted by '\0'). When we write some string using double quotes (“…”), then it is converted into null terminated strings by the compiler.

Why does C use null-terminated strings?

Because in C strings are just a sequence of characters accessed viua a pointer to the first character. There is no space in a pointer to store the length so you need some indication of where the end of the string is. In C it was decided that this would be indicated by a null character.

Is a string always null-terminated?

Yes, the CString is always null terminated.


2 Answers

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.

like image 123
Mark Ransom Avatar answered Sep 24 '22 04:09

Mark Ransom


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.

like image 27
Greg Hewgill Avatar answered Sep 26 '22 04:09

Greg Hewgill