1

Some Win32 API structures require to concatenate an extra null character to a string, as in the following example taken from here:

c:\temp1.txt'\0'c:\temp2.txt'\0''\0'

When it comes to wide strings, what is the easiest way to append a L'\0' to the end of an existing wide string?

Here's what works for me but seems too cumbersome:

wchar_t my_string[10] = L"abc";
size_t len = wcslen(my_string);
wchar_t nullchar[1] = {'\0'};
memcpy(my_string + len + 1, nullchar, sizeof(wchar_t));
4
  • 1
    assuming my_string is long enough my_string[wcslen(my_string)+1]='\0'? Commented Oct 13, 2020 at 7:50
  • That simple... and it even worked. Please post as a reply. Commented Oct 13, 2020 at 8:11
  • You tagged your question as [c++], but your code seems more like C to me; e.g. in C++ you would use std::wstring (which is capable of storing embedded nulls), instead of raw wchar_t C-style arrays. Commented Oct 13, 2020 at 9:09
  • Fixed. @AlanBirtles please post your comment as an answer. Commented Oct 13, 2020 at 9:22

4 Answers 4

3

In your example you can just assign the value just like any other array. There's nothing special about wchar_t here.

my_string already has a single null-termination, so if you want double null-termination, then just add another 0 after it.

wchar_t my_string[10] = L"abc";
size_t len = wcslen(my_string);
// todo: check out-of-bounds
my_string[len + 1] = 0;

Or even simpler, if it's really just a string literal,

wchar_t my_string[10] = L"abc\0";

This will be doubly-null-terminated.

Sign up to request clarification or add additional context in comments.

1 Comment

in fact, it will even be sevenly-null-terminated :)
2

If you use std::wstring instead of wchar_t[], you can use its operator+= to append the extra null terminator, eg:

wstring my_string = L"abc";
...
my_string += L'\0';
// use my_string.c_str() as needed...

6 Comments

Confirmed that myWStr + L'\0' works, but myWStr + L"\0" (with double quotes as a string) doesn't. Any idea why?
@geneb. L'\0' is a single character, the operator has no choice but to add it to the wstring. But L"\0"` is a character string, and the operator treats it as a null terminated character string thus ignoring the \0 character, same as if you had written it as L"" instead.
Thanks; also, how would you collocate contiguous null-terminated strings to arrange a structure like the multi-file SHFileOperation requires?
@geneb. that's already covered in @Botje's answer (just note my comments on it).
std::vector<wchar_t> and std::wstring accomplish the same thing. However, with vector, you have to add the final terminator explicitly, whereas with wstring the final terminator is implicit.
|
0

assuming my_string is long enough:

my_string[wcslen(my_string)+1]='\0';

The terminating null will be translated to a wide char.

(Posted as a first comment to the question)

1 Comment

I am glad you have got your solution and thanks for your sharing, I would appreciate it if you mark them as answer and this will be beneficial to other community.
-1

Assuming you have the various paths in a std::vector<std::wstring>, you can just build the required format in a loop:

std::vector<std::wstring> paths;
paths.emplace_back(L""); // This empty path will add the extra NUL
std::wstring buf(1000, 0);
for (auto p : paths) {
    buf.append(p);
    buf.append(1, 0);
}
wchar_t *ptr = buf.c_str(); // Now do stuff with it

1 Comment

std::wstring buf(1000, 0); creates a string with 1000 initial \0 characters in it, then you append to the end of the string. That does not produce the desired output. You need to use std::wstring buf; buf.reserve(1000); instead. In which case the reserve() is optional. Also, the emplace_back(L"") on the vector is not needed either since wstring is guaranteed null terminated since C++11. Try this: std::vector<std::wstring> paths; ... std::wstring buf; buf.reserve(1000); for (auto&& p : paths) { buf.append(p); buf += L'\0'; } wchar_t *ptr = buf.c_str();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.