CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2013
    Posts
    22

    sending usigned char to listbox

    Got something like the following. A button that read characters from a thrid party tool and sends it to a listbox.
    But the contant is not readable.

    Code:
    void dlg::sendtolistbox()
    {
      unsigned char buf[250];
      thirdparty.GetData(buf, len);
      Sendmessage(hndl, listboxupdate,0 , (LPARAM)buf);
    }
    void Mydlg::UpdateListBox(wparam a,laparm b)
    {
      m_listbox.AddString((LPCTSTR)b);
    }
    the characters show up in the list box as short unreadable characters. like it is chopped.
    If i change to : Sendmessage(hndl, listboxupdate,0 , (LPARAM)&buf[15]);
    Then I can see readable valid strings of up to 50 characters and then empty unreadable characters afterwards. I tried all kinds of things , including using CString, still did not work.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: sending usigned char to listbox

    What is len?

    Does GetData() append a NULL terminator to the end of the data obtained into buf?

    Have you debugged the program using the debugger and examined the contents of buf in hex to see exactly what is there?

    Are you mixing Unicode from GetData() and multi-char in the program (or vice/versa)?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Feb 2013
    Posts
    22

    Re: sending usigned char to listbox

    when I write the buf to file using WriteFile, the content of file is: 15 unreadable characters followed by 50 readable characters and the rest are I'..
    When I do the following, I get the 50 readable characters in the listbox.
    buf[len]='\0;
    Sendmessage(hndl, listboxupdate,0 , (LPARAM)&buf[14]);

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: sending usigned char to listbox

    Have you examined the contents of the file in hex with something like HxD to see exactly what is being received? What would you expect to be received against what you are actually receiving?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Feb 2013
    Posts
    22

    Re: sending usigned char to listbox

    Good suggestion. Looked the file up under an hex editor. The first 8 bytes where the number of bytes. Then after, the bytes for the actual text that seem to be correct and the then the 2FEF bytes. Now how one can display all the bytes as readable in the listbox without subclassing the listbox?. As of now , in the list box, I can only display/read the text that comes after the number of bytes?.
    Last edited by tcp1; April 11th, 2015 at 12:47 PM.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: sending usigned char to listbox

    What you are seeing is a length-prefixed string (or sometimes called a Pascal string) as opposed to a null-terminated string as used in c/c++.
    See http://en.wikipedia.org/wiki/String_...ength-prefixed

    However, if the first 8 bytes is the number of bytes in the string what about the next 7 bytes before the readable characters start - as you said in post #3 that the first 15 chars were unreadable followed by 50 readable chars?

    Can you attach the file?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Feb 2013
    Posts
    22

    Re: sending usigned char to listbox

    I do not have the access to the file at this moment. This is a new protocol, 8 bytes + the appended string... This is the new library ... with new protocol ... That 15 bytes is replaced with 8 bytes ... ...

  8. #8
    Join Date
    Feb 2013
    Posts
    22

    Re: sending usigned char to listbox

    As I remember, with the new protocol the first 8 bytes in the file, when I used the notepad as the viewer, were extended ASCII characters and maybe these are not supported by Microsoft, VS?.
    http://www.theasciicode.com.ar/

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: sending usigned char to listbox

    As long as this is about file, the non-askii bytes are most probably BOM (http://en.m.wikipedia.org/wiki/Byte_order_mark)
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured