From 10:00 CET Friday 22nd November we will adjust how the server works to deal with some recently reported problems. Therefore there may be a load problem, if you experience more problems than usual trying to access the forum then please PM AndyS or any of the moderators so they can inform me.


Compile Error Newb: cannot convert char to lpcwstr



  • Hello Community,
    iam new in working with QT.
    I only want to compile some Code for my 64bit machine.
    I loaded the code but if i compile it i get the following failure in the code:
    @/**
    Opens the COMM Port.
    @param port Port number. COMM1 = 1, COMM2 = 2, etc.
    @param baud Baud rate, in BPS. Commonly 9600, 38400, etc.
    @param HwFc Set to non-zero to use hardware flow-control, or zero
    for no flow control.
    @param hwnd Window handle to recieve MESS_SERIAL messages. If set to NULL,
    you can still get characters using SerialGetChar().
    @return TRUE if port was opened, FALSE if opening failed.
    */
    BOOL OpenPort(int port,long baud,int HwFc, HWND hwnd)
    {
    HANDLE Comport;
    DCB myDCB;
    COMMTIMEOUTS CTout;
    char str[100];

    FlowControl = HwFc;

    // Open the serial port
    if (port > 9)
    sprintf(str,"\\.\COM%d",port);
    else
    sprintf(str,"COM%d",port);
    Comport = CreateFile(str,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
    if (Comport == INVALID_HANDLE_VALUE)
    return FALSE;
    // Configure Serial port (Setup Comm)
    if (!SetupComm(Comport,350,350)) // Buffer sizes
    return FALSE;

    // setup DCB using current values
    if (!GetCommState(Comport,&myDCB))
    return FALSE;
    myDCB.fInX = FALSE; // Turn off xon/xoff handler
    myDCB.fOutX = FALSE;
    myDCB.fOutxDsrFlow = FALSE;
    if (HwFc) // no flow control
    {
    myDCB.fOutxCtsFlow = TRUE; // no hardware flow control.
    myDCB.fRtsControl = RTS_CONTROL_HANDSHAKE;
    }
    else
    {
    myDCB.fOutxCtsFlow = FALSE; // no hardware flow control.
    }

    myDCB.BaudRate = baud;
    myDCB.DCBlength = sizeof(DCB);
    myDCB.fBinary = 1;
    myDCB.fParity = 0;
    myDCB.fDtrControl = DTR_CONTROL_DISABLE;
    myDCB.fDsrSensitivity = 0;
    myDCB.fTXContinueOnXoff = 1;
    myDCB.fNull = 0;
    myDCB.fRtsControl = RTS_CONTROL_DISABLE;
    myDCB.fDummy2 = 0;
    myDCB.wReserved = 0;
    myDCB.Parity = NOPARITY;
    myDCB.StopBits = ONESTOPBIT;
    myDCB.wReserved1 = 0;
    myDCB.ByteSize = 8;

    if (!SetCommState(Comport,&myDCB))
    {
    ShowLastError();
    return FALSE;
    }

        // Set timeouts
    

    //CTout.ReadIntervalTimeout = 0xffffffff;

    CTout.ReadIntervalTimeout = MAXDWORD;
    CTout.ReadTotalTimeoutMultiplier = 0;
    CTout.ReadTotalTimeoutConstant = 0;
    CTout.WriteTotalTimeoutMultiplier = 0;
    CTout.WriteTotalTimeoutConstant = 5000; // don't hang if CTS is locked, for example

    SetCommTimeouts(Comport,&CTout);

    EscapeCommFunction(Comport,SETDTR);
    PurgeComm(Comport,PURGE_TXCLEAR | PURGE_RXCLEAR);

    handle = hwnd;
    SerialPort = Comport;
    StartCommThread();

    return TRUE;
    }@

    Failure:

    C:\Qt\Tools\QtCreator\bin\untitled\serial.cpp:262: Fehler: cannot convert 'char*' to 'LPCWSTR {aka const wchar_t*}' for argument '1' to 'void* CreateFileW(LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE)'
    Comport = CreateFile(str,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
    ^

    Can anybody say what can I change that it will work?
    The complete code is downloadable from "http://www.mikrocontroller.net/attachment/87079/fboot-win-devcpp-mod.zip":http://www.mikrocontroller.net/attachment/87079/fboot-win-devcpp-mod.zip



  • You try to pass a char 'str' to a function (createFile) that expects a LPCWSTR as type.

    Try it with C-cast:
    @CreateFile((LPCWSTR)str,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);@




Log in to reply