Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6973085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:03:47+00:00 2026-05-27T17:03:47+00:00

I have a problem with wchar_t* to char* conversion. I’m getting a wchar_t* string

  • 0

I have a problem with wchar_t* to char* conversion.

I’m getting a wchar_t* string from the FILE_NOTIFY_INFORMATION structure, returned by the ReadDirectoryChangesW WinAPI function, so I assume that string is correct.

Assume that wchar string is “New Text File.txt”
In Visual Studio debugger when hovering on variable in shows “N” and some unknown Chinese letters. Though in watches string is represented correctly.

When I try to convert wchar to char with wcstombs

wcstombs(pfileName, pwfileName, fileInfo.FileNameLength);

it converts just two letters to char* (“Ne”) and then generates an error.

Some internal error in wcstombs.c at function _wcstombs_l_helper() at this block:

if (*pwcs > 255)  /* validate high byte */
{
    errno = EILSEQ;
    return (size_t)-1;  /* error */
}

It’s not thrown up as exception.

What can be the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:03:47+00:00Added an answer on May 27, 2026 at 5:03 pm

    In order to do what you’re trying to do The Right Way, there are several nontrivial things that you need to take into account. I’ll do my best to break them down for you here.

    Let’s start with the definition of the count parameter from the wcstombs() function’s documentation on MSDN:

    The maximum number of bytes that can be stored in the multibyte output string.

    Note that this does NOT say anything about the number of wide characters in the wide character input string. Even though all of the wide characters in your example input string (“New Text File.txt”) can be represented as single-byte ASCII characters, we cannot assume that each wide character in the input string will generate exactly one byte in the output string for every possible input string (if this statement confuses you, you should check out Joel’s article on Unicode and character sets). So, if you pass wcstombs() the size of the output buffer, how does it know how long the input string is? The documentation states that the input string is expected to be null-terminated, as per the standard C language convention:

    If wcstombs encounters the wide-character null character (L’\0′) either before or when count occurs, it converts it to an 8-bit 0 and stops.

    Though this isn’t explicitly stated in the documentation, we can infer that if the input string isn’t null-terminated, wcstombs() will keep reading wide characters until it has written count bytes to the output string. So if you’re dealing with a wide character string that isn’t null-terminated, it isn’t enough to just know how long the input string is; you would have to somehow know exactly how many bytes the output string would need to be (which is impossible to determine without doing the conversion) and pass that as the count parameter to make wcstombs() do what you want it to do.

    Why am I focusing so much on this null-termination issue? Because the FILE_NOTIFY_INFORMATION structure’s documentation on MSDN has this to say about its FileName field:

    A variable-length field that contains the file name relative to the directory handle. The file name is in the Unicode character format and is not null-terminated.

    The fact that the FileName field isn’t null-terminated explains why it has a bunch of “unknown Chinese letters” at the end of it when you look at it in the debugger. The FILE_NOTIFY_INFORMATION structure’s documentation also contains another nugget of wisdom regarding the FileNameLength field:

    The size of the file name portion of the record, in bytes.

    Note that this says bytes, not characters. Therefore, even if you wanted to assume that each wide character in the input string will generate exactly one byte in the output string, you shouldn’t be passing fileInfo.FileNameLength for count; you should be passing fileInfo.FileNameLength / sizeof(WCHAR) (or use a null-terminated input string, of course). Putting all of this information together, we can finally understand why your original call to wcstombs() was failing: it was reading past the end of the string and choking on invalid data (thereby triggering the EILSEQ error).

    Now that we’ve elucidated the problem, it’s time to talk about a possible solution. In order to do this The Right Way, the first thing you need to know is how big your output buffer needs to be. Luckily, there is one final tidbit in the documentation for wcstombs() that will help us out here:

    If the mbstr argument is NULL, wcstombs returns the required size in bytes of the destination string.

    So the idiomatic way to use the wcstombs() function is to call it twice: the first time to determine how big your output buffer needs to be, and the second time to actually do the conversion. The final thing to note is that as we stated previously, the wide character input string needs to be null-terminated for at least the first call to wcstombs().

    Putting this all together, here is a snippet of code that does what you are trying to do:

    size_t fileNameLengthInWChars = fileInfo.FileNameLength / sizeof(WCHAR); //get the length of the filename in characters
    WCHAR *pwNullTerminatedFileName = new WCHAR[fileNameLengthInWChars + 1]; //allocate an intermediate buffer to hold a null-terminated version of fileInfo.FileName; +1 for null terminator
    wcsncpy(pwNullTerminatedFileName, fileInfo.FileName, fileNameLengthInWChars); //copy the filename into a the intermediate buffer
    pwNullTerminatedFileName[fileNameLengthInWChars] = L'\0'; //null terminate the new buffer
    size_t fileNameLengthInChars = wcstombs(NULL, pwNullTerminatedFileName, 0); //first call to wcstombs() determines how long the output buffer needs to be
    char *pFileName = new char[fileNameLengthInChars + 1]; //allocate the final output buffer; +1 to leave room for null terminator
    wcstombs(pFileName, pwNullTerminatedFileName, fileNameLengthInChars + 1); //finally do the conversion!
    

    Of course, don’t forget to call delete[] pwNullTerminatedFileName and delete[] pFileName when you’re done with them to clean up.

    ONE LAST THING

    After writing this answer, I reread your question a bit more closely and thought of another mistake you may be making. You say that wcstombs() fails after just converting the first two letters (“Ne”), which means that it’s hitting uninitialized data in the input string after the first two wide characters. Did you happen to use the assignment operator to copy one FILE_NOTIFY_INFORMATION variable to another? For example,

    FILE_NOTIFY_INFORMATION fileInfo = someOtherFileInfo;
    

    If you did this, it would only copy the first two wide characters of someOtherFileInfo.FileName to fileInfo.FileName. In order to understand why this is the case, consider the declaration of the FILE_NOTIFY_INFORMATION structure:

    typedef struct _FILE_NOTIFY_INFORMATION {
      DWORD NextEntryOffset;
      DWORD Action;
      DWORD FileNameLength;
      WCHAR FileName[1];
    } FILE_NOTIFY_INFORMATION, *PFILE_NOTIFY_INFORMATION;
    

    When the compiler generates code for the assignment operation, it does’t understand the trickery that is being pulled with FileName being a variable length field, so it just copies sizeof(FILE_NOTIFY_INFORMATION) bytes from someOtherFileInfo to fileInfo. Since FileName is declared as an array of one WCHAR, you would think that only one character would be copied, but the compiler pads the struct to be an extra two bytes long (so that its length is an integer multiple of the size of an int), which is why a second WCHAR is copied as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have problem with fancybox. I want to write a function that will run
I have a problem understanding why a certain implicit conversion is not working as
I have a structure defined in my header file: struct video { wchar_t* videoName;
I have problem while sending messages from android to PC. Messages are send when
I want to send my C# string to a C++ DLL function. I have
I have a problem: I use SendMessage from a procedure in a DLL to
Still working on a problem that started from here Calling C++ dll function from
I have a problem with getting PCSC reader serial number if card is not
I have problem in some JavaScript that I am writing where the Switch statement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.