Results 1 to 2 of 2

Thread: wchar_t vs QString support with QT IPC server and Windows Natice IPC Client

  1. #1
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default wchar_t vs QString support with QT IPC server and Windows Natice IPC Client

    all i have a QT app that opens a IPC port on windows and waits for a connection from a client... when i connect with QT client its fine, however a native windows app, it fails to get the proper data. I am convinced its a character encoding issue. But im unsure as to the *best* way to address it... i can alter either the QT app or the Native windows app..This will only run on windows, so portability isnt a concern. The Client MUST be native windows, so i cant do all QT... (i wish believe me)

    I see the count of the data received in the client as "READ(30)", so i know data is going...

    QT IPC Socket Code
    Qt Code:
    1. void LocalSocketIpcServer::returnPrinter(QString printer)
    2. {
    3.  
    4. PRN_DEBUG() << "Returning: " << printer;
    5.  
    6. QByteArray block;
    7. QDataStream out(&block, QIODevice::ReadWrite);
    8. out.setVersion(QDataStream::Qt_4_0);
    9. out << (quint16)0;
    10. out << printer;
    11. out.device()->seek(0);
    12. out << (quint16)(block.size() - sizeof(quint16));
    13.  
    14. QLocalSocket *clientConnection = m_server->nextPendingConnection();
    15.  
    16. connect(clientConnection, SIGNAL(disconnected()),
    17. clientConnection, SLOT(deleteLater()));
    18.  
    19. clientConnection->write(block);
    20. clientConnection->flush();
    21. clientConnection->disconnectFromServer();
    22.  
    23. emit finishedSending();
    24. }
    To copy to clipboard, switch view to plain text mode 


    Native Windows client
    Qt Code:
    1. #include <windows.h>
    2. #include <conio.h>
    3. #include <iostream>
    4. using namespace std;
    5.  
    6. const wchar_t cszPipeName[] = L"\\\\.\\pipe\\TestPort";
    7.  
    8. int main(int argc, wchar_t* argv[])
    9. {
    10. HANDLE hPipe = INVALID_HANDLE_VALUE;
    11.  
    12. do {
    13.  
    14. // Get handle to a read only pipe that must exists.
    15. hPipe = CreateFileW(
    16. cszPipeName,
    17. GENERIC_READ, // | GENERIC_WRITE,
    18. 0,
    19. NULL,
    20. OPEN_EXISTING,
    21. 0,
    22. NULL
    23. );
    24.  
    25. if( hPipe == INVALID_HANDLE_VALUE ){
    26. cerr << "CreateFile fail. Err: " << GetLastError() << endl;
    27. break;
    28. }
    29.  
    30. cout << "Press any key to exit" << endl;
    31.  
    32. wchar_t szMessage[256] = {0};
    33.  
    34. // Keep buffer null terminated. Don't use last element.
    35. DWORD dwMsgSize = (sizeof(szMessage)-1)*sizeof(wchar_t);
    36.  
    37. //while(!_kbhit()) {
    38.  
    39. // Read messages until an error on the pipe happen
    40. // or the user press any key on the keyboard.
    41. DWORD dwRead = 0;
    42. BOOL bRet = ReadFile(
    43. hPipe,
    44. szMessage,
    45. dwMsgSize,
    46. &dwRead,
    47. NULL
    48. );
    49. if(!bRet) {
    50.  
    51. cerr << "Read fail. Err: " << GetLastError() << endl;
    52. break;
    53. }
    54.  
    55. wcout << "READ (" << dwRead << ") " << szMessage << endl;
    56.  
    57. //} // end while
    58.  
    59. } while(false); // end do
    60.  
    61. if(hPipe != INVALID_HANDLE_VALUE) {
    62. CloseHandle(hPipe);
    63. }
    64.  
    65. return 0;
    66. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: wchar_t vs QString support with QT IPC server and Windows Natice IPC Client

    QDataStream is a Qt serialisation mechanism. For all but the intrinsic types it writes more to the stream than you expect in order that the receiving Qt program can reconstruct the objects by reading from QDataStream. This is certainly the case with QString, which will output a 4 byte length followed by the internal data representing the string (probably a 16-bit ints). Your generic receiver does not account for this.

    If you are sending to a non-Qt receiver then you best avoid QDataStream and use QIODevice::write() directly.

Similar Threads

  1. Replies: 1
    Last Post: 4th December 2010, 17:20
  2. QString -> wchar_t*
    By elizabeth.h1 in forum Qt Programming
    Replies: 0
    Last Post: 26th October 2009, 09:34
  3. Assign a wchar_t array to QString
    By jacky in forum Qt Programming
    Replies: 5
    Last Post: 18th April 2009, 12:28
  4. QString to wchar_t
    By rajveer in forum Qt Programming
    Replies: 1
    Last Post: 4th September 2008, 06:11
  5. QString and wchar_t?
    By jamos in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 18:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.