Thread: Unicode c++, unable to write to the pipe.

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    5

    Unicode c++, unable to write to the pipe.

    Goal: To be able to send the data to the pipe after serializing using msgpack(have attached the link to the header files).


    insert
    Code:
    LPDWORD  lpNumberOfBytesWritten = 0;
    CStringA strEvalResultW= "abcdefghijklmnopqrstuwxyz";
            
            //// serialize the object into the buffer.
            //// any classes that implements write(const char*,size_t) can be a buffer.
        std::stringstream buffer;
        msgpack::type::tuple<std::string, std::string> src("SECURITY_BASELINE_BLOB", (LPCSTR)strEvalResultW);
        msgpack::pack(buffer,src);
            
        printf("\n\nThe length of the string is %ld \n\n", strlen(strEvalResultW));
        BOOL result = WriteFile(hPipe, (LPCVOID)buffer.str(), strlen(buffer), lpNumberOfBytesWritten, NULL);
        if (result) {
            printf("\Successfully wrote to the pipe \n");
        }

    I am fairly new to unicode in c++ and very confused about why I am not getting the right arguments for writeFile..(I am trying to write to a pipe and have been able to connect to the pipe successfully, hPipe is the handler to the pipe).

    I want to be able to send the packed data(using msg pack available at NuGet Gallery
    | msgpack-c 0.5.9.1
    ) to the pipe. Any help will be really appreciated and I am willing to provide more details if needed. I am not too sure of the highlighted code as well. The highlighted code in writeFile is failing.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,895
    Is it logical to assume Unicode data has no /0 bytes in it?
    Because your code assumes that is true.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    5

    Post unable to understand your reply..

    Code:
    LPDWORD  lpNumberOfBytesWritten = 0;
    CStringA testString= "abcdefghijklmnopqrstuwxyz";
            
            
    msgpack::sbuffer buffer;    LPCSTR toWrite = testString;
    
    msgpack::pack(buffer,toWrite);
            
    printf("\n\nThe length of the string is %ld \n\n", strlen(strEvalResultW));
    BOOL result = WriteFile(hPipe, buffer.data(), buffer.size(), lpNumberOfBytesWritten, NULL);
        if (result) {
            printf("\Successfully wrote to the pipe \n");
        }
    I have edited the question, I also don't clearly understand what you mean. Can you help me with this?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3,895
    The function strlen() and likely the .str() assumes you are dealing with C string this is an /0 terminated char array.
    C string can not have embedded /0 in it!

    Therefore, you can NOT use UTF-16 with your code!

    MS Windows uses some version of UTF-16.

    Edit: I am not an expert on Unicode; you should verify what I post in this thread.

    Tim S.
    Last edited by stahta01; 3 Weeks Ago at 10:07 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    971
    This seems to have very little (if anything) to do with Unicode.

    Are you knowingly using ATL/MFC?
    Are you sure you want to use CString (more specifically, for some reason, CStringA)?
    Using CString | Microsoft Docs

    Are you sure you want to use this NuGet thing?
    Why?
    What is NuGet and what does it do? | Microsoft Docs

    This is incorrect:
    Code:
    LPDWORD lpNumberOfBytesWritten = 0;
    Presumably you mean
    Code:
    DWORD numberOfBytesWritten = 0;
    which you would pass to WriteFile as a pointer like &numberOfBytesWritten.

    It may be best to describe what exactly you are trying to do in general terms, without reference to any specific technologies.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  6. #6
    Registered User
    Join Date
    Sep 2020
    Posts
    5
    I already have some string in CStringA. My main goal is to be able to serialize this data using msgpack (MessagePack: It's like JSON. but fast and small.) and be able to send it to the pipe. The reason I am using the nuget package is for the header files.

    Thank you for the suggestion for the numberOfBytesWritten, I agree with the change.
    Also, to answer your question, yes I am knowingly using ATL/MFC because the code already uses it and I am just trying to add the functionality to send it to the pipe. I have replaced the CStringA with some random string in this case.

    The pipe can accept data in fluentd protocol only(hence using msgpack) in my case. I basically want to pack(serialize this data) and send it across.

    Thank you so much for your reply. Any help would be appreciated. I am fairly new to Unicode and even though I am reading a lot about it, I am not understanding how to send that data.

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    971
    I don't see what this has to do with Unicode.
    Nothing, apparently, or at least the underlying UTF-8 encoding is not part of your problem.
    But you obviously can't use strlen on a stringstream.
    How about buffer.str().length().
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  8. #8
    Registered User
    Join Date
    Sep 2020
    Posts
    5
    Got it, so you are saying that UTF-8 is not a problem in this case. I am just trying to understand how to send the data using msgpack.Initially, I thought the way my data is formatted could be a problem...
    In that case, if I want to use the msgpack-c "pack" function, can I change the name of the thread or create a new thread for the same?

    Also, thank you so much for your prompt replies Means a lot!

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    971
    You've lost me. I don't see any mention of threads.

    A nitpick: you should probably remove the W at the end of strEvalResultW since that would usually indicate you are using CStringW (char16_t-based) instead of CStringA (char-based). Just strEvalResult is good.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. timeout on write() for linux pipe
    By waza123 in forum C Programming
    Replies: 2
    Last Post: 02-22-2020, 09:13 AM
  2. write to a pipe and so on
    By sharonch in forum Windows Programming
    Replies: 0
    Last Post: 12-07-2012, 01:41 PM
  3. unable to write to a file
    By roaan in forum C Programming
    Replies: 2
    Last Post: 08-07-2009, 08:18 AM
  4. Replies: 2
    Last Post: 12-06-2008, 02:17 PM
  5. write() with pipe()
    By und3rdog in forum C Programming
    Replies: 11
    Last Post: 05-03-2003, 10:57 PM

Tags for this Thread