Results 1 to 9 of 9

Thread: how to convert a QString into a PCHAR

  1. #1
    Join Date
    Aug 2010
    Posts
    32
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default how to convert a QString into a PCHAR

    Hello everyone,
    I have a function that the parameter is a PCHAR.
    So how can I convert a QString into a PCHAR to pass it as a parameter in my function?

    To be more clear, that's the text I am trying to pass:

    QString vid_pid = "vid_04d8&pid_1510";

    And that's my function:

    DWORD *MPUSBGetDeviceCount(PCHAR pVID_PID);

    Any Ideas?

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to convert a QString into a PCHAR

    Qt Code:
    1. QString vid_pid = "vid_04d8&pid_1510";
    2.  
    3. x = MPUSBGetDeviceCount(vid_pid.toLocal8Bit().constData());
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2010
    Posts
    32
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to convert a QString into a PCHAR

    Quote Originally Posted by squidge View Post
    Qt Code:
    1. QString vid_pid = "vid_04d8&pid_1510";
    2.  
    3. x = MPUSBGetDeviceCount(vid_pid.toLocal8Bit().constData());
    To copy to clipboard, switch view to plain text mode 
    What the diference between do what you said e do it:

    Qt Code:
    1. QString vid_p = "vid_04d8&pid_1510"; // VID e PID
    2.  
    3. PCHAR vid_pid = vid_p.toLocal8Bit().constData();
    4.  
    5. x = MPUSBGetDeviceCount(vid_pid);
    To copy to clipboard, switch view to plain text mode 

    Because it's not working for me. I get the following error:
    - invalid conversion from 'const char*' to 'CHAR*' in the third line.


    I tried to do as you said now and I get the same error message
    Last edited by digog; 10th February 2011 at 19:41.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to convert a QString into a PCHAR

    PCHAR vid_pid = vid_p.toLocal8Bit().constData();
    Because 'vid_p.toLocal8Bit().constData()' returns 'const char*' and PCHAR is 'char*' ( CHAR is just a typedef for 'char' ).
    Will this function 'MPUSBGetDeviceCount(PCHAR param)' change the 'param' ?
    If yes, then you need to make a local copy of string returned by 'toLocal8Bit().constData()' and pass the copy as parameter.
    Otherwise you can try to cast 'const char*' to 'char*'.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to convert a QString into a PCHAR

    use .data instead of .constData

    Qt Code:
    1. QString vid_pid = "vid_04d8&pid_1510";
    2.  
    3. x = MPUSBGetDeviceCount(vid_pid.toLocal8Bit().data());
    To copy to clipboard, switch view to plain text mode 

    Not however that this will NOT work:

    Qt Code:
    1. QString vid_p = "vid_04d8&pid_1510"; // VID e PID
    2.  
    3. PCHAR vid_pid = vid_p.toLocal8Bit().data(); // or .constData
    4.  
    5. x = MPUSBGetDeviceCount(vid_pid);
    To copy to clipboard, switch view to plain text mode 

    It'll compile, but the pointer will be invalid as the intermediate QByteArray will be destroyed, therefore you need to do it as above in the one line or assign to a QByteArray yourself that is more permanent:

    Qt Code:
    1. QByteArray ba_vid_pid = vid_p.toLocal8Bit();
    2. PCHAR vid_pid = ba_vid_pid.data();
    3.  
    4. x = MPUSBGetDeviceCount(vid_pid); // OK
    To copy to clipboard, switch view to plain text mode 
    Last edited by squidge; 10th February 2011 at 20:07.

  6. #6
    Join Date
    Aug 2010
    Posts
    32
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to convert a QString into a PCHAR

    First of all, thanks for the help.

    I tried to do as you said here:
    Quote Originally Posted by squidge View Post
    use .data instead of .constData

    Qt Code:
    1. QString vid_pid = "vid_04d8&pid_1510";
    2.  
    3. x = MPUSBGetDeviceCount(vid_pid.toLocal8Bit().data());
    To copy to clipboard, switch view to plain text mode 
    but still did not work. So when I wrote the QString as const QString it worked.
    Like this:
    file.h
    Qt Code:
    1. ...
    2. const QString vid_pid = "vid_04d8&pid_1510";
    3. ...
    To copy to clipboard, switch view to plain text mode 
    file.cpp
    Qt Code:
    1. ...
    2. x = MPUSBGetDeviceCount(vid_pid.toLocal8Bit().data());
    3. ...
    To copy to clipboard, switch view to plain text mode 

    When I tried to do what you said here it didnt work
    It'll compile, but the pointer will be invalid as the intermediate QByteArray will be destroyed, therefore you need to do it as above in the one line or assign to a QByteArray yourself that is more permanent:

    Qt Code:
    1. QByteArray ba_vid_pid = vid_p.toLocal8Bit();
    2. PCHAR vid_pid = ba_vid_pid.data();
    3.  
    4. x = MPUSBGetDeviceCount(vid_pid); // OK
    To copy to clipboard, switch view to plain text mode 
    I did like this:
    file.h
    Qt Code:
    1. ...
    2. QString vid_p = "vid_04d8&pid_1510";
    3. QByteArray ba_vid_pid = vid_p.toLocal8Bit();
    4. PCHAR vid_pid = ba_vid_pid.data();
    5. ...
    To copy to clipboard, switch view to plain text mode 
    file.cpp
    Qt Code:
    1. ...
    2. x = MPUSBGetDeviceCount(vid_pid);
    3. ...
    To copy to clipboard, switch view to plain text mode 
    And I get the error message: "collect2: ld returned 1 exit status"

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to convert a QString into a PCHAR

    And I get the error message: "collect2: ld returned 1 exit status"
    Probably linker could not find needed files, could you show us your .pro file ?
    If you have a line in your .pro
    Qt Code:
    1. LIBS += -l<dll name>
    To copy to clipboard, switch view to plain text mode 
    then you should add
    Qt Code:
    1. LIBS += -L"path/to/dir/with/dll/file"
    To copy to clipboard, switch view to plain text mode 
    in order to point the linker to directory where it should search for the referenced libraries.

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to convert a QString into a PCHAR

    Can you post your entire build log? Typically there is more than one error when you get "ld returned 1 error status".

    Secondly, I would advise against putting executable statements in header files, unless they are part of inline class methods.

    Also, if you could be a bit more informative instead of "It didn't work", would could try to help more.

  9. #9
    Join Date
    Aug 2010
    Posts
    32
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to convert a QString into a PCHAR

    Ok, I will write my entire code here.
    testempusbapi.pro
    Qt Code:
    1. TARGET = testempusbapi
    2. TEMPLATE = app
    3. SOURCES += main.cpp \
    4. testempusb.cpp \
    5. picconnector.cpp
    6. HEADERS += testempusb.h \
    7. mpusbapi.h \
    8. picconnector.h
    To copy to clipboard, switch view to plain text mode 
    picconnector.h
    Qt Code:
    1. #ifndef PICCONNECTOR_H
    2. #define PICCONNECTOR_H
    3.  
    4. #include <QWidget>
    5. #include <windows.h>
    6.  
    7. const DWORD MAXSIZE = 64;
    8. const BYTE PING = 0x05;
    9. //const PCHAR vid_pid = "vid_04d8&pid_1510"; // VID e PID
    10. //const PCHAR out_pipe = "\\MCHP_EP1";
    11. //const PCHAR in_pipe = "\\MCHP_EP1";
    12.  
    13. const QString vid_pid = "vid_04d8&pid_1510"; // VID e PID --tem q ser convertido para PCHAR quando for chamado usado .toLocal8Bit().data
    14. const QString out_pipe = "\\MCHP_EP1"; // --tem q ser convertido para PCHAR quando for chamado usado .toLocal8Bit().data
    15. const QString in_pipe = "\\MCHP_EP1"; // --tem q ser convertido para PCHAR quando for chamado usado .toLocal8Bit().data
    16.  
    17. // QByteArray ba_vid_pid = vid_p.toLocal8Bit();
    18. // QByteArray ba_out_pipe = out_p.toLocal8Bit();
    19. // QByteArray ba_in_pipe = in_p.toLocal8Bit();
    20.  
    21. // PCHAR vid_pid = ba_vid_pid.data();
    22. // PCHAR out_pipe = ba_out_pipe.data();
    23. // PCHAR in_pipe = ba_in_pipe.data();
    24.  
    25. class PICConnector : public QWidget
    26. {
    27.  
    28. public:
    29. PICConnector(QWidget *parent = 0);
    30. virtual ~PICConnector();
    31. bool Open();
    32. void Close();
    33. void LoadDLL();
    34. DWORD SendData(PVOID SendData, DWORD SendLength, PVOID ReceiveData,
    35. PDWORD ReceiveLength, DWORD SendDelay, DWORD ReceiveDelay);
    36. DWORD getDLLversion();
    37. void CheckInvalidHandle();
    38.  
    39. private:
    40. //USB
    41. HINSTANCE hinstLib;
    42. HANDLE myOutPipe;
    43. HANDLE myInPipe;
    44. };
    45.  
    46. #endif // PICCONNECTOR_H
    To copy to clipboard, switch view to plain text mode 
    picconnector.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include <stdlib.h>
    3. #include <stdio.h>
    4. #include "picconnector.h"
    5. #include "mpusbapi.h"
    6.  
    7. PICConnector::PICConnector(QWidget *parent)
    8. : QWidget(parent)
    9. {
    10. LoadDLL();
    11. myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
    12. }
    13.  
    14. //--------------------------------------------------------------------------------
    15.  
    16. // Load DLL file
    17. void PICConnector::LoadDLL()
    18. {
    19.  
    20. hinstLib = LoadLibrary(TEXT("mpusbapi.dll"));
    21. if (hinstLib == NULL) {
    22. printf("ERROR: unable to load DLL\n");
    23. QMessageBox::warning(this, tr("LoadLibrary"),
    24. tr("ERROR: unable to load DLL"),
    25. FreeLibrary(hinstLib);
    26. }
    27. // Get function pointer
    28. else{
    29. MPUSBGetDLLVersion=(DWORD(*)(void))\
    30. GetProcAddress(hinstLib,"_MPUSBGetDLLVersion");
    31.  
    32. MPUSBGetDeviceCount=(DWORD(*)(PCHAR))\
    33. GetProcAddress(hinstLib,"_MPUSBGetDeviceCount");
    34. MPUSBOpen=(HANDLE(*)(DWORD,PCHAR,PCHAR,DWORD,DWORD))\
    35. GetProcAddress(hinstLib,"_MPUSBOpen");
    36. MPUSBWrite=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\
    37. GetProcAddress(hinstLib,"_MPUSBWrite");
    38. MPUSBRead=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\
    39. GetProcAddress(hinstLib,"_MPUSBRead");
    40. MPUSBReadInt=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\
    41. GetProcAddress(hinstLib,"_MPUSBReadInt");
    42. MPUSBClose=(BOOL(*)(HANDLE))GetProcAddress(hinstLib,"_MPUSBClose");
    43.  
    44. if((MPUSBGetDeviceCount == NULL) || (MPUSBOpen == NULL) ||
    45. (MPUSBWrite == NULL) || (MPUSBRead == NULL) ||
    46. (MPUSBClose == NULL) || (MPUSBGetDLLVersion == NULL) ||
    47. (MPUSBReadInt == NULL)){
    48. QMessageBox::warning(this, "LoadDLL",
    49. tr("ERROR: unable to find DLL function"),
    50. FreeLibrary(hinstLib);
    51. }
    52. }
    53. //FreeLibrary(hinstLib);
    54. }
    55.  
    56. //--------------------------------------------------------------------------------
    57.  
    58.  
    59. PICConnector::~PICConnector()
    60. {
    61. if (myOutPipe != INVALID_HANDLE_VALUE)
    62. MPUSBClose(myOutPipe);
    63. if ( myInPipe != INVALID_HANDLE_VALUE)
    64. MPUSBClose(myInPipe);
    65.  
    66. myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
    67.  
    68. // Always check to close the library too.
    69. if (hinstLib != NULL) FreeLibrary(hinstLib);
    70.  
    71. }
    72.  
    73. //--------------------------------------------------------------------------------
    74. // retorna true se conseguiu abrir(inicializar) o dispositivo
    75.  
    76. bool PICConnector::Open()
    77. {
    78.  
    79. // Always open one device only
    80.  
    81. if (MPUSBGetDeviceCount(vid_pid.toLocal8Bit().data()) > 0) {
    82.  
    83.  
    84. myOutPipe = MPUSBOpen(0,vid_pid.toLocal8Bit().data(),out_pipe.toLocal8Bit().data(),MP_WRITE,0);
    85. myInPipe = MPUSBOpen(0,vid_pid.toLocal8Bit().data(),out_pipe.toLocal8Bit().data(),MP_READ,0);
    86.  
    87. if(myOutPipe == INVALID_HANDLE_VALUE || myInPipe == INVALID_HANDLE_VALUE)
    88. {
    89. QMessageBox::warning(this, tr("MPUSBOpen"),
    90. tr("ERROR: Failed to open data pipes"),
    91. return FALSE;
    92. }//end if
    93.  
    94. // testa a conexão - envia ping
    95. // DWORD ReceiveLength, SentLength;
    96. //
    97. // byte send_buffer[1];
    98. // byte recv_buffer[1];
    99. // send_buffer[0] = PING;
    100. // MPUSBWrite(myOutPipe,send_buffer,1,&SentLength,1000);
    101. // MPUSBRead(myInPipe,recv_buffer,MAXSIZE,&ReceiveLength,1000);
    102. // if ((ReceiveLength!=1)|(recv_buffer[0]!=PING)){
    103. // QMessageBox::warning(this, tr("Teste Conexao"),
    104. // tr("ERROR: O teste de conexao falhou\n"),
    105. // QMessageBox::Ok);
    106. // }
    107. }
    108. else {
    109. QMessageBox::warning(this, tr("MPUSBGetDeviceCount"),
    110. tr("ERROR: No devices connected"),
    111. return FALSE;
    112. }
    113. return TRUE;
    114. }
    115.  
    116. //--------------------------------------------------------------------------------
    117.  
    118. void PICConnector::Close()
    119. {
    120. MPUSBClose(myOutPipe);
    121. MPUSBClose(myInPipe);
    122. myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
    123. }
    124.  
    125. //--------------------------------------------------------------------------------
    126.  
    127. DWORD PICConnector::getDLLversion()
    128. {
    129. return MPUSBGetDLLVersion();
    130. }
    131.  
    132. //--------------------------------------------------------------------------------
    133.  
    134. // envia "data" e le o que foi enviado para checar que foi transmitido corretamente
    135. DWORD PICConnector::SendData(PVOID SendData, DWORD SendLength, PVOID ReceiveData,
    136. PDWORD ReceiveLength, DWORD SendDelay, DWORD ReceiveDelay)
    137. {
    138. DWORD SentDataLength;
    139. DWORD ExpectedReceiveLength = *ReceiveLength;
    140.  
    141. if(myOutPipe != INVALID_HANDLE_VALUE && myInPipe != INVALID_HANDLE_VALUE)
    142. {
    143. if(MPUSBWrite(myOutPipe,SendData,SendLength,&SentDataLength,SendDelay))
    144. {
    145.  
    146. if(MPUSBRead(myInPipe,ReceiveData, ExpectedReceiveLength,
    147. ReceiveLength,ReceiveDelay))
    148. {
    149. if(*ReceiveLength == ExpectedReceiveLength)
    150. {
    151. return 1; // Success!
    152. }
    153. else if(*ReceiveLength < ExpectedReceiveLength)
    154. {
    155. return 2; // Partially failed, incorrect receive length
    156. }//end if else
    157. }
    158. else
    159. CheckInvalidHandle();
    160. }
    161. else
    162. CheckInvalidHandle();
    163. }//end if
    164.  
    165. return 0; // Operation Failed
    166. }//end SendReceivePacket
    167.  
    168. //--------------------------------------------------------------------------------
    169.  
    170. void PICConnector::CheckInvalidHandle()
    171. {
    172. if(GetLastError() == ERROR_INVALID_HANDLE)
    173. {
    174. // Most likely cause of the error is the board was disconnected.
    175. MPUSBClose(myOutPipe);
    176. MPUSBClose(myInPipe);
    177. myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
    178. }//end if
    179. else
    180. QMessageBox::warning(this, tr("LoadLibrary"),
    181. tr("Error Code %1").arg(QString::number(GetLastError(),16)),
    182. }//end CheckInvalidHandle
    To copy to clipboard, switch view to plain text mode 

    testempusb.h
    Qt Code:
    1. #ifndef TESTEMPUSB_H
    2. #define TESTEMPUSB_H
    3.  
    4. #include <QWidget>
    5. #include <windows.h>
    6.  
    7. class PICConnector;
    8. class QLineEdit;
    9. class QLabel;
    10.  
    11. class testempusb : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. testempusb(QWidget *parent = 0);
    17.  
    18. private slots:
    19. void Send();
    20. void ShowDLLversion();
    21.  
    22. private:
    23. QLabel *label;
    24. QLabel *label2;
    25. QLineEdit *lineEdit;
    26. QLineEdit *lineEdit2;
    27. QPushButton *okButton;
    28. QPushButton *sendButton;
    29.  
    30. PICConnector *device;
    31. };
    32.  
    33. #endif // TESTEMPUSB_H
    To copy to clipboard, switch view to plain text mode 

    testempusb.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "testempusb.h"
    3. #include "picconnector.h"
    4.  
    5. testempusb::testempusb(QWidget *parent)
    6. : QWidget(parent)
    7.  
    8. {
    9. device = new PICConnector;
    10. label = new QLabel(tr("Get DLL Version:"));
    11. label2 = new QLabel(tr("Data to Send(00-FF):"));
    12. lineEdit = new QLineEdit;
    13. lineEdit2 = new QLineEdit;
    14. label->setBuddy(lineEdit);
    15. label2->setBuddy(lineEdit2);
    16. okButton = new QPushButton(tr("&Ok"));
    17. sendButton = new QPushButton(tr("&Send"));
    18. connect(okButton, SIGNAL(clicked()),this, SLOT(ShowDLLversion()));
    19. connect(sendButton,SIGNAL(clicked()),this,SLOT(Send()));
    20.  
    21. QHBoxLayout *firstLayout = new QHBoxLayout;
    22. firstLayout->addWidget(label);
    23. firstLayout->addWidget(lineEdit);
    24. firstLayout->addWidget(okButton);
    25.  
    26. QHBoxLayout *secondLayout = new QHBoxLayout;
    27. secondLayout->addWidget(label2);
    28. secondLayout->addWidget(lineEdit2);
    29. secondLayout->addWidget(sendButton);
    30.  
    31. QVBoxLayout *boxLayout = new QVBoxLayout;
    32. boxLayout->addLayout(firstLayout);
    33. boxLayout->addLayout(secondLayout);
    34. setLayout(boxLayout);
    35.  
    36. }
    37.  
    38. //--------------------------------------------------------------------------------
    39.  
    40. void testempusb::ShowDLLversion()
    41. {
    42. DWORD temp = device->getDLLversion();
    43.  
    44. int aux1 = HIWORD(temp)>>8;
    45. int aux2 = HIWORD(temp) & 0xFF;
    46. int aux3 = LOWORD(temp)>>8;
    47. int aux4 = LOWORD(temp) & 0xFF;
    48.  
    49. lineEdit->setText( tr("MPUSBAPI Version:%1.%2.%3.%4").arg(QString::number(aux1,10))
    50. .arg(QString::number(aux2,10)).arg(QString::number(aux3,10))
    51. .arg(QString::number(aux4,10)));
    52. }
    53.  
    54. //--------------------------------------------------------------------------------
    55.  
    56. void testempusb::Send()
    57. {
    58. sendButton->setDisabled(true);
    59. if (device->Open()){
    60.  
    61. BYTE send_buf[64],receive_buf[64];
    62. DWORD RecvLength=1;
    63.  
    64. send_buf[0] = PING;
    65. bool ok;
    66. send_buf[1] = lineEdit2->text().toInt(&ok,16);
    67.  
    68. if( device->SendData(send_buf,2,receive_buf,&RecvLength,1000,1000) == 1){ //testar ==0
    69.  
    70. if(RecvLength != 1 || receive_buf[0] != PING ){
    71. QMessageBox::warning(this, tr("Send"),
    72. tr("Failed to transmit DATA"),
    73. }
    74. else{
    75. QMessageBox::warning(this, tr("Send"),
    76. tr("Dados transmitidos com sucesso!"),
    77. }
    78. }
    79. }
    80. device->Close();
    81. sendButton->setEnabled(true);
    82. }
    To copy to clipboard, switch view to plain text mode 

    Thats all. As I said before the code only compiled when I wrote the QString as const QString in the picconnector.h file.

Similar Threads

  1. convert QString to int
    By mattia in forum Newbie
    Replies: 2
    Last Post: 4th January 2008, 09:10
  2. Cannot convert double to QString
    By maxpower in forum Qt Programming
    Replies: 9
    Last Post: 24th December 2007, 03:04
  3. how to convert int to QString?
    By phillip_Qt in forum Newbie
    Replies: 2
    Last Post: 5th October 2007, 08:07
  4. How to convert Int to QString in QT4?
    By drake1983 in forum Newbie
    Replies: 2
    Last Post: 11th March 2007, 06:58
  5. How to convert from QString to quint16 ?
    By probine in forum Qt Programming
    Replies: 5
    Last Post: 31st March 2006, 09:00

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.