Zero WCHAR Conversion

I have this one

WCHAR file_name [1];

as the return value from a function (this is a sys 32 function, so I cannot change the return type). I need to make fileName equal to zero, so I'm trying to add "\ 0" to it, but nothing works.

As soon as I get the zero completed WCHAR, I will need to pass it to another sys 32 function, so I need it to remain as WCHAR.

Can someone give me some suggestion?

================================================= =

Many thanks for your help. It looks like my problem is more than the lack of a null terminated string.

//It works:

WCHAR szPath1[50] = L"\\Invalid2.txt.txt"; dwResult = FbwfCommitFile(szDrive, pPath1); //Successful 

//It does not mean:

 std::wstring l_fn(L"\\"); //Because Cache_detail->fileName is \Invalid2.txt.txt and I need two l_fn.append(Cache_detail->fileName); l_fn += L""; //To ensure null terminated fprintf(output, "l_fn.c_str: %ls\n", l_fn.c_str()); //Prints "\\Invalid2.txt.txt" iCommitErr = FbwfCommitFile(L"C:", (WCHAR*)l_fn.c_str()); //Unsuccessful 

// Then, when I do a comparison on these two, they are unequal.

 int iCompareResult = l_fn.compare(pPath1); // returns -1 

So, I need to find out how these two turned out to be different.

Thank you so much!

+3
source share
4 answers

Since you mentioned fbwffindfirst / fbwffindnext in the comment, you are talking about the file name that was returned in FbwfCacheDetail . Therefore, from the fileNameLength field, you know the length for fileName in bytes. The length of the file name in WCHAR is fileNameLength / sizeof (WCHAR). So the simple answer is that you can set

 fileName[fileNameLength/sizeof(WCHAR)+1] = L'\0' 

Now this is important , you need to make sure that the buffer that you send for the cacheDetail parameter in fbwffindfirst / fbwffindnext is sizeof (WCHAR) bytes larger than you need, the above code fragment can work outside the bounds of your array. Therefore, for the size parameter fbwffindfirst / fbwffindnext, specify the buffer size - sizeof (WCHAR).

For example:

 // *** Caution: This example has no error checking, nor has it been compiled *** ULONG error; ULONG size; FbwfCacheDetail *cacheDetail; // Make an intial call to find how big of a buffer we need size = 0; error = FbwfFindFirst(volume, NULL, &size); if (error == ERROR_MORE_DATA) { // Allocate more than we need cacheDetail = (FbwfCacheDetail*)malloc(size + sizeof(WCHAR)); // Don't tell this call about the bytes we allocated for the null error = FbwfFindFirstFile(volume, cacheDetail, &size); cacheDetail->fileName[cacheDetail->fileNameLength/sizeof(WCHAR)+1] = L"\0"; // ... Use fileName as a null terminated string ... // Have to free what we allocate free(cacheDetail); } 

Of course, you will need to change a good bit to match your code (plus you will also need to call fbwffindnext)

If you are wondering why the FbwfCacheDetail structure ends with the WCHAR field [1], see this blog post . This is a fairly common pattern in the Windows API.

+3
source

Use L '\ 0', not '\ 0'.

+2
source

Since each WCHAR character is 16 bits in size, you should add \0\0 to it, but I'm not sure if this works. By the way, WCHAR fileName[1]; creates a WCHAR length 1, maybe you need something like a WCHAR fileName[1024]; .

0
source

WCHAR fileName[1]; - an array of 1 character, therefore, if zero is completed, it will contain only the zero delimiter L'\0' .

What API function are you calling?

Edited

The fileName element in FbwfCacheDetail is only 1 character, which is the common method used when the length of the array is unknown and the member is the last member in the structure. Since you probably already noticed that the allocated allocated buffer is sizeof (FbwfCacheDetail) length, then FbwfFindFirst returns ERROR_NOT_ENOUGH_MEMORY .

So, if I understand, what you want to do is return a NULL name with a completed name using fprintf. This can be done as follows:

 fprintf (outputfile, L"%.*ls", cacheDetail.fileNameLength, cacheDetail.fileName); 

This will print only the first fileNameLength characters of fileName .

An alternative approach is to add a NULL terminator to the end of fileName. First you need to make sure the buffer is long enough, which can be done by subtracting sizeof (WCHAR) from the size argument, which you pass to FbwfFindFirst. Therefore, if you allocate a buffer of size 1000 bytes, you will pass 998 to FbwfFindFirst, having reserved the last two bytes in the buffer for your own use. Then, to add the NULL terminator and display the file name, use

 cacheDetail.fileName[cacheDetail.fileNameLength] = L'\0'; fprintf (outputfile, L"%ls", cacheDetail.fileName); 
0
source

All Articles